如何在上午8点至上午10点之间每15分钟配置SinglethreadScheduledexecutor,然后全天每1hr跑15分钟

发布于 2025-02-13 16:58:02 字数 336 浏览 0 评论 0原文

我需要一些帮助,以实施如何实施SinglethreadScheduledexecutor,以在英国时间上午8点至上午10点之间每15分钟,然后全天每1hr跑。

目前,我有以下Scala代码 -

val pollingExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("MyTestScheduledThread"))

pollingExecutor.scheduleWithFixedDelay(new MyRunnableJob(), 0, 15, TimeUnit.MINUTES)

I need some assistance on how to implement the SingleThreadScheduledExecutor to run the job every 15mins between 8 am to 10 am UK time and then every 1hr throughout the day.

Currently, I have the following Scala code -

val pollingExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("MyTestScheduledThread"))

pollingExecutor.scheduleWithFixedDelay(new MyRunnableJob(), 0, 15, TimeUnit.MINUTES)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤ぢ 2025-02-20 16:58:02

我会在工作的工作周围制作一个包装器:

class ScheduledRun(
   job: Runnable, 
   scheduler: ScheduledExecutorService
 )(delay: => Duration) extends Runnable {
   def schedule(): Unit = delay match {
     case d: FiniteDuration => scheduler.schedule(this, d.toMillis, MILLISECONDS)
     case _ => 
   }
   def run(): Unit = {
     job.run()
     schedule()
   }
}

然后您可以做:

new ScheduledRun(myRunnableJob, pollingExecutor)(
   if (isMorningInUK) 15 minutes else 1 hour
).schedule

I would make a wrapper around the job that takes care of scheduling:

class ScheduledRun(
   job: Runnable, 
   scheduler: ScheduledExecutorService
 )(delay: => Duration) extends Runnable {
   def schedule(): Unit = delay match {
     case d: FiniteDuration => scheduler.schedule(this, d.toMillis, MILLISECONDS)
     case _ => 
   }
   def run(): Unit = {
     job.run()
     schedule()
   }
}

And then you can just do:

new ScheduledRun(myRunnableJob, pollingExecutor)(
   if (isMorningInUK) 15 minutes else 1 hour
).schedule
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文