Search in sources :

Example 16 with JobConsumer

use of org.apache.sling.event.jobs.consumer.JobConsumer in project sling by apache.

the class ClassloadingTest method testFailedClassloading.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testFailedClassloading() throws Exception {
    final AtomicInteger failedJobsCount = new AtomicInteger(0);
    final List<Event> finishedEvents = Collections.synchronizedList(new ArrayList<Event>());
    this.registerJobConsumer(TOPIC + "/failed", new JobConsumer() {

        @Override
        public JobResult process(Job job) {
            failedJobsCount.incrementAndGet();
            return JobResult.OK;
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            finishedEvents.add(event);
        }
    });
    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();
    try {
        // 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() == 0 && 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);
        // moves the job to the history section
        jobManager.removeJobById(id);
        assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1, (Map<String, Object>[]) null).size());
    } finally {
        // removes the job permanently
        jobManager.removeJobById(id);
    }
}
Also used : HashMap(java.util.HashMap) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop) EventHandler(org.osgi.service.event.EventHandler) JobManager(org.apache.sling.event.jobs.JobManager) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.osgi.service.event.Event) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 17 with JobConsumer

use of org.apache.sling.event.jobs.consumer.JobConsumer in project sling by apache.

the class JobHandlingTest method testNotifications.

/**
     * Notifications.
     * We send several jobs which are treated different and then see
     * how many invocations have been sent.
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testNotifications() throws Exception {
    final List<String> cancelled = Collections.synchronizedList(new ArrayList<String>());
    final List<String> failed = Collections.synchronizedList(new ArrayList<String>());
    final List<String> finished = Collections.synchronizedList(new ArrayList<String>());
    final List<String> started = Collections.synchronizedList(new ArrayList<String>());
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        @Override
        public JobResult process(Job job) {
            // events 1 and 4 finish the first time
            final String id = (String) job.getProperty("id");
            if ("1".equals(id) || "4".equals(id)) {
                return JobResult.OK;
            // 5 fails always
            } else if ("5".equals(id)) {
                return JobResult.FAILED;
            } else {
                int retry = 0;
                if (job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT) != null) {
                    retry = (Integer) job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
                }
                // 2 fails the first time
                if ("2".equals(id)) {
                    if (retry == 0) {
                        return JobResult.FAILED;
                    } else {
                        return JobResult.OK;
                    }
                }
                // 3 fails the first and second time
                if ("3".equals(id)) {
                    if (retry == 0 || retry == 1) {
                        return JobResult.FAILED;
                    } else {
                        return JobResult.OK;
                    }
                }
            }
            return JobResult.FAILED;
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_CANCELLED, new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            final String id = (String) event.getProperty("id");
            cancelled.add(id);
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FAILED, new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            final String id = (String) event.getProperty("id");
            failed.add(id);
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            final String id = (String) event.getProperty("id");
            finished.add(id);
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_STARTED, new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            final String id = (String) event.getProperty("id");
            started.add(id);
        }
    });
    final JobManager jobManager = this.getJobManager();
    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());
}
Also used : EventHandler(org.osgi.service.event.EventHandler) JobManager(org.apache.sling.event.jobs.JobManager) IOException(java.io.IOException) Event(org.osgi.service.event.Event) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Aggregations

JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)17 Test (org.junit.Test)17 Job (org.apache.sling.event.jobs.Job)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 JobManager (org.apache.sling.event.jobs.JobManager)10 Barrier (org.apache.sling.event.impl.Barrier)7 Event (org.osgi.service.event.Event)7 EventHandler (org.osgi.service.event.EventHandler)7 HashMap (java.util.HashMap)5 Queue (org.apache.sling.event.jobs.Queue)3 BundleContext (org.osgi.framework.BundleContext)3 ServiceReference (org.osgi.framework.ServiceReference)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ScheduledJobInfo (org.apache.sling.event.jobs.ScheduledJobInfo)2 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RetryLoop (org.apache.sling.testing.tools.retry.RetryLoop)1