你如何每x分钟有效地重复一次动作?

时间:2021-05-10 02:30:22

I have an application that runs in JBoss. I have an incoming web service request that will update an ArrayList. I want to poll this list from another class every 60 seconds. What would be the most efficient way of doing this?

我有一个在JBoss中运行的应用程序。我有一个传入的Web服务请求将更新ArrayList。我想每隔60秒从另一个班级轮询这个列表。这样做最有效的方法是什么?

Could anyone point me to a good example?

有谁能指出我的好榜样?

6 个解决方案

#1


14  

I would also recommend ScheduledExecutorService, which offers increased flexibility over Timer and TimerTask including the ability to configure the service with multiple threads. This means that if a specific task takes a long time to run it will not prevent other tasks from commencing.

我还建议使用ScheduledExecutorService,它提供了比Timer和TimerTask更高的灵活性,包括使用多个线程配置服务的能力。这意味着如果特定任务需要很长时间才能运行,则不会阻止其他任务开始。

// Create a service with 3 threads.
ScheduledExecutorService execService = Executors.newScheduledThreadPool(3);

// Schedule a task to run every 5 seconds with no initial delay.
execService.scheduleAtFixedRate(new Runnable() {
  public void run() {
    System.err.println("Hello, World");
  }
}, 0L, 5L, TimeUnit.SECONDS);

#2


9  

As abyx posted, Timer and TimerTask are a good lightweight solution to running a class at a certain interval. If you need a heavy duty scheduler, may I suggest Quartz. It is an enterprise level job scheduler. It can easily handle thousands of scheduled jobs. Like I said, this might be overkill for your situation though.

正如abyx发布的那样,Timer和TimerTask是一个很好的轻量级解决方案,可以在一定的时间间隔内运行类。如果你需要一个重型调度程序,我可以建议Quartz。它是一个企业级作业调度程序。它可以轻松处理数千个预定的工作。就像我说的那样,这可能对你的情况有些过分。

#3


5  

You can use Timer and TimerTask. An example is shown here.

您可以使用Timer和TimerTask。这里显示了一个例子。

#4


0  

See java.util.Timer. You'll need to start a robot in a separate thread when your app comes up and have it do the polling.

请参阅java.util.Timer。当你的应用程序出现并让它进行轮询时,你需要在一个单独的线程中启动一个机器人。

#5


0  

Check the answers to the question "How to run a task daily from Java" for a list of resources related to your problem.

检查问题“如何每天从Java运行任务”的答案,以获取与您的问题相关的资源列表。

#6


0  

The other answers are basically advising you do your own threads. Nothing wrong with that, but it isn't in conformance with the EJB spec. If that is a problem, you can use JBoss' timer facilities. Here is an example of how to do that.

其他答案基本上建议你做自己的线程。没有错,但它与EJB规范不符。如果这是一个问题,你可以使用JBoss的计时器工具。这是一个如何做到这一点的例子。

However, if the EJB spec is at issue, storing state like an ArrayList isn't compliant as well, so if you are just reading some static variable anyway, specifically using a container Timer service is likely overkill.

但是,如果EJB规范存在争议,那么像ArrayList这样的存储状态也不合规,所以如果你只是读取一些静态变量,特别是使用容器定时服务可能有点过分。

#1


14  

I would also recommend ScheduledExecutorService, which offers increased flexibility over Timer and TimerTask including the ability to configure the service with multiple threads. This means that if a specific task takes a long time to run it will not prevent other tasks from commencing.

我还建议使用ScheduledExecutorService,它提供了比Timer和TimerTask更高的灵活性,包括使用多个线程配置服务的能力。这意味着如果特定任务需要很长时间才能运行,则不会阻止其他任务开始。

// Create a service with 3 threads.
ScheduledExecutorService execService = Executors.newScheduledThreadPool(3);

// Schedule a task to run every 5 seconds with no initial delay.
execService.scheduleAtFixedRate(new Runnable() {
  public void run() {
    System.err.println("Hello, World");
  }
}, 0L, 5L, TimeUnit.SECONDS);

#2


9  

As abyx posted, Timer and TimerTask are a good lightweight solution to running a class at a certain interval. If you need a heavy duty scheduler, may I suggest Quartz. It is an enterprise level job scheduler. It can easily handle thousands of scheduled jobs. Like I said, this might be overkill for your situation though.

正如abyx发布的那样,Timer和TimerTask是一个很好的轻量级解决方案,可以在一定的时间间隔内运行类。如果你需要一个重型调度程序,我可以建议Quartz。它是一个企业级作业调度程序。它可以轻松处理数千个预定的工作。就像我说的那样,这可能对你的情况有些过分。

#3


5  

You can use Timer and TimerTask. An example is shown here.

您可以使用Timer和TimerTask。这里显示了一个例子。

#4


0  

See java.util.Timer. You'll need to start a robot in a separate thread when your app comes up and have it do the polling.

请参阅java.util.Timer。当你的应用程序出现并让它进行轮询时,你需要在一个单独的线程中启动一个机器人。

#5


0  

Check the answers to the question "How to run a task daily from Java" for a list of resources related to your problem.

检查问题“如何每天从Java运行任务”的答案,以获取与您的问题相关的资源列表。

#6


0  

The other answers are basically advising you do your own threads. Nothing wrong with that, but it isn't in conformance with the EJB spec. If that is a problem, you can use JBoss' timer facilities. Here is an example of how to do that.

其他答案基本上建议你做自己的线程。没有错,但它与EJB规范不符。如果这是一个问题,你可以使用JBoss的计时器工具。这是一个如何做到这一点的例子。

However, if the EJB spec is at issue, storing state like an ArrayList isn't compliant as well, so if you are just reading some static variable anyway, specifically using a container Timer service is likely overkill.

但是,如果EJB规范存在争议,那么像ArrayList这样的存储状态也不合规,所以如果你只是读取一些静态变量,特别是使用容器定时服务可能有点过分。