Examples of JobManager


Examples of org.apache.sling.event.jobs.JobManager

                return JobResult.OK;
            }
         });

        try {
            final JobManager jobManager = this.getJobManager();

            // we start 20 jobs, every second job has no processor
            final int COUNT = 20;
            for(int i = 0; i < COUNT; i++ ) {
                final String jobTopic = (i % 2 == 0 ? TOPIC : TOPIC + "2");

                jobManager.addJob(jobTopic, null);
            }
            while ( count.get() < COUNT / 2) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ie) {
                    // ignore
                }
            }

            assertEquals("Finished count", COUNT / 2, count.get());
            // unprocessed count should be 0 as there is no job consumer for this job
            assertEquals("Unprocessed count", 0, jobManager.getStatistics().getNumberOfJobs());
            assertEquals("Finished count", COUNT / 2, jobManager.getStatistics().getNumberOfFinishedJobs());

            // now remove jobs
            for(final Job j : jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "2", -1, (Map<String, Object>[])null)) {
                jobManager.removeJobById(j.getId());
            }
        } finally {
            eh1.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.jobs.JobManager

                    public void handleEvent(Event event) {
                        finishedEvents.add(event);
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();

            final List<String> list = new ArrayList<String>();
            list.add("1");
            list.add("2");

            final EventPropertiesMap map = new EventPropertiesMap();
            map.put("a", "a1");
            map.put("b", "b2");

            // we start a single job
            final Map<String, Object> props = new HashMap<String, Object>();
            props.put("string", "Hello");
            props.put("int", new Integer(5));
            props.put("long", new Long(7));
            props.put("list", list);
            props.put("map", map);

            final String jobId = jobManager.addJob(TOPIC, props).getId();

            new RetryLoop(Conditions.collectionIsNotEmptyCondition(finishedEvents,
                    "Waiting for finishedEvents to have at least one element"), 5, 50);

            // no jobs queued, none processed and no available
            new RetryLoop(new RetryLoop.Condition() {

                @Override
                public String getDescription() {
                    return "Waiting for job to be processed. Conditions: queuedJobs=" + jobManager.getStatistics().getNumberOfQueuedJobs() +
                            ", jobsCount=" + processedJobsCount + ", findJobs=" +
                            jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[]) null)
                            .size();
                }

                @Override
                public boolean isTrue() throws Exception {
                    return jobManager.getStatistics().getNumberOfQueuedJobs() == 0
                            && processedJobsCount.get() == 1
                            && jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[]) null)
                                    .size() == 0;
                }
            }, CONDITION_TIMEOUT_SECONDS, CONDITION_INTERVAL_MILLIS);

            final String jobTopic = (String)finishedEvents.get(0).getProperty(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC);
            assertNotNull(jobTopic);
            assertEquals("Hello", finishedEvents.get(0).getProperty("string"));
            assertEquals(new Integer(5), Integer.valueOf(finishedEvents.get(0).getProperty("int").toString()));
            assertEquals(new Long(7), Long.valueOf(finishedEvents.get(0).getProperty("long").toString()));
            assertEquals(list, finishedEvents.get(0).getProperty("list"));
            assertEquals(map, finishedEvents.get(0).getProperty("map"));

            jobManager.removeJobById(jobId);
        } finally {
            jcReg.unregister();
            ehReg.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.jobs.JobManager

                    public void handleEvent(Event event) {
                        finishedEvents.add(event);
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();

            // dao is an invisible class for the dynamic class loader as it is not public
            // therefore scheduling this job should fail!
            final DataObject dao = new DataObject();

            // we start a single job
            final Map<String, Object> props = new HashMap<String, Object>();
            props.put("dao", dao);

            final String id = jobManager.addJob(TOPIC + "/failed", props).getId();

            // wait until the conditions are met
            new RetryLoop(new RetryLoop.Condition() {

                @Override
                public boolean isTrue() throws Exception {
                    return failedJobsCount.get() == 0
                            && finishedEvents.size() == 0
                            && jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1,
                                    (Map<String, Object>[]) null).size() == 1
                            && jobManager.getStatistics().getNumberOfQueuedJobs() == 1
                            && jobManager.getStatistics().getNumberOfActiveJobs() == 0;
                }

                @Override
                public String getDescription() {
                    return "Waiting for job failure to be recorded. Conditions " +
                           "faildJobsCount=" + failedJobsCount.get() +
                           ", finishedEvents=" + finishedEvents.size() +
                           ", findJobs= " + jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1,
                                   (Map<String, Object>[]) null).size()
                           +", queuedJobs=" + jobManager.getStatistics().getNumberOfQueuedJobs()
                           +", activeJobs=" + jobManager.getStatistics().getNumberOfActiveJobs();
                }
            }, CONDITION_TIMEOUT_SECONDS, CONDITION_INTERVAL_MILLIS);

            jobManager.removeJobById(id); // moves the job to the history section
            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1, (Map<String, Object>[])null).size());

            jobManager.removeJobById(id); // removes the job permanently
        } finally {
            jcReg.unregister();
            ehReg.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.jobs.JobManager

    /**
     * Ordered Queue Test
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testOrderedQueue() throws Exception {
        final JobManager jobManager = this.getJobManager();

        // register consumer and event handler
        final Barrier cb = new Barrier(2);
        final AtomicInteger count = new AtomicInteger(0);
        final AtomicInteger parallelCount = new AtomicInteger(0);
        final ServiceRegistration jcReg = this.registerJobConsumer("sling/orderedtest/*",
                new JobConsumer() {

                    private volatile int lastCounter = -1;

                    @Override
                    public JobResult process(final Job job) {
                        final int counter = job.getProperty("counter", -10);
                        assertNotEquals("Counter property is missing", -10, counter);
                        assertTrue("Counter should only increment by max of 1 " + counter + " - " + lastCounter,
                                counter == lastCounter || counter == lastCounter +1);
                        lastCounter = counter;
                        if ("sling/orderedtest/start".equals(job.getTopic()) ) {
                            cb.block();
                            return JobResult.OK;
                        }
                        if ( parallelCount.incrementAndGet() > 1 ) {
                            parallelCount.decrementAndGet();
                            return JobResult.FAILED;
                        }
                        final String topic = job.getTopic();
                        if ( topic.endsWith("sub1") ) {
                            final int i = (Integer)job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
                            if ( i == 0 ) {
                                parallelCount.decrementAndGet();
                                return JobResult.FAILED;
                            }
                        }
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException ie) {
                            // ignore
                        }
                        parallelCount.decrementAndGet();
                        return JobResult.OK;
                    }
                });
        final ServiceRegistration ehReg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED,
                new EventHandler() {

                    @Override
                    public void handleEvent(final Event event) {
                        count.incrementAndGet();
                    }
                });

        try {
            // we first sent one event to get the queue started
            final Map<String, Object> properties = new HashMap<String, Object>();
            properties.put("counter", -1);
            jobManager.addJob("sling/orderedtest/start", properties);
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();

            // get the queue
            final Queue q = jobManager.getQueue("orderedtest");
            assertNotNull("Queue 'orderedtest' should exist!", q);

            // suspend it
            q.suspend();

            final int NUM_JOBS = 30;

            // we start "some" jobs:
            for(int i = 0; i < NUM_JOBS; i++ ) {
                final String subTopic = "sling/orderedtest/sub" + (i % 10);
                properties.clear();
                properties.put("counter", i);
                jobManager.addJob(subTopic, properties);
            }
            // start the queue
            q.resume();
            while ( count.get() < NUM_JOBS +1 ) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    // ignore
                }
            }
            // we started one event before the test, so add one
            assertEquals("Finished count", NUM_JOBS + 1, count.get());
            assertEquals("Finished count", NUM_JOBS + 1, jobManager.getStatistics().getNumberOfFinishedJobs());
            assertEquals("Finished count", NUM_JOBS + 1, q.getStatistics().getNumberOfFinishedJobs());
            assertEquals("Failed count", NUM_JOBS / 10, q.getStatistics().getNumberOfFailedJobs());
            assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
        } finally {
            jcReg.unregister();
View Full Code Here

Examples of org.apache.sling.event.jobs.JobManager

        super.cleanup();
    }

    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testRoundRobinQueue() throws Exception {
        final JobManager jobManager = this.getJobManager();

        final Barrier cb = new Barrier(2);

        final ServiceRegistration jc1Reg = this.registerJobConsumer(TOPIC + "/start",
                new JobConsumer() {

                    @Override
                    public JobResult process(final Job job) {
                        cb.block();
                        return JobResult.OK;
                    }
                });

        // register new consumer and event handle
        final AtomicInteger count = new AtomicInteger(0);
        final AtomicInteger parallelCount = new AtomicInteger(0);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC + "/*",
                new JobConsumer() {

                    @Override
                    public JobResult process(final Job job) {
                        if ( parallelCount.incrementAndGet() > MAX_PAR ) {
                            parallelCount.decrementAndGet();
                            return JobResult.FAILED;
                        }
                        sleep(30);
                        parallelCount.decrementAndGet();
                        return JobResult.OK;
                    }
                });
        final ServiceRegistration ehReg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED,
                new EventHandler() {

                    @Override
                    public void handleEvent(final Event event) {
                        count.incrementAndGet();
                    }
                });

        try {
            // we first sent one event to get the queue started
            jobManager.addJob(TOPIC + "/start", null);
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();

            // get the queue
            final Queue q = jobManager.getQueue(QUEUE_NAME);
            assertNotNull("Queue '" + QUEUE_NAME + "' should exist!", q);

            // suspend it
            q.suspend();

            // we start "some" jobs:
            // first jobs without id
            for(int i = 0; i < NUM_JOBS; i++ ) {
                final String subTopic = TOPIC + "/sub" + (i % 10);
                jobManager.addJob(subTopic, null);
            }
            // second jobs with id
            for(int i = 0; i < NUM_JOBS; i++ ) {
                final String subTopic = TOPIC + "/sub" + (i % 10);
                jobManager.addJob(subTopic, "id" + i, null);
            }
            // start the queue
            q.resume();
            while ( count.get() < 2 * NUM_JOBS  + 1 ) {
                assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
                assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
                sleep(500);
            }
            // we started one event before the test, so add one
            assertEquals("Finished count", 2 * NUM_JOBS + 1, count.get());
            assertEquals("Finished count", 2 * NUM_JOBS + 1, jobManager.getStatistics().getNumberOfFinishedJobs());
            assertEquals("Finished count", 2 * NUM_JOBS + 1, q.getStatistics().getNumberOfFinishedJobs());
            assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
            assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
        } finally {
            jc1Reg.unregister();
View Full Code Here

Examples of org.apache.sling.event.jobs.JobManager

            } catch (final InterruptedException e) {
                Thread.currentThread().interrupt();
                this.running = false;
            }
            if ( event != null && this.running ) {
                final JobManager jm = this.jobManager;
                if ( jm == null ) {
                    try {
                        this.writeQueue.put(event);
                        Thread.sleep(500);
                    } catch (final InterruptedException ie) {
View Full Code Here

Examples of org.apache.stanbol.commons.jobs.api.JobManager

        // No id
        if(id == null || id.equals("")){
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
       
        JobManager m = getJobManager();

        // If the job exists
        if (m.hasJob(id)) {
            log.info("Found job with id {}", id);
            Future<?> f = m.ping(id);
            this.info = new JobInfoImpl();
            if(f.isDone()){
                // The job is finished
                if(f.isCancelled()){
                    // NOTE: Canceled jobs should never exist.
                    // The web service remove any deleted process from the manager
                    // If a process have been canceled programmatically, it cannot be managed by the service anymore
                    // (except for DELETE)
                    log.warn("Job with id {} have been canceled. Returning 404 Not found.", id);
                    return Response.status(Response.Status.NOT_FOUND).build();
                }else{
                    // Job is complete
                    info.setFinished();
                    info.addMessage("You can remove this job using DELETE");
                }
            }else{
                // the job exists but it is not complete
                info.setRunning();
                info.addMessage("You can interrupt this job using DELETE");
            }
            // Returns 200, the job exists
            info.setOutputLocation(getPublicBaseUri() + m.getResultLocation(id));

            if(isHTML()){
                // Result as HTML
                return Response.ok(new Viewable("info", this)).build();
            }else{
View Full Code Here

Examples of org.apache.stanbol.commons.jobs.api.JobManager

    @Path("/{jid}")
    public Response delete(@PathParam(value = "jid") String jid){
        log.info("Called DELETE ({})", jid);
        if(!jid.equals("")){
            log.info("Looking for test job {}", jid);
            JobManager m = getJobManager();

            // If the job exists
            if (m.hasJob(jid)){
                log.info("Deleting Job id {}", jid);
                m.remove(jid);
                return Response.ok("Job deleted.").build();
            }else {
                log.info("No job found with id {}", jid);
                return Response.status(Response.Status.NOT_FOUND).build();
            }
View Full Code Here

Examples of org.apache.stanbol.commons.jobs.api.JobManager

     * @return
     */
    @DELETE
    public Response delete(){
        log.info("Called DELETE all jobs");
        JobManager manager = getJobManager();
        manager.removeAll();
        return Response.ok("All jobs have been deleted.").build();
    }
View Full Code Here

Examples of org.apache.stanbol.commons.jobs.api.JobManager

        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 = getJobManager();
            // 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 = getJobManager();
            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");
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.