Search in sources :

Example 1 with Job

use of org.apache.stanbol.commons.jobs.api.Job in project stanbol by apache.

the class JobsResource method test.

/**
     * Creates a new background job to be used to test the service.
     * This method is for testing the service and to provide a sample implementation
     * of a long term operation started form a rest service.
     * 
     * @return
     */
@GET
@Path("/test{jid: (/.*)?}")
public Response test(@PathParam(value = "jid") String jid) {
    log.info("Called GET (create test job)");
    // If an Id have been provided, check whether the job has finished and return the result
    if (!jid.equals("")) {
        log.info("Looking for test job {}", jid);
        JobManager m = jobManager;
        // Remove first slash from param value
        jid = jid.substring(1);
        // If the job exists
        if (m.hasJob(jid)) {
            log.info("Found job with id {}", jid);
            Future<?> f = m.ping(jid);
            if (f.isDone() && (!f.isCancelled())) {
                /**
                     * We return OK with the result
                     */
                Object o;
                try {
                    o = f.get();
                    if (o instanceof JobResult) {
                        JobResult result = (JobResult) o;
                        return Response.ok(result.getMessage()).build();
                    } else {
                        log.error("Job {} is not a test job", jid);
                        throw new WebApplicationException(Response.Status.NOT_FOUND);
                    }
                } catch (InterruptedException e) {
                    log.error("Error: ", e);
                    throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
                } catch (ExecutionException e) {
                    log.error("Error: ", e);
                    throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
                }
            } else {
                /**
                     * We return 404 with additional info (Content-Location, the related job resource)
                     * 
                     * TODO
                     * Change into json representations
                     */
                String location = getPublicBaseUri() + "jobs/" + jid;
                String info = new StringBuilder().append("Result not ready.\n").append("Job Location: ").append(location).toString();
                return Response.status(404).header("Content-Location", location).header("Content-type", "text/plain").entity(info).build();
            }
        } else {
            log.info("No job found with id {}", jid);
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } else {
        // No id have been provided, we create a new test job
        JobManager m = jobManager;
        String id = m.execute(new Job() {

            @Override
            public JobResult call() throws Exception {
                for (int i = 0; i < 30; i++) {
                    try {
                        log.info("Test Process is working");
                        Thread.sleep(1000);
                    } catch (InterruptedException ie) {
                    }
                }
                return new JobResult() {

                    @Override
                    public String getMessage() {
                        return "This is a test job";
                    }

                    @Override
                    public boolean isSuccess() {
                        return true;
                    }
                };
            }

            @Override
            public String buildResultLocation(String jobId) {
                return "jobs/test/" + jobId;
            }
        });
        // This service returns 201 Created on success
        String location = getPublicBaseUri() + "jobs/" + id;
        String info = new StringBuilder().append("Job started.\n").append("Location: ").append(location).toString();
        return Response.created(URI.create(location)).header("Content-type", "text/plain").entity(info).build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JobResult(org.apache.stanbol.commons.jobs.api.JobResult) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) ExecutionException(java.util.concurrent.ExecutionException) Job(org.apache.stanbol.commons.jobs.api.Job) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with Job

use of org.apache.stanbol.commons.jobs.api.Job in project stanbol by apache.

the class TestJobManagerImpl method addJob.

private static void addJob() {
    final int max = countUntil;
    final int number = jobs.size() + 1;
    final int jst = jobsleepTime;
    jobs.add(jobManager.execute(new Job() {

        @Override
        public JobResult call() {
            final int num = number;
            for (int i = 0; i < max; i++) {
                try {
                    //log.debug("Process " + Integer.toString(num) + " is working");
                    Thread.sleep(jst);
                } catch (InterruptedException ie) {
                }
            }
            JobResult r = new JobResult() {

                @Override
                public String getMessage() {
                    return "This is process " + Integer.toString(num);
                }

                @Override
                public boolean isSuccess() {
                    return true;
                }
            };
            return r;
        }

        @Override
        public String buildResultLocation(String jobId) {
            return null;
        }
    }));
}
Also used : JobResult(org.apache.stanbol.commons.jobs.api.JobResult) Job(org.apache.stanbol.commons.jobs.api.Job)

Aggregations

Job (org.apache.stanbol.commons.jobs.api.Job)2 JobResult (org.apache.stanbol.commons.jobs.api.JobResult)2 ExecutionException (java.util.concurrent.ExecutionException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JobManager (org.apache.stanbol.commons.jobs.api.JobManager)1