0

I am using Osgi (Karaf) EventAdmin to broadcast data from a change Data Capture thread to some bundles in my OSGI container using post event and I want to stop the thread once there is no consumers for the topic or channel. I started with a simple example and this is how I braodcast the events:

ServiceTracker eventAdminTracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
 eventAdminTracker.open();
 eventAdmin = (EventAdmin) eventAdminTracker.getService(); 
cdcEvent.put("DataChange",new String(source, offset, length) );
Event event =  new Event(POST_EVENT_QUEUE, cdcEvent);
eventAdmin.postEvent(event);

this the event handler

Dictionary dp = new Hashtable();
dp.put(EventConstants.EVENT_TOPIC, POST_EVENT_QUEUE);
context.registerService(EventHandler.class.getName(), new PostEventHandler(), dp)

....

public class PostEventHandler implements EventHandler{
@Override
public void handleEvent(Event event) {
String value = event.getProperty("DataChange").toString();
System.out.println("value from dataSource : "+value );
}
}

Is there a way to check if there is consumers on a channel or a Queu ?

elpazio
  • 697
  • 2
  • 9
  • 25

1 Answers1

3

You can track the EventHandler services with your topic as a property and react once there is no more handler.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Sorry, can you explain more ? or maybe a short exemple on how to do it ? – elpazio May 03 '17 at 09:31
  • 1
    Create a ServiceTracker that tracks the EventAdmin interface and filters for (event.topics=yourtopic"). You can override the addingService and removedService methods to track when eventhandlers are added or removed. – Christian Schneider May 03 '17 at 09:35
  • Btw. I suggest to not use a ServiceTracker in most production systems. Instead use declarative services. – Christian Schneider May 04 '17 at 07:40