Search in sources :

Example 6 with JobManager

use of org.apache.sling.event.jobs.JobManager in project sling by apache.

the class JobHandlingTest method testGetJob.

/**
     * Test get a job
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testGetJob() throws Exception {
    final Barrier cb = new Barrier(2);
    final Barrier cb2 = new Barrier(2);
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        @Override
        public JobResult process(Job job) {
            cb.block();
            cb2.block();
            return JobResult.OK;
        }
    });
    final JobManager jobManager = this.getJobManager();
    final Job j = jobManager.addJob(TOPIC, null);
    cb.block();
    assertNotNull(jobManager.getJob(TOPIC, null));
    // and continue job
    cb2.block();
    jobManager.removeJobById(j.getId());
}
Also used : Barrier(org.apache.sling.event.impl.Barrier) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) JobManager(org.apache.sling.event.jobs.JobManager) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 7 with JobManager

use of org.apache.sling.event.jobs.JobManager 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 JobManager

use of org.apache.sling.event.jobs.JobManager in project sling by apache.

the class JobHandlingDistributionQueueTest method testPackageAdditionAndStatusCheck.

@SuppressWarnings("unchecked")
@Test
public void testPackageAdditionAndStatusCheck() throws Exception {
    JobManager jobManager = mock(JobManager.class);
    JobBuilder builder = mock(JobBuilder.class);
    when(builder.properties(any(Map.class))).thenReturn(builder);
    Job job = mock(Job.class);
    when(job.getId()).thenReturn("id-123");
    when(builder.add()).thenReturn(job);
    String topic = JobHandlingDistributionQueue.DISTRIBUTION_QUEUE_TOPIC + "/aname";
    when(jobManager.createJob(topic)).thenReturn(builder);
    when(jobManager.getJobById(anyString())).thenReturn(job);
    when(builder.properties(any(Map.class))).thenReturn(builder);
    DistributionQueue queue = new JobHandlingDistributionQueue("aname", topic, jobManager, true, DistributionQueueType.ORDERED);
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_PATHS, new String[] { "/foo" });
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD);
    DistributionQueueItem distributionQueueItem = new DistributionQueueItem("an-id", packageInfo);
    assertNotNull(queue.add(distributionQueueItem));
    DistributionQueueItemStatus status = queue.getItem(job.getId()).getStatus();
    assertNotNull(status);
    assertEquals(DistributionQueueItemState.QUEUED, status.getItemState());
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JobBuilder(org.apache.sling.event.jobs.JobBuilder) JobManager(org.apache.sling.event.jobs.JobManager) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.apache.sling.event.jobs.Job) Map(java.util.Map) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 9 with JobManager

use of org.apache.sling.event.jobs.JobManager in project sling by apache.

the class JobHandlingDistributionQueueProviderTest method testGetOrCreateNamedQueue.

@Test
public void testGetOrCreateNamedQueue() throws Exception {
    JobManager jobManager = mock(JobManager.class);
    BundleContext context = mock(BundleContext.class);
    JobHandlingDistributionQueueProvider jobHandlingdistributionQueueProvider = new JobHandlingDistributionQueueProvider("dummy-agent", jobManager, context);
    DistributionQueue queue = jobHandlingdistributionQueueProvider.getQueue("default");
    assertNotNull(queue);
}
Also used : DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JobManager(org.apache.sling.event.jobs.JobManager) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 10 with JobManager

use of org.apache.sling.event.jobs.JobManager in project sling by apache.

the class JobHandlingDistributionQueueProviderTest method testDisableQueueProcessing.

@Test
public void testDisableQueueProcessing() throws Exception {
    JobManager jobManager = mock(JobManager.class);
    ConfigurationAdmin configAdmin = mock(ConfigurationAdmin.class);
    Configuration config = mock(Configuration.class);
    when(configAdmin.createFactoryConfiguration(QueueConfiguration.class.getName(), null)).thenReturn(config);
    BundleContext context = mock(BundleContext.class);
    JobHandlingDistributionQueueProvider jobHandlingdistributionQueueProvider = new JobHandlingDistributionQueueProvider("dummy-agent", jobManager, context);
    jobHandlingdistributionQueueProvider.disableQueueProcessing();
}
Also used : QueueConfiguration(org.apache.sling.event.jobs.QueueConfiguration) Configuration(org.osgi.service.cm.Configuration) QueueConfiguration(org.apache.sling.event.jobs.QueueConfiguration) JobManager(org.apache.sling.event.jobs.JobManager) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

JobManager (org.apache.sling.event.jobs.JobManager)17 Test (org.junit.Test)16 Job (org.apache.sling.event.jobs.Job)12 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Event (org.osgi.service.event.Event)7 EventHandler (org.osgi.service.event.EventHandler)7 HashMap (java.util.HashMap)6 Barrier (org.apache.sling.event.impl.Barrier)6 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)3 Queue (org.apache.sling.event.jobs.Queue)3 BundleContext (org.osgi.framework.BundleContext)3 IOException (java.io.IOException)2 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)2 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)2 JobBuilder (org.apache.sling.event.jobs.JobBuilder)2 QueueConfiguration (org.apache.sling.event.jobs.QueueConfiguration)2