Showing posts with label OSGi. Show all posts
Showing posts with label OSGi. Show all posts

Thursday, April 29, 2010

Handling Spring ApplicationContext events in Spring DM based OSGi environment

Listening for and handling Spring ApplicationContext events in a Spring DM setup needs needs two steps:

1) Implement a class for the interface org.springframework.osgi.context.event.OsgiBundleApplicationContextListener
import org.osgi.framework.Bundle;
import org.springframework.context.ApplicationContext;
import org.springframework.osgi.context.event.OsgiBundleApplicationContextEvent;
import org.springframework.osgi.context.event.OsgiBundleApplicationContextListener;
import org.springframework.osgi.context.event.OsgiBundleContextClosedEvent;
import org.springframework.osgi.context.event.OsgiBundleContextRefreshedEvent;

public class BundleApplicationContextTracker implements OsgiBundleApplicationContextListener {
public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
if(event instanceof OsgiBundleContextRefreshedEvent) {
// handle bundle start
} else if(event instanceof OsgiBundleContextClosedEvent) {
// handle bundle close
}
}
}

2) Expose the above class as an OSGi service by putting the following entries in Spring beans-config.xml

<beans:bean id="bundleContextTracker" class="org.mypackage.BundleApplicationContextTracker" scope="singleton"/>
<osgi:service id="bundleContextTrackerOSGi" ref="bundleContextTracker" interface="org.springframework.osgi.context.event.OsgiBundleApplicationContextListener"/>

Thursday, February 25, 2010

Controlling setup order for Spring DM applications

A quick and easy way to control the order in which Spring DM applications come up is using cardinality property in service imports.

Assume we have two applications app1 and app2 running in a single OSGi container and using Spring DM. Suppose we want app2 wait till app1 is initialized by Spring DM, we could do the following.
i) Export a service with appropriate interfaces / filters from app1.
For example:-
<osgi:service id="dependencyOSGi" ref="applicationDependency" interface="com.test.ApplicationDependency">
<osgi:service-properties><<entry key="application" value="app1-1.4.0.0"/></osgi:service-properties>
</osgi:service>

ii) Imported the above service in app2 with a cardinality 1..1. The cardinality marks the service dependency as mandatory and causes Spring DM to delay the setup of app2 till the dependency is met (i.e. app1 is up an running).

<osgi:reference id="dependencyApp1OSGi" interface="com.test.ApplicationDependency" filter="(application=app1-1.4.0.0)" cardinality="1..1"/>