Asier,

Do you have an event admin bundle installed and active? The Equinox
implementation is called org.eclipse.equinox.event.

Without that bundle active you will of course always get null when
trying to obtain the EventAdmin service.

Regards,
Neil

On Mon, Nov 9, 2009 at 12:01 PM, Asier Aranbarri Beldarrain
<[email protected]> wrote:
> Hello everyone!
> I am having troubles with OSGi, using Eclipse's Equinox. My problem is that
> I want to create two bundles, one to POST(asynch.) events, and another one
> to LISTEN to them. My most frecuent error is that EventAdmin is null and
> cannot post any event, but I really do not know what is happening. I post
> you my codes for the Event Poster and Event Listener, which I took from
> here:
> http://kenai.com/projects/posteventvssendevent/sources/subversion/show. The
> author of it is Slim Ouertani:
>
> _____________________________________________________________________________________________________________
>
>
> Event Poster [CODE]package posteventos; import java.util.logging.Level;
> import java.util.logging.Logger; import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext; import java.util.Date; import
> java.util.Dictionary; import java.util.Properties; import
> org.osgi.service.event.Event; import org.osgi.service.event.EventAdmin;
> import org.osgi.util.tracker.ServiceTracker; public class Activator
> implements BundleActivator { private EventAdmin eventAdmin; private
> ServiceTracker eventAdminTracker; private static final String
> POST_EVENT_QUEUE = "An Event"; private void postEvent() { Dictionary props =
> new Properties(); int i = 0; while (true) { Date d = new Date();
> props.put("property", "" + i++ + " : " + d); System.out.println("before post
> : " + d); Event event = new Event(POST_EVENT_QUEUE, props);
> eventAdmin.postEvent(event); System.out.println("after post : " + d); try {
> Thread.sleep(10 * 1000); } catch (InterruptedException ex) {
> Logger.getLogger(Activator.class.getName()).log(Level.SEVERE, null, ex); } }
> } @Override public void start(BundleContext bundleContext) throws Exception
> { eventAdminTracker = new ServiceTracker(bundleContext,
> EventAdmin.class.getName(), null); eventAdminTracker.open(); eventAdmin =
> (EventAdmin) eventAdminTracker.getService(); if (eventAdmin != null) { new
> Thread() { @Override public void run() { postEvent(); } }.start(); } else {
> System.out.println("eventAdmin is null"); //this one to see if it's NULL } }
> @Override public void stop(BundleContext bundleContext) throws Exception {
> eventAdminTracker.close(); } } [/CODE]
> _____________________________________________________________________________________________________________
> Event Listener [CODE]package listenereventos; import java.util.Dictionary;
> import java.util.Hashtable; import java.util.Properties; import
> java.util.logging.Level; import java.util.logging.Logger; import
> org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext;
> import org.osgi.service.event.Event; import
> org.osgi.service.event.EventConstants; import
> org.osgi.service.event.EventHandler; /** * Hello world! * */ public class
> Activator implements BundleActivator { private static final String
> POST_EVENT_QUEUE = "Hola"; @Override public void start(BundleContext
> context) throws Exception { Dictionary dp = new Hashtable();
> dp.put(EventConstants.EVENT_TOPIC, POST_EVENT_QUEUE); for (int i = 0; i <
> 50000; i++) { context.registerService(EventHandler.class.getName(), new
> [B]PostEventHandler[/B](), dp); //code for this behind } } @Override public
> void stop(BundleContext context) throws Exception { } } [/CODE]
> ____________________________________ PostEventHandler: [CODE]package
> listenereventos; import java.util.logging.Level; import
> java.util.logging.Logger; import org.osgi.service.event.Event; import
> org.osgi.service.event.EventHandler; public class PostEventHandler { public
> void handleEvent(Event event) { String value =
> event.getProperty("property").toString(); try { Thread.sleep(30 * 1000); }
> catch (InterruptedException ex) {
> Logger.getLogger(Activator.class.getName()).log(Level.SEVERE, null, ex); }
> System.out.println("---------------->Post value : " + value); } } [/CODE] I
> really don't know what's happening since it's my first time using OSGi
> events, and I would really thank any help from you. Regards, Asier[/QUOTE]
>
> _____________________________________________________________________________________________________________
>
>
> I noticed that the EventTracker size is 0 :
> System.out.println(eventAdminTracker.size())
>
> Could it be the problem? Thank you very much,
>
> Asier
>
>
> _______________________________________________
> OSGi Developer Mail List
> [email protected]
> https://mail.osgi.org/mailman/listinfo/osgi-dev
>
_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to