spring – 在dropwizard中运行异步作业,并轮询其状态

在dropwizard中,我需要实现异步作业并轮询其状态.
我在资源中有2个端点:

@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本身提供更好的工具?会批评任何建议.

更新:我也看了https://github.com/gresrun/jesque,但找不到任何方式来轮询正在运行的工作状态.

解决方法:

您可以使用Managed接口.在下面的代码片段中,我使用ScheduledExecutorService来执行作业,但如果您愿意,可以使用Quartz.我更喜欢使用ScheduledExecutorService,因为它更简单,更容易……

第一步是注册您的托管服务.

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().请参阅Get a List of all Threads currently running in Java.您可能仍需要一些中间件,具体取决于您为应用程序布线的方式.

上一篇:Jenkins节点配置


下一篇:Azkaban实战