Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » need EventBroker as OSGi Service
need EventBroker as OSGi Service [message #904005] Mon, 27 August 2012 14:47 Go to next message
Thomas Kölling is currently offline Thomas Kölling
Messages: 45
Registered: December 2011
Location: Munich
Member
Hi all,

is there a possibility to get the EventBroker via the osgi registry? The problem is, that we have a spring dm bundle for some core functionality in a Eclipse e4 Application (which uses httpinvoker and stuff to connect to a service). From there i need to get access to the EventBroker, to notify some activities to the other bundles.

I've seen that the e4.ui.services bundle registred a Context Function with a IEventBroker property. How i can get access to this? Currently i'v tried that with Spring DM

<osgi:reference id="eventBroker" interface="org.eclipse.e4.core.contexts.IContextFunction" filter="(service.context.key=org.eclipse.e4.core.services.events.IEventBroker)"/>


But with that i have only the IContextFunction Interface, which doesnt provide a way to access the eventbroker Confused

is there an other way?

greetings, thomas
Re: need EventBroker as OSGi Service [message #904049 is a reply to message #904005] Mon, 27 August 2012 16:14 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5419
Registered: July 2009
Senior Member
EventBroker lives above the OSGi-Level to allow us to have multiple ones
(e.g. for RAP and Vaadin this is essential).

What I tell now is an implementation detail: The eventbroker simply
reuses the EventAdmin-Service from OSGi which is a singleton you can get
access and send out events.

So you can simply send out events using the EventAdmin-Service directly
your UI-code will see the events (if you send them out with the
appropriate information - see the EventBroker implementation of e4 for
that).

Tom

Am 27.08.12 16:47, schrieb Thomas Kölling:
> Hi all,
>
> is there a possibility to get the EventBroker via the osgi registry? The
> problem is, that we have a spring dm bundle for some core functionality
> in a Eclipse e4 Application (which uses httpinvoker and stuff to connect
> to a service). From there i need to get access to the EventBroker, to
> notify some activities to the other bundles.
> I've seen that the e4.ui.services bundle registred a Context Function
> with a IEventBroker property. How i can get access to this? Currently
> i'v tried that with Spring DM
>
> <osgi:reference id="eventBroker"
> interface="org.eclipse.e4.core.contexts.IContextFunction"
> filter="(service.context.key=org.eclipse.e4.core.services.events.IEventBroker)"/>
>
>
> But with that i have only the IContextFunction Interface, which doesnt
> provide a way to access the eventbroker :?
> is there an other way?
>
> greetings, thomas
Re: need EventBroker as OSGi Service [message #904089 is a reply to message #904049] Mon, 27 August 2012 18:23 Go to previous messageGo to next message
Thomas Kölling is currently offline Thomas Kölling
Messages: 45
Registered: December 2011
Location: Munich
Member
Wow thats cool, I didn't know that. Seems to be useful for our serverside equinox too. big thanks!
Re: need EventBroker as OSGi Service [message #904115 is a reply to message #904089] Mon, 27 August 2012 19:50 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

For reference this

public boolean post(String topic, Object data) {
		Event event = constructEvent(topic, data);
		EventAdmin eventAdmin = Activator.getDefault().getEventAdmin();
		if (eventAdmin == null) {
			logger.error(NLS.bind(ServiceMessages.NO_EVENT_ADMIN, event.toString()));
			return false;
		}
		eventAdmin.postEvent(event);
		return true;
	}

	@SuppressWarnings("unchecked")
	private Event constructEvent(String topic, Object data) {
		Event event;
		if (data instanceof Dictionary<?,?>) {
			event = new Event(topic, (Dictionary<String,?>)data);
		} else if (data instanceof Map<?,?>) {
			event = new Event(topic, (Map<String,?>)data);
		} else {
			Dictionary<String, Object> d = new Hashtable<String, Object>(2);
			d.put(EventConstants.EVENT_TOPIC, topic);
			if (data != null)
				d.put(IEventBroker.DATA, data);
			event = new Event(topic, d);
		}
		return event;
	}


is the EB post method and you can see how the e4 event is wrapped in an EventAdmin event and posted.
Re: need EventBroker as OSGi Service [message #904549 is a reply to message #904089] Tue, 28 August 2012 16:50 Go to previous messageGo to next message
Fabio Mancinelli is currently offline Fabio Mancinelli
Messages: 8
Registered: August 2012
Junior Member
On 08/27/2012 08:23 PM, Thomas Kölling wrote:
> Wow thats cool, I didn't know that. Seems to be useful for our
> serverside equinox too. big thanks!

If you still want to use IEventBroker as a service in pure OSGi you can
do the following:

1) Create a declarative service that provides
org.eclipse.e4.core.services.log.Logger (this is needed by the
implementation EventBroker which requires a Logger service)

2) In your activator (or wherever you want), create a field

@Inject
IEventBroker broker

3) In the start method of your activator (or in the constructor of your
class) do the following:

IEclipseContext serviceContext =
EclipseContextFactory.getServiceContext(bundleContext);

ContextInjectionFactory.inject(this, serviceContext);

If you don't have the bundleContext available you can always retrieve it
using FrameworkUtil.getBundle(AClassInYourBundle.class).getBundleContext();

And you will have the E4 event broker injected in your instance.

-Fabio
Re: need EventBroker as OSGi Service [message #904572 is a reply to message #904549] Tue, 28 August 2012 17:50 Go to previous message
Thomas Kölling is currently offline Thomas Kölling
Messages: 45
Registered: December 2011
Location: Munich
Member
Hi Fabio,

i've done something similar in the Bundle Activator:

IEclipseContext eclipseCtx =
                EclipseContextFactory.getServiceContext(context);
eclipseCtx.set(Logger.class, null);
IEventBroker eventBroker = eclipseCtx.get(IEventBroker.class);
registration = context.registerService(IEventBroker.class, eventBroker, null);


Now i can inject the EventBroker via Spring DM in any combatible Module. Works like a charm Smile Maybe I convert this code to a pure spring bean, if possible.

greeting thomas

Previous Topic:Uninstalled Model Addon Show Error continuously.
Next Topic:Focusing windows
Goto Forum:
  


Current Time: Thu Oct 23 17:25:59 GMT 2014

Powered by FUDForum. Page generated in 0.02501 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software