Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » No events received when I listen outside the class who sends
No events received when I listen outside the class who sends [message #1619227] Mon, 16 February 2015 12:27 Go to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 111
Registered: January 2015
Senior Member

Dear experts,

I am sending my event through the bus but event listeners are seemingly not waken up by it. Only those listeners within the same class/file where the event is sent, react to it.

It seems to me that only classes directly linked to the application model via classURI are connected to the event bus... I also tried also with Eclipse events like UIEvents.UILifeCycle.APP_STARTUP_COMPLETE and still only the UI handlers receives the signal.

Is this an expected behaviour, and in this case: how would I be able to include all my classes in the happy yellow Event submarine system?

For instance:
public class WhateverWherever {
	@Inject
	@Optional
	private void postAppComplete(
			@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) Event event) {

		// Start listening socket;
		System.out.println("No way to get here.");

		// do stuff
		// [...]
	}
}


thanks for your support,
-Piero
Re: No events received when I listen outside the class who sends [message #1619588 is a reply to message #1619227] Mon, 16 February 2015 17:45 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2526
Registered: July 2012
Senior Member
Are your objects created via injection container? E.g. Using the ContextInjectionFactory?

The events are injected, therefore you only receive events in objects created this way.
Re: No events received when I listen outside the class who sends [message #1620618 is a reply to message #1619588] Tue, 17 February 2015 10:03 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 111
Registered: January 2015
Senior Member

Thank you Dirk.

Do you mean if I ContextInjectionFactory.inject() my WhateverWherever class in the example somewhere in the plugin? No, I didn't and I am sorry if I missed that somewhere in the docs.
In practice... does this mean that the scope of the events' bus is delimited by those classes in the application context?
Which means, without further programmer intervention, command or part handlers or anyway any class referred to in the application model (via classURI) ?

As Lars (thank you Lars for all the material you provide, God bless you) or one of his slaves writes in [1], the event bus is not aware of the context hierarchy (so no events' hierarchy neither) but I interpreted this as "events bus and context are completely separate things." I'd like to fully understand the logic here

[1] http://www.vogella.com/tutorials/Eclipse4EventSystem/article.html#eventsystem_summary

Meanwhile, my working workaround is now to implement a pure-OSGi "event handler" as org.osgi.service.event.EventHandler, and by registering it in the plugin activator:

public class Activator implements BundleActivator {
        public static final String MY_EVENT = "myEvent";
        [...]
        @Override
	public void start(BundleContext bundleContext) throws Exception {
                [...]
                /*** REGISTER MY_EVENT HANDLER ***/
		String topic = MY_EVENT;
		Dictionary<String, Object> properties = new Hashtable<String, Object>();
		properties.put(EventConstants.EVENT_TOPIC, topic);
		ServiceRegistration eventRegistration = context
				.registerService(EventHandler.class.getName(),
						new WhateverWherever(),
						properties);
		eventRegistration.getReference();
	}
}

public class WhateverWherever implements EventHandler {
        /*** HANDLE MY_EVENT EVENT ***/
	@Override
	public void handleEvent(Event event) {

		System.out.println("Event received : https://sometimesigetangry.files.wordpress.com/2011/06/fuck_yea_in_hd_by_crusierpl_re_sharenator_is_growing-s900x773-1208112.png.");

		// do stuff
		// [...]
	}
}


Still in this case I am not able to obtain a complete integration of my WhateverWherever class in the Eclipse event's bus, so I won't be able to register to events like APP_STARTUP_COMPLETE or other {@link org.eclipse.e4.ui.workbench.UIEvents}.

Thanks again for your patience, to all.
-Piero
Re: No events received when I listen outside the class who sends [message #1620957 is a reply to message #1620618] Tue, 17 February 2015 15:01 Go to previous message
Thomas Elskens is currently offline Thomas ElskensFriend
Messages: 159
Registered: September 2014
Location: Brussels - Belgium
Senior Member
Hello Piero,

I think what is meant by the event bus "not being aware of contexts" is that the eventbus "broadcasts" its events to all listeners, regardless of contexts.

If you want to use @UIEventTopic, you have to do so in a location where Eclipse can perform dependency injection, this means in a context function or in any Java class referred by the application model.

If you can't use DI in your use case, registering an eventhandler seems the way to go. As an alternative, you can also use a declarative service. For instance (taken from http://eclipsesource.com/blogs/2009/09/15/osgi-eventadmin/)
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" 
   name="org.eclipse.example.memoryHandler">
<implementation class="org.eclipse.example.MemoryEventHandler"/>
<service> 
 <provide interface="org.osgi.service.event.EventHandler"/>
</service>
 <property name="event.topics" value="org/eclipse/equinox/events/MemoryEvent/CRITICAL" />
</scr:component>
Previous Topic:Injecting own object to global Eclipse context
Next Topic:Custom window heading
Goto Forum:
  


Current Time: Thu Nov 23 15:14:56 GMT 2017

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

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