Package org.apache.sling.event.jobs

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


                        cb.block();
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            final Job job = jobManager.addJob(TOPIC, null);

            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            // the job is retried after two seconds, so we wait again
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            // the job is retried after two seconds, so we wait again
            assertTrue("No event received in the given time.", cb.block(5));
            // we have reached the retry so we expect to not get an event
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
            assertEquals("Unexpected number of retries", 3, retryCountList.size());

            jobManager.removeJobById(job.getId());
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here


                        final String id = (String)event.getProperty("id");
                        started.add(id);
                    }
                });

        final JobManager jobManager = this.getJobManager();
        try {
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"1"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"2"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"3"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"4"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"5"));

            int count = 0;
            final long startTime = System.currentTimeMillis();
            do {
                count = finished.size() + cancelled.size();
                // after 25 seconds we cancel the test
                if ( System.currentTimeMillis() - startTime > 25000 ) {
                    throw new Exception("Timeout during notification test.");
                }
            } while ( count < 5 || started.size() < 10 );
            assertEquals("Finished count", 4, finished.size());
            assertEquals("Cancelled count", 1, cancelled.size());
            assertEquals("Started count", 10, started.size());
            assertEquals("Failed count", 5, failed.size());
        } finally {
            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
            for(final Job j : col) {
                jobManager.removeJobById(j.getId());
            }
            jcReg.unregister();
            eh1Reg.unregister();
            eh2Reg.unregister();
            eh3Reg.unregister();
View Full Code Here

                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

                    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

                    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

    /**
     * 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

        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

            } 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

TOP

Related Classes of org.apache.sling.event.jobs.JobManager

Copyright © 2018 www.massapicom. 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.