Search in sources :

Example 6 with JobExecutor

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

the class JobHandlingTest method testSimpleJobExecutionUsingJobExecutor.

/**
     * Test simple job execution.
     * The job is executed once and finished successfully.
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleJobExecutionUsingJobExecutor() throws Exception {
    final Barrier cb = new Barrier(2);
    this.registerJobExecutor(TOPIC, new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            cb.block();
            return context.result().succeeded();
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    assertTrue("No event received in the given time.", cb.block(5));
    cb.reset();
    assertFalse("Unexpected event received in the given time.", cb.block(5));
}
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) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 7 with JobExecutor

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

the class QueueJobCache method getNextJob.

/**
     * Get the next job.
     * This method is potentially called concurrently, and
     * {@link #reschedule(String, JobHandler, StatisticsManager)} and {@link #handleNewTopics(Set)}
     * can be called concurrently.
     * @param jobConsumerManager The job consumer manager
     * @param statisticsManager The statistics manager
     * @param queue The queue
     * @param doFull Whether to do a full scan
     * @return The job handler or {@code null}.
     */
public JobHandler getNextJob(final JobConsumerManager jobConsumerManager, final StatisticsManager statisticsManager, final Queue queue, final boolean doFull) {
    JobHandler handler = null;
    if (!this.queueIsBlocked.get()) {
        synchronized (this.cache) {
            boolean retry;
            do {
                retry = false;
                if (this.cache.isEmpty()) {
                    final Set<String> checkingTopics = new HashSet<String>();
                    synchronized (this.topicsWithNewJobs) {
                        checkingTopics.addAll(this.topicsWithNewJobs);
                        this.topicsWithNewJobs.clear();
                    }
                    if (doFull) {
                        checkingTopics.addAll(this.topics);
                    }
                    if (!checkingTopics.isEmpty()) {
                        this.loadJobs(queue.getName(), checkingTopics, statisticsManager);
                    }
                }
                if (!this.cache.isEmpty()) {
                    final JobImpl job = this.cache.remove(0);
                    final JobExecutor consumer = jobConsumerManager.getExecutor(job.getTopic());
                    handler = new JobHandler(job, consumer, this.configuration);
                    if (consumer != null) {
                        if (!handler.startProcessing(queue)) {
                            statisticsManager.jobDequeued(queue.getName(), handler.getJob().getTopic());
                            if (logger.isDebugEnabled()) {
                                logger.debug("Discarding removed job {}", Utility.toString(job));
                            }
                            handler = null;
                            retry = true;
                        }
                    } else {
                        statisticsManager.jobDequeued(queue.getName(), handler.getJob().getTopic());
                        // no consumer on this instance, assign to another instance
                        handler.reassign();
                        handler = null;
                        retry = true;
                    }
                }
            } while (handler == null && retry);
        }
    }
    return handler;
}
Also used : JobHandler(org.apache.sling.event.impl.jobs.JobHandler) JobImpl(org.apache.sling.event.impl.jobs.JobImpl) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) HashSet(java.util.HashSet)

Example 8 with JobExecutor

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

the class JobConsumerManagerTest method testSimpleMappingExecutor.

