Search in sources :

Example 86 with Event

use of org.osgi.service.event.Event in project sling by apache.

the class DistributedEventSender method runInBackground.

/**
     * Background thread
     */
private void runInBackground() {
    while (this.running) {
        // so let's wait/get the next event from the queue
        String path = null;
        try {
            path = this.queue.take();
        } catch (final InterruptedException e) {
            this.ignoreException(e);
            Thread.currentThread().interrupt();
            this.running = false;
        }
        if (path != null && path.length() > 0 && this.running) {
            ResourceResolver resolver = null;
            try {
                resolver = this.resourceResolverFactory.getServiceResourceResolver(null);
                final Resource eventResource = resolver.getResource(path);
                if (eventResource == null) {
                    this.logger.warn("runInBackground : resource not found at " + path);
                } else if (DistributedEventAdminImpl.RESOURCE_TYPE_EVENT.equals(eventResource.getResourceType())) {
                    final Event e = this.readEvent(eventResource);
                    if (e != null) {
                        // we check event admin as processing is async
                        final EventAdmin localEA = this.eventAdmin;
                        if (localEA != null) {
                            localEA.postEvent(e);
                        } else {
                            this.logger.error("Unable to post event as no event admin is available.");
                        }
                    }
                }
            } catch (final LoginException ex) {
                this.logger.error("Exception during creation of resource resolver.", ex);
            } finally {
                if (resolver != null) {
                    resolver.close();
                }
            }
        }
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Event(org.osgi.service.event.Event) LoginException(org.apache.sling.api.resource.LoginException)

Example 87 with Event

use of org.osgi.service.event.Event in project sling by apache.

the class DistributingEventHandlerTest method testSendEvent.

@org.junit.Test(timeout = 5000)
public void testSendEvent() throws Exception {
    this.events.clear();
    final String VALUE = "some value";
    final String topic = TOPIC_PREFIX + "event/test";
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("a property", VALUE);
    final Event e = new Event(topic, props);
    this.receiver.handleEvent(e);
    while (this.events.size() == 0) {
        Thread.sleep(5);
    }
    final Event receivedEvent = this.events.get(0);
    assertEquals(topic, receivedEvent.getTopic());
    assertEquals(OTHER_APP_ID, receivedEvent.getProperty(DEAConstants.PROPERTY_APPLICATION));
    assertEquals(VALUE, receivedEvent.getProperty("a property"));
    assertNull(receivedEvent.getProperty(ResourceResolver.PROPERTY_RESOURCE_TYPE));
    this.events.clear();
}
Also used : Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 88 with Event

use of org.osgi.service.event.Event in project sling by apache.

the class DistributingEventHandlerTest method testSendEventWithResourceType.

@org.junit.Test(timeout = 5000)
public void testSendEventWithResourceType() throws Exception {
    this.events.clear();
    final String topic = TOPIC_PREFIX + "event/test";
    final String RT = "my:resourceType";
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, RT);
    final Event e = new Event(topic, props);
    this.receiver.handleEvent(e);
    while (this.events.size() == 0) {
        Thread.sleep(5);
    }
    final Event receivedEvent = this.events.get(0);
    assertEquals(topic, receivedEvent.getTopic());
    assertEquals(OTHER_APP_ID, receivedEvent.getProperty(DEAConstants.PROPERTY_APPLICATION));
    assertEquals(RT, receivedEvent.getProperty(ResourceResolver.PROPERTY_RESOURCE_TYPE));
    assertNull(receivedEvent.getProperty("event.dea." + ResourceResolver.PROPERTY_RESOURCE_TYPE));
    this.events.clear();
}
Also used : Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 89 with Event

use of org.osgi.service.event.Event in project sling by apache.

the class DistributedEventReceiver method stop.

/**
     * Deactivate this component.
     */
public void stop() {
    if (this.serviceRegistration != null) {
        this.serviceRegistration.unregister();
        this.serviceRegistration = null;
    }
    // stop background threads by putting empty objects into the queue
    this.running = false;
    try {
        this.writeQueue.put(new Event(TOPIC_STOPPED, (Dictionary<String, Object>) null));
    } catch (final InterruptedException e) {
        this.ignoreException(e);
        Thread.currentThread().interrupt();
    }
}
Also used : Dictionary(java.util.Dictionary) TopologyEvent(org.apache.sling.discovery.TopologyEvent) Event(org.osgi.service.event.Event)

Example 90 with Event

use of org.osgi.service.event.Event in project ddf by codice.

the class EventProcessorImpl method postEvent.

/**
     * Posts a Metacard to a given topic
     *
     * @param topic - The topic to post the event
     * @param card  - The Metacard that will be posted to the topic
     */
protected void postEvent(String topic, Metacard card, Metacard oldCard) {
    String methodName = "postEvent";
    LOGGER.debug("ENTERING: {}", methodName);
    LOGGER.debug("Posting to topic: {}", topic);
    Dictionary<String, Object> properties = new Hashtable<>(2, 1);
    properties.put(EventProcessor.EVENT_METACARD, card);
    properties.put(EventProcessor.EVENT_TIME, System.currentTimeMillis());
    Event event = new Event(topic, properties);
    eventAdmin.postEvent(event);
    LOGGER.debug("EXITING: {}", methodName);
}
Also used : Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Aggregations

Event (org.osgi.service.event.Event)142 Test (org.junit.Test)79 HashMap (java.util.HashMap)48 Hashtable (java.util.Hashtable)44 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)23 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)20 EventAdmin (org.osgi.service.event.EventAdmin)19 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)18 Predicate (ddf.catalog.pubsub.predicate.Predicate)16 ArrayList (java.util.ArrayList)16 ActivityEvent (org.codice.ddf.activities.ActivityEvent)13 EventHandler (org.osgi.service.event.EventHandler)13 SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)11 Date (java.util.Date)11 Job (org.apache.sling.event.jobs.Job)9 Map (java.util.Map)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 JobManager (org.apache.sling.event.jobs.JobManager)7 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7