Platinum Partner
java,osgi,frameworks,spring dm,server-side,client-side,event admin

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.

 

package com.jtunisie.osgi.event.notifier.impl;

import java.util.Date;
import java.util.Properties;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.springframework.osgi.extensions.annotation.ServiceReference;

public class Activator {

private String EVENT_QUEUE;

protected void init() throws InterruptedException {
Properties props = new Properties();
int i=0;
while (true) {
props.setProperty("property", ""+i+++" : "+ new Date());
eventAdmin.sendEvent(new Event(EVENT_QUEUE, props));
Thread.sleep(30*1000);
}
}
private EventAdmin eventAdmin;

@ServiceReference
public void setEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}

public void setEVENT_QUEUE(String EVENT_QUEUE) {
this.EVENT_QUEUE = EVENT_QUEUE;
}
}

 On the spring config file we just call the init method after bundle starting :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"
>

<bean id="activator" class="com.jtunisie.osgi.event.notifier.impl.Activator" init-method="init">
<property name="EVENT_QUEUE" value="JTUNISE"/>
</bean>

<bean class="org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor"/>
</beans>

 

2 -  Subscriber Bundle

  •  Contains a single Activator class
  •  Implements EventHandler Interface
  •  When an event is intercepted it prints the new property.

 

package com.jtunisie.osgi.event.subscriber.impl;

import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

/**
*
* @author slim
*/
public class Activator implements EventHandler {

private String property;

@Override
public void handleEvent(Event event) {

String value = event.getProperty(property).toString();
System.out.println("value : " + value);
}

public void setProperty(String property) {
this.property = property;
}
}

 

Spring DM helps us to register our bundle as an event hander service and setting it to listen jtunisie queue.

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"
>


<bean id="activator" class="com.jtunisie.osgi.event.subscriber.impl.Activator">
<property name="property" value="property"/>
</bean>

<osgi:service id="activatorService" ref="activator" interface="org.osgi.service.event.EventHandler">
<osgi:service-properties>
<entry key="event.topics" value="JTUNISE"/>
</osgi:service-properties>
</osgi:service>


</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 :

 

osgi> value : 1 : Tue Nov 11 23:57:33 CET 2008
value : 2 : Tue Nov 11 23:58:03 CET 2008
value : 3 : Tue Nov 11 23:58:33 CET 2008
value : 4 : Tue Nov 11 23:59:03 CET 2008
value : 5 : Tue Nov 11 23:59:33 CET 2008
value : 6 : Wed Nov 12 00:00:03 CET 2008
value : 7 : Wed Nov 12 00:00:33 CET 2008
value : 8 : Wed Nov 12 00:01:03 CET 2008
value : 9 : Wed Nov 12 00:01:33 CET 2008
value : 10 : Wed Nov 12 00:02:03 CET 2008
value : 11 : Wed Nov 12 00:02:33 CET 2008
value : 12 : Wed Nov 12 00:03:03 CET 2008
value : 13 : Wed Nov 12 00:03:33 CET 2008
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

 

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}