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();
}
}
}
}
}
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();
}
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();
}
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();
}
}
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);
}
Aggregations