In my current post, I will try to highlight using event admin with spring DM. By an example, we will send an event inside the OSGi container from a notifier bundle and intercept by second subscriber bundle.
1 - Notifier Bundle
- It contains a single Activator class.
- Get an Event Admin service.
- Send an event every 30 seconds.
- An event is according to JTUNISIe queue and containing the current step and time stamp.
01.
package com.jtunisie.osgi.event.notifier.impl;
02.
03.
import java.util.Date;
04.
import java.util.Properties;
05.
import org.osgi.service.event.Event;
06.
import org.osgi.service.event.EventAdmin;
07.
import org.springframework.osgi.extensions.annotation.ServiceReference;
08.
09.
public class Activator {
10.
11.
private String EVENT_QUEUE;
12.
13.
protected void init() throws InterruptedException {
14.
Properties props = new Properties();
15.
int i= 0 ;
16.
while ( true ) {
17.
props.setProperty( "property" , "" +i+++ " : " + new Date());
18.
eventAdmin.sendEvent( new Event(EVENT_QUEUE, props));
19.
Thread.sleep( 30 * 1000 );
20.
}
21.
}
22.
private EventAdmin eventAdmin;
23.
24.
@ServiceReference
25.
public void setEventAdmin(EventAdmin eventAdmin) {
26.
this .eventAdmin = eventAdmin;
27.
}
28.
29.
public void setEVENT_QUEUE(String EVENT_QUEUE) {
30.
this .EVENT_QUEUE = EVENT_QUEUE;
31.
}
32.
}
On the spring config file we just call the init method after bundle starting :
01.
<?xml version= "1.0" encoding= "UTF-8" ?>
02.
<beans xmlns= "http://www.springframework.org/schema/beans "
03.
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
04.
xmlns:osgi= "http://www.springframework.org/schema/osgi "
05.
xsi:schemaLocation="http: //www.springframework.org/schema/beans
06.
http: //www.springframework.org/schema/beans/spring-beans.xsd
07.
http: //www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd "
08.
>
09.
10.
<bean id= "activator" class = "com.jtunisie.osgi.event.notifier.impl.Activator" init-method= "init" >
11.
<property name= "EVENT_QUEUE" value= "JTUNISE" />
12.
</bean>
13.
14.
<bean class = "org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor" />
15.
</beans>
2 - Subscriber Bundle
- Contains a single Activator class
- Implements EventHandler Interface
- When an event is intercepted it prints the new property.
01.
package com.jtunisie.osgi.event.subscriber.impl;
02.
03.
import org.osgi.service.event.Event;
04.
import org.osgi.service.event.EventHandler;
05.
06.
/**
07.
*
08.
* @author slim
09.
*/
10.
public class Activator implements EventHandler {
11.
12.
private String property;
13.
14.
@Override
15.
public void handleEvent(Event event) {
16.
17.
String value = event.getProperty(property).toString();
18.
System.out.println( "value : " + value);
19.
}
20.
21.
public void setProperty(String property) {
22.
this .property = property;
23.
}
24.
}
Spring DM helps us to register our bundle as an event hander service and setting it to listen jtunisie queue.
01.
<?xml version= "1.0" encoding= "UTF-8" ?>
02.
<beans xmlns= "http://www.springframework.org/schema/beans "
03.
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
04.
xmlns:osgi= "http://www.springframework.org/schema/osgi "
05.
xmlns:osgix= "http://www.springframework.org/schema/osgi-compendium "
06.
xmlns:context= "http://www.springframework.org/schema/context "
07.
xsi:schemaLocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
08.
http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
09.
http: //www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
10.
http: //www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd "
11.
>
12.
13.
14.
<bean id= "activator" class = "com.jtunisie.osgi.event.subscriber.impl.Activator" >
15.
<property name= "property" value= "property" />
16.
</bean>
17.
18.
<osgi:service id= "activatorService" ref= "activator" interface = "org.osgi.service.event.EventHandler" >
19.
<osgi:service-properties>
20.
<entry key= "event.topics" value= "JTUNISE" />
21.
</osgi:service-properties>
22.
</osgi:service>
23.
24.
25.
</beans>
3- Deployment
- Project is composed of one Maven project with two sub projects using Felix plug-in.
- At the deployment level we need :
1- An OSGi container.
2- Spring dependencies ( Extenders, beans ...) with spring-osgi-annotations
After installing the two bundles, the output should be the same as :
01.
osgi> value : 1 : Tue Nov 11 23:57:33 CET 2008
02.
value : 2 : Tue Nov 11 23:58:03 CET 2008
03.
value : 3 : Tue Nov 11 23:58:33 CET 2008
04.
value : 4 : Tue Nov 11 23:59:03 CET 2008
05.
value : 5 : Tue Nov 11 23:59:33 CET 2008
06.
value : 6 : Wed Nov 12 00:00:03 CET 2008
07.
value : 7 : Wed Nov 12 00:00:33 CET 2008
08.
value : 8 : Wed Nov 12 00:01:03 CET 2008
09.
value : 9 : Wed Nov 12 00:01:33 CET 2008
10.
value : 10 : Wed Nov 12 00:02:03 CET 2008
11.
value : 11 : Wed Nov 12 00:02:33 CET 2008
12.
value : 12 : Wed Nov 12 00:03:03 CET 2008
13.
value : 13 : Wed Nov 12 00:03:33 CET 2008
14.
value : 14 : Wed Nov 12 00:04:03 CET 2008
Note : source code is under svn : svn checkout http://osgievent.googlecode.com/svn/trunk/ osgievent-read-only
本文通过实例展示了如何使用Spring DM与OSGi容器内的事件管理服务(EventAdmin)进行交互。具体包括从通知者模块发送事件及由订阅者模块接收并处理这些事件的过程。

252

被折叠的 条评论
为什么被折叠?



