Search in sources :

Example 1 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class TopicMatchingTest method testDeepMatching.

/**
     * Test deep pattern matching /**
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testDeepMatching() throws Exception {
    final Barrier barrier = new Barrier(2);
    this.registerJobExecutor("sling/**", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            return context.result().succeeded();
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            barrier.block();
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    barrier.block();
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 2 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class TopicMatchingTest method testSimpleMatching.

/**
     * Test simple pattern matching /*
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleMatching() throws Exception {
    final Barrier barrier = new Barrier(2);
    this.registerJobExecutor("sling/test/*", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            return context.result().succeeded();
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            barrier.block();
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    barrier.block();
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 3 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class UnorderedQueueTest method testUnorderedQueue.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testUnorderedQueue() 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 4 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class JobHandlingTest method testStartJobAndReschedule.

/**
     * Reschedule test.
     * The job is rescheduled two times before it fails.
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testStartJobAndReschedule() throws Exception {
    final List<Integer> retryCountList = new ArrayList<Integer>();
    final Barrier cb = new Barrier(2);
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        int retryCount;

        @Override
        public JobResult process(Job job) {
            int retry = 0;
            if (job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT) != null) {
                retry = (Integer) job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
            }
            if (retry == retryCount) {
                retryCountList.add(retry);
            }
            retryCount++;
            cb.block();
            return JobResult.FAILED;
        }
    });
    final JobManager jobManager = this.getJobManager();
    final Job job = jobManager.addJob(TOPIC, null);
    assertTrue("No event received in the given time.", cb.block(5));
    cb.reset();
    // the job is retried after two seconds, so we wait again
    assertTrue("No event received in the given time.", cb.block(5));
    cb.reset();
    // the job is retried after two seconds, so we wait again
    assertTrue("No event received in the given time.", cb.block(5));
    // we have reached the retry so we expect to not get an event
    cb.reset();
    assertFalse("Unexpected event received in the given time.", cb.block(5));
    assertEquals("Unexpected number of retries", 3, retryCountList.size());
    jobManager.removeJobById(job.getId());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) 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 5 with Barrier

use of org.apache.sling.event.impl.Barrier 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)

Aggregations

Barrier (org.apache.sling.event.impl.Barrier)11 Job (org.apache.sling.event.jobs.Job)11 Test (org.junit.Test)11 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7 JobManager (org.apache.sling.event.jobs.JobManager)6 Event (org.osgi.service.event.Event)5 EventHandler (org.osgi.service.event.EventHandler)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 JobExecutionContext (org.apache.sling.event.jobs.consumer.JobExecutionContext)4 JobExecutionResult (org.apache.sling.event.jobs.consumer.JobExecutionResult)4 JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)4 HashMap (java.util.HashMap)3 Queue (org.apache.sling.event.jobs.Queue)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1