Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EIG:Distributed EventAdmin Service"

(Getting the Distributed EventAdmin Service)
Line 34: Line 34:
  
 
To run, click on 'Launch an Eclipse application' at the lower left.
 
To run, click on 'Launch an Eclipse application' at the lower left.
 +
 +
If you run the EventAdmin Generic Server.product, you should get output every few seconds like this:
 +
 +
<pre>
 +
handleEvent
 +
topic=defaultTopic
 +
message=message #0
 +
sender=ecftcp://localhost:3787/server
 +
handleEvent
 +
topic=defaultTopic
 +
message=message #1
 +
sender=ecftcp://localhost:3787/server
 +
...
 +
</pre>
 +
 +
This indicates that the EventAdmin service is being accessed to send test messages every 2 seconds.  Here's the code (in '''org.eclipse.ecf.examples.internal.eventadmin.app.TestSender''' class) that is making the send call:
 +
 +
<source lang="java">
 +
while (!done) {
 +
try {
 +
wait(waittime);
 +
Map msgProps = new Properties();
 +
msgProps.put("message", "message #"
 +
+ messageCounter++);
 +
msgProps.put("sender", sender);
 +
eventAdmin.postEvent(new Event(topic, msgProps));
 +
} catch (Exception e) {
 +
e.printStackTrace();
 +
}
 +
}
 +
</source>
 +
 +
What this does is create an Event instance with values for a couple of properties (i.e. "message", and "sender"), and then call eventAdmin.postEvent(Event) to have the eventAdmin implementation deliver the message to EventHandlers.
 +
 +
The test event handler implementation is this (in '''org.eclipse.ecf.examples.internal.eventadmin.app.TestEventHandler''' class):
 +
 +
<source lang="java">
 +
public class TestEventHandler implements EventHandler {
 +
 +
public void handleEvent(Event event) {
 +
System.out.println("handleEvent\n\ttopic=" + event.getTopic()
 +
+ "\n\tmessage=" + event.getProperty("message") + "\n\tsender="
 +
+ event.getProperty("sender"));
 +
}
 +
 +
}
 +
</source>

Revision as of 17:05, 11 June 2009

Introduction

OSGi declares an EventAdmin service that is responsible for distributing events to listeners registered via the (org.osgi.service.event.EventHandler). It's possible to create distributed implementations of such services using JMS and/or other messaging frameworks for distributing messages to other OSGi frameworks.

Distributedeventadmin.png

ECF's provider architecture allows the creation of a distributed EventAdmin implementation that can use a variety of wire protocols. For example, ActiveMQ/JMS 5.2 as above, or ECF generic, XMPP, JavaGroups, commercial/proprietary messaging buses, or any others that can implement the ECF Shared Object API.

Getting the Distributed EventAdmin Service

First, install ECF 3.0. Here is the download page.

Then get the two projects that implement the event admin example. This is a project set file. Save the project set file to your local disk and then import to get into your Eclipse workspace. For anonymous CVS access:

host: dev.eclipse.org

path: /cvsroot/rt

module: org.eclipse.ecf/examples/bundles

projects: org.eclipse.ecf.examples.eventadmin, org.eclipse.ecf.examples.eventadmin.app


Running a Server and Clients

Once these two projects are in your workspace, you can run the ECF generic based server and client by opening the product editor for the following product files:

org.eclipse.ecf.examples.eventadmin.app/EventAdmin Generic Server.product

org.eclipse.ecf.examples.eventadmin.app/EventAdmin Generic Client.product

Productlaunch.png


To run, click on 'Launch an Eclipse application' at the lower left.

If you run the EventAdmin Generic Server.product, you should get output every few seconds like this:

handleEvent
	topic=defaultTopic
	message=message #0
	sender=ecftcp://localhost:3787/server
handleEvent
	topic=defaultTopic
	message=message #1
	sender=ecftcp://localhost:3787/server
...

This indicates that the EventAdmin service is being accessed to send test messages every 2 seconds. Here's the code (in org.eclipse.ecf.examples.internal.eventadmin.app.TestSender class) that is making the send call:

			while (!done) {
				try {
					wait(waittime);
					Map msgProps = new Properties();
					msgProps.put("message", "message #"
							+ messageCounter++);
					msgProps.put("sender", sender);
					eventAdmin.postEvent(new Event(topic, msgProps));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

What this does is create an Event instance with values for a couple of properties (i.e. "message", and "sender"), and then call eventAdmin.postEvent(Event) to have the eventAdmin implementation deliver the message to EventHandlers.

The test event handler implementation is this (in org.eclipse.ecf.examples.internal.eventadmin.app.TestEventHandler class):

public class TestEventHandler implements EventHandler {
 
	public void handleEvent(Event event) {
		System.out.println("handleEvent\n\ttopic=" + event.getTopic()
				+ "\n\tmessage=" + event.getProperty("message") + "\n\tsender="
				+ event.getProperty("sender"));
	}
 
}

Back to the top