Search in sources :

Example 11 with JobManager

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

the class JobHandlingDistributionQueueTest method testPackageAddition.

@SuppressWarnings("unchecked")
@Test
public void testPackageAddition() 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.findJobs(JobManager.QueryType.ALL, topic, -1)).thenReturn(Collections.<Job>emptySet());
    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));
}
Also used : 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 12 with JobManager

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

the class JobHandlingDistributionQueueProviderTest method testEnableQueueProcessing.

@Test
public void testEnableQueueProcessing() 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);
    DistributionQueueProcessor queueProcessor = mock(DistributionQueueProcessor.class);
    jobHandlingdistributionQueueProvider.enableQueueProcessing(queueProcessor);
}
Also used : DistributionQueueProcessor(org.apache.sling.distribution.queue.DistributionQueueProcessor) 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)

Example 13 with JobManager

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

the class JobHandlingTest method testCancelJob.

/**
     * Test canceling a job
     * The job execution always fails
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testCancelJob() 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.FAILED;
        }
    });
    final Map<String, Object> jobProperties = Collections.singletonMap("id", (Object) "cancelJobId");
    @SuppressWarnings("unchecked") final Map<String, Object>[] jobPropertiesAsArray = new Map[1];
    jobPropertiesAsArray[0] = jobProperties;
    // create job
    final JobManager jobManager = this.getJobManager();
    jobManager.addJob(TOPIC, jobProperties);
    cb.block();
    assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, jobPropertiesAsArray).size());
    // job is currently waiting, therefore cancel fails
    final Job e1 = jobManager.getJob(TOPIC, jobProperties);
    assertNotNull(e1);
    // and continue job
    cb2.block();
    sleep(200);
    // the job is now in the queue again
    final Job e2 = jobManager.getJob(TOPIC, jobProperties);
    assertNotNull(e2);
    assertTrue(jobManager.removeJobById(e2.getId()));
    assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, jobPropertiesAsArray).size());
    final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, jobPropertiesAsArray);
    try {
        assertEquals(1, col.size());
    } finally {
        for (final Job j : col) {
            jobManager.removeJobById(j.getId());
        }
    }
}
Also used : Barrier(org.apache.sling.event.impl.Barrier) JobManager(org.apache.sling.event.jobs.JobManager) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Map(java.util.Map) Test(org.junit.Test)

Example 14 with JobManager

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

the class RoundRobinQueueTest method testRoundRobinQueue.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testRoundRobinQueue() throws Exception {
    final JobManager jobManager = this.getJobManager();
    final Barrier cb = new Barrier(2);
    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 Set<Integer> maxParticipants = new HashSet<Integer>();
    this.registerJobConsumer(TOPIC + "/*", new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            final int max = parallelCount.incrementAndGet();
            if (max > MAX_PAR) {
                parallelCount.decrementAndGet();
                return JobResult.FAILED;
            }
            synchronized (maxParticipants) {
                maxParticipants.add(max);
            }
            sleep(job.getProperty("sleep", 30));
            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
    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:
    for (int i = 0; i < NUM_JOBS; i++) {
        final String subTopic = TOPIC + "/sub" + (i % 10);
        final Map<String, Object> props = new HashMap<String, Object>();
        if (i < 10) {
            props.put("sleep", 300);
        } else {
            props.put("sleep", 30);
        }
        jobManager.addJob(subTopic, props);
    }
    // start the queue
    q.resume();
    while (count.get() < NUM_JOBS + 1) {
        assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
        assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
        sleep(300);
    }
    // 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", 0, q.getStatistics().getNumberOfFailedJobs());
    assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
    for (int i = 1; i <= MAX_PAR; i++) {
        assertTrue("# Participants " + String.valueOf(i) + " not in " + maxParticipants, maxParticipants.contains(i));
    }
}
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) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with JobManager

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

the class ClassloadingTest method testSimpleClassloading.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleClassloading() throws Exception {
    final AtomicInteger processedJobsCount = new AtomicInteger(0);
    final List<Event> finishedEvents = Collections.synchronizedList(new ArrayList<Event>());
    final CountDownLatch latch = new CountDownLatch(1);
    this.registerJobConsumer(TOPIC, new JobConsumer() {

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

        @Override
        public void handleEvent(Event event) {
            finishedEvents.add(event);
            latch.countDown();
        }
    });
    final JobManager jobManager = this.getJobManager();
    final List<String> list = new ArrayList<String>();
    list.add("1");
    list.add("2");
    final Map<String, String> map = new HashMap<String, String>();
    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();
    try {
        latch.await(5, TimeUnit.SECONDS);
        assertFalse("At least one finished job", finishedEvents.isEmpty());
        assertEquals(1, processedJobsCount.get());
        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"));
    } finally {
        jobManager.removeJobById(jobId);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventHandler(org.osgi.service.event.EventHandler) JobManager(org.apache.sling.event.jobs.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) 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