use of org.apache.sling.event.jobs.consumer.JobConsumer 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));
}
}
use of org.apache.sling.event.jobs.consumer.JobConsumer 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());
}
use of org.apache.sling.event.jobs.consumer.JobConsumer in project sling by apache.
the class JobHandlingTest method testNoJobProcessor.
/**
* Test sending of jobs with and without a processor
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testNoJobProcessor() throws Exception {
final AtomicInteger count = new AtomicInteger(0);
this.registerJobConsumer(TOPIC, new JobConsumer() {
@Override
public JobResult process(final Job job) {
count.incrementAndGet();
return JobResult.OK;
}
});
final JobManager jobManager = this.getJobManager();
// we start 20 jobs, every second job has no processor
final int COUNT = 20;
for (int i = 0; i < COUNT; i++) {
final String jobTopic = (i % 2 == 0 ? TOPIC : TOPIC + "2");
jobManager.addJob(jobTopic, null);
}
while (jobManager.getStatistics().getNumberOfFinishedJobs() < COUNT / 2) {
this.sleep(50);
}
assertEquals("Finished count", COUNT / 2, count.get());
// unprocessed count should be 0 as there is no job consumer for this job
assertEquals("Unprocessed count", 0, jobManager.getStatistics().getNumberOfJobs());
assertEquals("Finished count", COUNT / 2, jobManager.getStatistics().getNumberOfFinishedJobs());
}
use of org.apache.sling.event.jobs.consumer.JobConsumer in project sling by apache.
the class JobConsumerManagerTest method testSimpleMappingConsumer.
@Test
public void testSimpleMappingConsumer() {
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/b");
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"));
assertNull(jcs.getExecutor("a/c"));
assertNull(jcs.getExecutor("a/b/a"));
}
use of org.apache.sling.event.jobs.consumer.JobConsumer 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());
}
Aggregations