use of org.osgi.service.event.EventAdmin 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.EventAdmin in project ddf by codice.
the class SendCommand method sendNotification.
private void sendNotification() throws Exception {
Long sysTimeMillis = System.currentTimeMillis();
String id = UUID.randomUUID().toString().replaceAll("-", "");
String sessionId = "mockSessionId";
Notification notification = new Notification(id, sessionId, application, title, message, sysTimeMillis, userId);
notification.put("status", "Started");
notification.put("bytes", "12345");
Event event = new Event(Notification.NOTIFICATION_TOPIC_DOWNLOADS, notification);
// Get OSGi Event Admin service
EventAdmin eventAdmin;
@SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
if (serviceReferences == null || serviceReferences.length != 1) {
LOGGER.debug("Found no service references for {}", SERVICE_PID);
} else {
LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
eventAdmin = (EventAdmin) bundleContext.getService(serviceReferences[0]);
if (eventAdmin != null) {
eventAdmin.postEvent(event);
}
}
}
use of org.osgi.service.event.EventAdmin in project ddf by codice.
the class SendCommand method sendActivity.
private void sendActivity() throws Exception {
String id = UUID.randomUUID().toString().replaceAll("-", "");
String sessionId = "mockSessionId";
Map<String, String> operations = new HashMap<>();
operations.put("cancel", "true");
ActivityEvent eventProperties = new ActivityEvent(id, sessionId, new Date(), "Activity category", "Activity title", "Activity message", UNKNOWN_PROGRESS, operations, userId, ActivityStatus.RUNNING, 100L);
Event event = new Event(ActivityEvent.EVENT_TOPIC, eventProperties);
// Get OSGi Event Admin service
EventAdmin eventAdmin;
@SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
if (serviceReferences == null || serviceReferences.length != 1) {
LOGGER.debug("Found no service references for {}", SERVICE_PID);
} else {
LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
eventAdmin = (EventAdmin) bundleContext.getService(serviceReferences[0]);
if (eventAdmin != null) {
eventAdmin.postEvent(event);
}
}
}
use of org.osgi.service.event.EventAdmin in project sling by apache.
the class MockEventAdminTest method testSendEvent_Other3.
@Test
public void testSendEvent_Other3() {
EventAdmin eventAdmin = context.getService(EventAdmin.class);
eventAdmin.sendEvent(EVENT_OTHER_3);
assertEquals(ImmutableList.of(), eventHandler1.getReceivedEvents());
assertEquals(ImmutableList.of(), eventHandler12.getReceivedEvents());
assertEquals(ImmutableList.of(), eventHandlerSampleAll.getReceivedEvents());
assertEquals(ImmutableList.of(EVENT_OTHER_3), eventHandlerAll.getReceivedEvents());
}
use of org.osgi.service.event.EventAdmin in project sling by apache.
the class MockEventAdminTest method testSendEvent_Sample2.
@Test
public void testSendEvent_Sample2() {
EventAdmin eventAdmin = context.getService(EventAdmin.class);
eventAdmin.sendEvent(EVENT_SAMPLE_2);
assertEquals(ImmutableList.of(), eventHandler1.getReceivedEvents());
assertEquals(ImmutableList.of(EVENT_SAMPLE_2), eventHandler12.getReceivedEvents());
assertEquals(ImmutableList.of(EVENT_SAMPLE_2), eventHandlerSampleAll.getReceivedEvents());
assertEquals(ImmutableList.of(EVENT_SAMPLE_2), eventHandlerAll.getReceivedEvents());
}
Aggregations