在Dropwizard中运行异步作业,并轮询其状态 [英] Running async jobs in dropwizard, and polling their status
问题描述
在dropwizard中,我需要实现异步作业并轮询它们的状态. 我在资源中有2个端点:
In dropwizard, I need to implement asynchronous jobs and poll their status. I have 2 endpoints for this in resource:
@Path("/jobs")
@Component
public class MyController {
@POST
@Produces(MediaType.APPLICATION_JSON)
public String startJob(@Valid MyRequest request) {
return 1111;
}
@GET
@Path("/{jobId}")
@Produces(MediaType.APPLICATION_JSON)
public JobStatus getJobStatus(@PathParam("id") String jobId) {
return JobStatus.READY;
}
}
我正在考虑使用石英来开始工作,但只能一次,不能重复.当请求状态时,我将获得触发器状态.但是,将石英用于非预定用途的想法看起来很奇怪. 有没有更好的方法呢?也许dropwizard本身提供了更好的工具?会提供任何建议.
I am considering to use quartz to start job, but only single time and without repeating. And when requesting status, I will get trigger status. But the idea of using quartz for none-scheduled usage looks weird. Is there any better approaches for this? Maybe dropwizard provides better tools itself? Will appriciate any advices.
更新:我还查看了 https://github.com/gresrun/jesque ,但可以找不到任何方法来轮询正在运行的作业的状态.
UPDATE: I also looking at https://github.com/gresrun/jesque, but can not find any way to poll the status of running job.
推荐答案
您可以使用Managed
界面.在下面的代码段中,我正在使用ScheduledExecutorService
执行任务,但是如果愿意,可以使用Quartz
代替.我更喜欢使用ScheduledExecutorService
,因为它更简单,更容易...
You can use the Managed
interface. In the snippet below I am using the ScheduledExecutorService
to exuecute jobs, but you can use Quartz
instead if you like. I prefer working with ScheduledExecutorService
as it is simpler and easier...
第一步是注册您的托管服务.
first step is to register your managed service.
environment.lifecycle().manage(new JobExecutionService());
第二步是编写它.
/**
* A wrapper around the ScheduledExecutorService so all jobs can start when the server starts, and
* automatically shutdown when the server stops.
* @author Nasir Rasul {@literal nasir@rasul.ca}
*/
public class JobExecutionService implements Managed {
private final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
@Override
public void start() throws Exception {
System.out.println("Starting jobs");
service.scheduleAtFixedRate(new HelloWorldJob(), 1, 1, TimeUnit.SECONDS);
}
@Override
public void stop() throws Exception {
System.out.println("Shutting down");
service.shutdown();
}
}
和工作本身
/**
* A very simple job which just prints the current time in millisecods
* @author Nasir Rasul {@literal nasir@rasul.ca}
*/
public class HelloWorldJob implements Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
System.out.println(System.currentTimeMillis());
}
}
如下面的注释中所述,如果使用Runnable
,则可以Thread.getState()
.请参考获取当前正在其中运行的所有线程的列表Java .根据您对应用程序的连接方式,您可能仍然需要一些中间件.
As mentioned in the comment below, if you use Runnable
, you can Thread.getState()
. Please refer to Get a List of all Threads currently running in Java. You may still need some intermediary pieces depending on how you're wiring you application.
这篇关于在Dropwizard中运行异步作业,并轮询其状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!