Search in sources :

Example 1 with JobExecutionContext

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

the class JobConsumerManager method unbindService.

/**
     * Unbind a consumer or executor
     * @param serviceReference The service reference to the consumer or executor.
     * @param isConsumer Indicating whether this is a JobConsumer or JobExecutor
     */
private void unbindService(final ServiceReference<?> serviceReference, final boolean isConsumer) {
    final String[] topics = PropertiesUtil.toStringArray(serviceReference.getProperty(JobConsumer.PROPERTY_TOPICS));
    if (topics != null && topics.length > 0) {
        final ConsumerInfo info = new ConsumerInfo(serviceReference, isConsumer);
        boolean changed = false;
        synchronized (this.topicToConsumerMap) {
            for (final String t : topics) {
                if (t != null) {
                    final String topic = t.trim();
                    if (topic.length() > 0) {
                        final List<ConsumerInfo> consumers = this.topicToConsumerMap.get(topic);
                        if (consumers != null) {
                            // sanity check
                            for (final ConsumerInfo oldConsumer : consumers) {
                                if (oldConsumer.equals(info) && oldConsumer.executor != null) {
                                    // notify listener
                                    for (final Object[] listenerObjects : this.listenerMap.values()) {
                                        if (listenerObjects[0] == oldConsumer.executor) {
                                            final JobExecutionContext context = (JobExecutionContext) listenerObjects[1];
                                            context.asyncProcessingFinished(context.result().failed());
                                            break;
                                        }
                                    }
                                }
                            }
                            consumers.remove(info);
                            if (consumers.size() == 0) {
                                this.topicToConsumerMap.remove(topic);
                                changed = true;
                            }
                        }
                    }
                }
            }
            if (changed) {
                this.calculateTopics(this.propagationService != null);
            }
        }
        if (changed && this.propagationService != null) {
            logger.debug("Updating property provider with: {}", this.topics);
            this.propagationService.setProperties(this.getRegistrationProperties());
        }
    }
}
Also used : JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext)

Example 2 with JobExecutionContext

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

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

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

the class TopicMatchingTest method testOrdering.

/**
     * Test ordering of matchers
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testOrdering() throws Exception {
    final Barrier barrier1 = new Barrier(2);
    final Barrier barrier2 = new Barrier(2);
    final Barrier barrier3 = new Barrier(2);
    this.registerJobExecutor("sling/**", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            barrier1.block();
            return context.result().succeeded();
        }
    });
    final ServiceRegistration<JobExecutor> reg2 = this.registerJobExecutor("sling/test/*", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            barrier2.block();
            return context.result().succeeded();
        }
    });
    final ServiceRegistration<JobExecutor> reg3 = this.registerJobExecutor(TOPIC, new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            barrier3.block();
            return context.result().succeeded();
        }
    });
    // first test, all three registered, reg3 should get the precedence
    this.getJobManager().addJob(TOPIC, null);
    barrier3.block();
    // second test, unregister reg3, now it should be reg2
    long cc = this.getConsumerChangeCount();
    this.unregister(reg3);
    this.waitConsumerChangeCount(cc + 1);
    this.getJobManager().addJob(TOPIC, null);
    barrier2.block();
    // third test, unregister reg2, reg1 is now the only one
    cc = this.getConsumerChangeCount();
    this.unregister(reg2);
    this.waitConsumerChangeCount(cc + 1);
    this.getJobManager().addJob(TOPIC, null);
    barrier1.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) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 5 with JobExecutionContext

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

Aggregations

JobExecutionContext (org.apache.sling.event.jobs.consumer.JobExecutionContext)6 Job (org.apache.sling.event.jobs.Job)5 JobExecutionResult (org.apache.sling.event.jobs.consumer.JobExecutionResult)5 JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)5 Test (org.junit.Test)5 Barrier (org.apache.sling.event.impl.Barrier)4 Event (org.osgi.service.event.Event)2 EventHandler (org.osgi.service.event.EventHandler)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1