OSGi Event Admin with Spring DM

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" ?>
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" ?>
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值