Skip to main content


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öllingFriend
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 SchindlFriend
Messages: 6651
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öllingFriend
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
Eclipse UserFriend
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 MancinelliFriend
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 messageGo to next message
Thomas Kölling is currently offline Thomas KöllingFriend
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

Re: need EventBroker as OSGi Service [message #1691088 is a reply to message #904549] Thu, 02 April 2015 08:55 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 114
Registered: January 2015
Senior Member

Hi Fabio,
very useful post.

However, I do not get this:

Quote:
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)


Could you clarify this a bit..?

Thank you very much.
In my case, setting an equinox Logger to the Eclipse context won't work either:

eclipseCtx.set(Logger.class, null);


I can't get rid of the Unable to process "EventBroker.logger" error. Sad
Re: need EventBroker as OSGi Service [message #1691109 is a reply to message #1691088] Thu, 02 April 2015 11:16 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 114
Registered: January 2015
Senior Member

I tried by using the ServiceRegistration<?>, like proposed in [1], but still the Logger is not resolved for the broker, same as before.

[1] https://www.eclipse.org/forums/index.php?t=msg&th=389240&goto=956075&#msg_956075
Re: need EventBroker as OSGi Service [message #1694290 is a reply to message #1691109] Mon, 04 May 2015 08:18 Go to previous message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 114
Registered: January 2015
Senior Member

PS The problem is I was setting a org.eclipse.equinox.log.Logger instead of a org.eclipse.e4.core.services.log.Logger.
(The former gives no "non-API" warning (and no compile-time error), whereas the latter does)
Previous Topic:Closed view displayed again after restarting application
Next Topic:InjectionException while obtaining IEventBroker from IEclipseContext
Goto Forum:
  


Current Time: Thu Apr 25 11:08:58 GMT 2024

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

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

Back to the top