Im new to OSGi and I tried to create a simple EventPublisher/-Admin application using DS to ensure that the EventAdmin is not null. But I'm not sure how to use the DS in the right way.
The Activator class:
package publishertest;
import java.util.HashMap;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
public class Activator implements BundleActivator {
private static BundleContext context;
@Reference
EventAdmin eventAdmin;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Event event = new Event("test", new HashMap<String, Object>());
eventAdmin.postEvent(event);
System.out.println("event posted");
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
The EventHandler class:
package publishertest;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
@Component(
property = {
"event.topics=org/osgi/framework/BundleEvent/STARTED,test"
}
)
public class ServiceComponent implements EventHandler {
public void handleEvent(Event event) {
System.out.println(event.getTopic());
}
}
Adding the @Reference annotation leads to a BundleException. Can somebody help? thanks :)