use of org.apache.sling.event.jobs.JobManager 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.JobManager in project sling by apache.
the class AbstractJobHandlingTest method getJobManager.
protected JobManager getJobManager() {
JobManager result = null;
int count = 0;
do {
final ServiceReference<JobManager> sr = this.bc.getServiceReference(JobManager.class);
if (sr != null) {
result = this.bc.getService(sr);
} else {
count++;
if (count == 10) {
break;
}
sleep(500);
}
} while (result == null);
return result;
}
use of org.apache.sling.event.jobs.JobManager in project sling by apache.
the class ChaosTest method testDoChaos.
@Test(timeout = DURATION * 16000L)
public void testDoChaos() throws Exception {
final JobManager jobManager = this.getJobManager();
// setup added, created and finished map
// added and finished are filled by notifications
// created is filled by the threads starting jobs
final Map<String, AtomicLong> added = new HashMap<>();
final Map<String, AtomicLong> created = new HashMap<>();
final Map<String, AtomicLong> finished = new HashMap<>();
final List<String> topics = new ArrayList<>();
for (int i = 0; i < NUM_ORDERED_TOPICS; i++) {
added.put(ORDERED_TOPICS[i], new AtomicLong());
created.put(ORDERED_TOPICS[i], new AtomicLong());
finished.put(ORDERED_TOPICS[i], new AtomicLong());
topics.add(ORDERED_TOPICS[i]);
}
for (int i = 0; i < NUM_PARALLEL_TOPICS; i++) {
added.put(PARALLEL_TOPICS[i], new AtomicLong());
created.put(PARALLEL_TOPICS[i], new AtomicLong());
finished.put(PARALLEL_TOPICS[i], new AtomicLong());
topics.add(PARALLEL_TOPICS[i]);
}
for (int i = 0; i < NUM_ROUND_TOPICS; i++) {
added.put(ROUND_TOPICS[i], new AtomicLong());
created.put(ROUND_TOPICS[i], new AtomicLong());
finished.put(ROUND_TOPICS[i], new AtomicLong());
topics.add(ROUND_TOPICS[i]);
}
final List<Thread> threads = new ArrayList<>();
final AtomicLong finishedThreads = new AtomicLong();
this.registerEventHandler("org/apache/sling/event/notification/job/*", new EventHandler() {
@Override
public void handleEvent(final Event event) {
final String topic = (String) event.getProperty(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC);
if (NotificationConstants.TOPIC_JOB_FINISHED.equals(event.getTopic())) {
finished.get(topic).incrementAndGet();
} else if (NotificationConstants.TOPIC_JOB_ADDED.equals(event.getTopic())) {
added.get(topic).incrementAndGet();
}
}
});
// setup job consumers
this.setupJobConsumers();
// setup job creation tests
this.setupJobCreationThreads(threads, jobManager, created, finishedThreads);
this.setupChaosThreads(threads, finishedThreads);
System.out.println("Starting threads...");
// start threads
for (final Thread t : threads) {
t.setDaemon(true);
t.start();
}
System.out.println("Sleeping for " + DURATION + " seconds to wait for threads to finish...");
// for sure we can sleep for the duration
this.sleep(DURATION * 1000);
System.out.println("Polling for threads to finish...");
// wait until threads are finished
while (finishedThreads.get() < threads.size()) {
this.sleep(100);
}
System.out.println("Waiting for job handling to finish...");
final Set<String> allTopics = new HashSet<>(topics);
while (!allTopics.isEmpty()) {
final Iterator<String> iter = allTopics.iterator();
while (iter.hasNext()) {
final String topic = iter.next();
if (finished.get(topic).get() == created.get(topic).get()) {
iter.remove();
}
}
this.sleep(100);
}
/* We could try to enable this with Oak again - but right now JR observation handler is too
* slow.
System.out.println("Checking notifications...");
for(final String topic : topics) {
assertEquals("Checking topic " + topic, created.get(topic).get(), added.get(topic).get());
}
*/
}
use of org.apache.sling.event.jobs.JobManager 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.JobManager 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());
}
Aggregations