@Test
public void testSimpleMappingExecutor() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobExecutor jc1 = Mockito.mock(JobExecutor.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.bindJobExecutor(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 9 with JobExecutor

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

the class JobConsumerManagerTest method testRanking.

@Test
public void testRanking() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobExecutor jc1 = Mockito.mock(JobExecutor.class);
    final JobExecutor jc2 = Mockito.mock(JobExecutor.class);
    final JobExecutor jc3 = Mockito.mock(JobExecutor.class);
    final JobExecutor jc4 = Mockito.mock(JobExecutor.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobExecutor.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.bindJobExecutor(ref1);
    assertEquals(jc1, jcs.getExecutor("a/b"));
    final ServiceReference ref2 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref2.getProperty(JobExecutor.PROPERTY_TOPICS)).thenReturn("a/b");
    Mockito.when(ref2.getProperty(Constants.SERVICE_RANKING)).thenReturn(10);
    Mockito.when(ref2.getProperty(Constants.SERVICE_ID)).thenReturn(2L);
    Mockito.when(bc.getService(ref2)).thenReturn(jc2);
    jcs.bindJobExecutor(ref2);
    assertEquals(jc2, jcs.getExecutor("a/b"));
    final ServiceReference ref3 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref3.getProperty(JobExecutor.PROPERTY_TOPICS)).thenReturn("a/b");
    Mockito.when(ref3.getProperty(Constants.SERVICE_RANKING)).thenReturn(5);
    Mockito.when(ref3.getProperty(Constants.SERVICE_ID)).thenReturn(3L);
    Mockito.when(bc.getService(ref3)).thenReturn(jc3);
    jcs.bindJobExecutor(ref3);
    assertEquals(jc2, jcs.getExecutor("a/b"));
    final ServiceReference ref4 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref4.getProperty(JobExecutor.PROPERTY_TOPICS)).thenReturn("a/b");
    Mockito.when(ref4.getProperty(Constants.SERVICE_RANKING)).thenReturn(5);
    Mockito.when(ref4.getProperty(Constants.SERVICE_ID)).thenReturn(4L);
    Mockito.when(bc.getService(ref4)).thenReturn(jc4);
    jcs.bindJobExecutor(ref4);
    assertEquals(jc2, jcs.getExecutor("a/b"));
    jcs.unbindJobExecutor(ref2);
    assertEquals(jc3, jcs.getExecutor("a/b"));
    jcs.unbindJobExecutor(ref3);
    assertEquals(jc4, jcs.getExecutor("a/b"));
}
Also used : JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 10 with JobExecutor

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

the class HistoryTest method testHistory.

/**
     * Test history.
     * Start 10 jobs and cancel some of them and succeed others
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testHistory() throws Exception {
    this.registerJobExecutor(TOPIC, new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            sleep(5L);
            final long count = job.getProperty(PROP_COUNTER, Long.class);
            if (count == 2 || count == 5 || count == 7) {
                return context.result().message(Job.JobState.ERROR.name()).cancelled();
            }
            return context.result().message(Job.JobState.SUCCEEDED.name()).succeeded();
        }
    });
    for (int i = 0; i < 10; i++) {
        this.addJob(i);
    }
    this.sleep(200L);
    while (this.getJobManager().findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, (Map<String, Object>[]) null).size() < 10) {
        this.sleep(20L);
    }
    Collection<Job> col = this.getJobManager().findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, (Map<String, Object>[]) null);
    assertEquals(10, col.size());
    assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.ACTIVE, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.QUEUED, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(3, this.getJobManager().findJobs(JobManager.QueryType.CANCELLED, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.DROPPED, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(3, this.getJobManager().findJobs(JobManager.QueryType.ERROR, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.GIVEN_UP, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.STOPPED, TOPIC, -1, (Map<String, Object>[]) null).size());
    assertEquals(7, this.getJobManager().findJobs(JobManager.QueryType.SUCCEEDED, TOPIC, -1, (Map<String, Object>[]) null).size());
    // find all topics
    assertEquals(7, this.getJobManager().findJobs(JobManager.QueryType.SUCCEEDED, null, -1, (Map<String, Object>[]) null).size());
    // verify order, message and state
    long last = 9;
    for (final Job j : col) {
        assertNotNull(j.getFinishedDate());
        final long count = j.getProperty(PROP_COUNTER, Long.class);
        assertEquals(last, count);
        if (count == 2 || count == 5 || count == 7) {
            assertEquals(Job.JobState.ERROR, j.getJobState());
        } else {
            assertEquals(Job.JobState.SUCCEEDED, j.getJobState());
        }
        assertEquals(j.getJobState().name(), j.getResultMessage());
        last--;
    }
}
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) Job(org.apache.sling.event.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)10 Test (org.junit.Test)9 Job (org.apache.sling.event.jobs.Job)5 JobExecutionContext (org.apache.sling.event.jobs.consumer.JobExecutionContext)5 JobExecutionResult (org.apache.sling.event.jobs.consumer.JobExecutionResult)5 Barrier (org.apache.sling.event.impl.Barrier)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 Event (org.osgi.service.event.Event)2 EventHandler (org.osgi.service.event.EventHandler)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 JobHandler (org.apache.sling.event.impl.jobs.JobHandler)1 JobImpl (org.apache.sling.event.impl.jobs.JobImpl)1