Search in sources :

Example 6 with JobConsumer

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

the class JobHandlingTest method testSimpleJobExecutionUsingJobConsumer.

/**
     * Test simple job execution.
     * The job is executed once and finished successfully.
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleJobExecutionUsingJobConsumer() throws Exception {
    final Barrier cb = new Barrier(2);
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            cb.block();
            return JobResult.OK;
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    assertTrue("No event received in the given time.", cb.block(5));
    cb.reset();
    assertFalse("Unexpected event received in the given time.", cb.block(5));
}
Also used : Barrier(org.apache.sling.event.impl.Barrier) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 7 with JobConsumer

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

the class OrderedQueueTest method testOrderedQueue.

/**
     * 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);
    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;
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            count.incrementAndGet();
        }
    });
    // 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());
}
Also used : HashMap(java.util.HashMap) EventHandler(org.osgi.service.event.EventHandler) Barrier(org.apache.sling.event.impl.Barrier) JobManager(org.apache.sling.event.jobs.JobManager) 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) Queue(org.apache.sling.event.jobs.Queue) Test(org.junit.Test)

Example 8 with JobConsumer

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

the class SchedulingTest method testScheduling.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testScheduling() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            if (job.getTopic().equals(TOPIC)) {
                counter.incrementAndGet();
            }
            return JobResult.OK;
        }
    });
    // we schedule three jobs
    final ScheduledJobInfo info1 = this.getJobManager().createJob(TOPIC).schedule().hourly(5).add();
    assertNotNull(info1);
    final ScheduledJobInfo info2 = this.getJobManager().createJob(TOPIC).schedule().daily(10, 5).add();
    assertNotNull(info2);
    final ScheduledJobInfo info3 = this.getJobManager().createJob(TOPIC).schedule().weekly(3, 19, 12).add();
    assertNotNull(info3);
    // scheduled jobs
    assertEquals(3, this.getJobManager().getScheduledJobs().size());
    info3.unschedule();
    // scheduled jobs
    assertEquals(2, this.getJobManager().getScheduledJobs().size());
    info1.unschedule();
    // scheduled jobs
    assertEquals(1, this.getJobManager().getScheduledJobs().size());
    info2.unschedule();
    // scheduled jobs
    assertEquals(0, this.getJobManager().getScheduledJobs().size());
}
Also used : ScheduledJobInfo(org.apache.sling.event.jobs.ScheduledJobInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 9 with JobConsumer

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

the class JobConsumerManagerTest method testSubCategoryMappingConsumer.

@Test
public void testSubCategoryMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn("a/**");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNotNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 10 with JobConsumer

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

the class JobConsumerManagerTest method testCategoryMappingConsumer.

@Test
public void testCategoryMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn("a/*");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) 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