Search in sources :

Example 11 with JobConsumer

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

the class TimedJobsTest method testTimedJob.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testTimedJob() 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;
        }
    });
    final Date d = new Date();
    // run in 3 seconds
    d.setTime(System.currentTimeMillis() + 3000);
    // create scheduled job
    final ScheduledJobInfo info = this.getJobManager().createJob(TOPIC).schedule().at(d).add();
    assertNotNull(info);
    while (counter.get() == 0) {
        this.sleep(1000);
    }
    // job is not scheduled anymore
    assertEquals(0, this.getJobManager().getScheduledJobs().size());
    info.unschedule();
}
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) Date(java.util.Date) Test(org.junit.Test)

Example 12 with JobConsumer

use of org.apache.sling.event.jobs.consumer.JobConsumer 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 13 with JobConsumer

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

the class JobHandlingTest method testManyJobs.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testManyJobs() throws Exception {
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            return JobResult.OK;
        }
    });
    final AtomicInteger count = new AtomicInteger(0);
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            count.incrementAndGet();
        }
    });
    // we start "some" jobs
    final int COUNT = 300;
    for (int i = 0; i < COUNT; i++) {
        this.getJobManager().addJob(TOPIC, null);
    }
    while (count.get() < COUNT) {
        this.sleep(50);
    }
    assertEquals("Finished count", COUNT, count.get());
    assertEquals("Finished count", COUNT, this.getJobManager().getStatistics().getNumberOfFinishedJobs());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventHandler(org.osgi.service.event.EventHandler) 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)

Example 14 with JobConsumer

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

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

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