Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 在dropwizard中运行异步作业,并轮询其状态_Spring_Jobs_Dropwizard_Jesque - Fatal编程技术网

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

Spring 在dropwizard中运行异步作业,并轮询其状态,spring,jobs,dropwizard,jesque,Spring,Jobs,Dropwizard,Jesque,在dropwizard中,我需要实现异步作业并轮询它们的状态。 我在资源中有2个端点: @Path("/jobs") @Component public class MyController { @POST @Produces(MediaType.APPLICATION_JSON) public String startJob(@Valid MyRequest request) { return 1111; } @GET @Pat

在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本身提供了更好的工具?我会通知你任何建议


更新:我也在查看,但找不到任何方法来轮询正在运行的作业的状态。

您可以使用
管理的
界面。在下面的代码片段中,我使用了
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()
。请参阅。根据应用程序的连接方式,您可能仍然需要一些中间部件。

谢谢您的回复。但在这种情况下,我将如何获得正在运行的作业的状态?当用户将POST发送到“/作业”时,作业将启动并返回作业id。当用户发送“/jobs/”时,应该返回作业状态-无论它是否仍在运行。@me1111如果您使用
可运行的
,则可以使用常规的
线程.getState()
。你可能需要把一些东西拼凑在一起,让它端到端地工作,但所有必要的部分都是可用的。获取所有线程()的列表,然后调用它们的state方法。