Skip to content

23. Asynchronous jobs

JakeHoward edited this page Jul 16, 2015 · 1 revision

Using the JobsResource, you can post requests in utterlyidle to be handled asynchronously. Your request to run a request as a job returns immediately with a 202-Accepted. You can then monitor the current state of running jobs using the JobsResource.

Starting a Job

Using the run method on the JobsResource, you can start your asynchronous job. Here's an example:

private Response runTheJob() throws Exception {
Request request = post("some/url").build();
String jobPath = "/jobs/run/" + request.uri().path();
return application.handle(post(jobPath).build());
}

Line 1 we create the request that we want to be run asynchronously.

Line 2 we create the request for posting it as a job.

Line 3 we fire the job request into the application.

Viewing a Job

Next, we can use the list method to see what the state of our job is:

application.handle(get("/jobs/list").build())

The default response from this will be a json representation of the current running state of the jobs, eg.

{
"anyExists":true,
"items":[{
"status":"running",
"started":"Mon Jul 29 10:57:07 BST 2013",
"completed":"",
"duration":225,
"request":{
"raw":"POST /some/url HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
"method":"POST",
"uri":"/some/url",
"entity":""
},
"response":{}
}]
}

Deleting Jobs

Finally, you can kill all current running jobs & clear down the history of job results by calling deleteAll

application.handle(get("/jobs/deleteAll").build())

How do I get jobs in my application?

To get jobs in your application, all you need to add is the JobsModule

public static void addModules(Application application) {
application.add(new JobsModule());
}