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;
}
}
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.
UPDATE: I also looking at https://github.com/gresrun/jesque, but can not find any way to poll the status of running job.
You can use the
Managed
interface. In the snippet below I am using theScheduledExecutorService
to exuecute jobs, but you can useQuartz
instead if you like. I prefer working withScheduledExecutorService
as it is simpler and easier...first step is to register your managed service.
Second step is to write it.
and the job itself
As mentioned in the comment below, if you use
Runnable
, you canThread.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.