Spring And Spring DM Support In Fuji
Fuji provides a strong support for Interacting of JBI application with that of JAVA code outside of JBI, This documents provide the details how this can achieved easily with the existing simple Service API, and what are the Future plans. This document to be precise concentrates on how the api can be used with respect to spring and springDM applications
Using Fuji simple service API
This blog
gives an idea how to provision a service and consume a service from within springDM and access the same from JBI, However we will delve into some of the details which one would require in order the setup the environment
Installing and starting the springDM bundles
This is key for the springDM applications to work, There a two possible approach one can take in order to install the springDM Bundles on Fuji-Felix or Fuji-Glassfish, The set of Bundles which needs to be installed are
- The extender bundle itself, org.springframework.osgi.extender
- The core implementation bundle for the Spring Dynamic Modules support, org.springframework.osgi.core
- The Spring Dynamic Modules I/O support library bundle, org.springframework.osgi.io
- spring-core.jar (bundle symbolic name org.springframework.core)
- spring-context.jar (bundle symbolic name org.springframework.context)
- spring-beans.jar (bundle symbolic name org.springframework.beans)
- spring-aop.jar (bundle symbolic name org.springframework.aop)
- aopalliance
- cglib-nodep (when proxying classes rather then interfaces, needed in most cases)
- commons-logging API (SLF4J version highly recommended:
- SLF4J API (com.springsource.sfl4j.api.jar)
- SLF4J Implementation Bridge (such as Log4j - com.springsource.sfl4j.log4j.jar)
- SLF4J commons logging adapter (com.springsource.sfl4j.org.apache.commons.logging.jar)
- logging implementation suitable for commons-logging (such as log4j)
The order in which this osgi bundles can be installed is shown below
[ 40] [Resolved ] [ 2] spring-osgi-core (1.2.0)
[ 41] [Active ] [ 1] cglib-nodep.osgi (2.1.3.SNAPSHOT)
[ 42] [Active ] [ 1] AOP Alliance API (1.0.0)
[ 43] [Active ] [ 1] SLF4J API (1.5.0)
[ 44] [Resolved ] [ 1] SLF4J Log4J Binding (1.5.0)
[ 45] [Active ] [ 1] SLF4J Jakarta Commons Logging Over SLF4J Binding (1.5.0)
[ 46] [Active ] [ 1] Spring AOP (2.5.6.A)
[ 47] [Active ] [ 1] Spring Beans (2.5.6.A)
[ 48] [Active ] [ 1] Spring Context (2.5.6.A)
[ 49] [Active ] [ 1] Spring Core (2.5.6.A)
[ 50] [Active ] [ 1] spring-osgi-extender (1.2.0)
[ 51] [Active ] [ 1] spring-osgi-io (1.2.0)
[ 52] [Active ] [ 1] log4j.osgi (1.2.15.SNAPSHOT)
Developers Approach , building Fuji-Felix with springDM bundles
One can always create a Fuji-Felix bundle, which has all the required springDM bundle preinstalled, For this follow the steps as shown
a) Modify /fuji/packaging/release-packages/felix/pom.xml
1. Incude the Bundles/jar as part of Fuji-Felix
<resources>
<resource>
<targetPath>fuji/bundle</targetPath>
<filtering>false</filtering>
<directory>target/staging</directory>
<includes>
<!-- Core OSGi -->
...
<!--spring DM-->
<include>spring-osgi-core.jar</include>
<include>spring-osgi-extender.jar</include>
<include>spring-osgi-io.jar</include>
<include>cglib-nodep.osgi.jar</include>
<include>com.springsource.org.aopalliance.jar</include>
<include>com.springsource.slf4j.api.jar</include>
<include>com.springsource.slf4j.log4j.jar</include>
<include>com.springsource.slf4j.org.apache.commons.logging.jar</include>
<include>org.springframework.aop.jar</include>
<include>org.springframework.beans.jar</include>
<include>org.springframework.context.jar</include>
<include>org.springframework.core.jar</include>
<include>log4j.osgi.jar</include>
</includes>
</resource>
...
2.Fetch spring DM framework module jars and dependencies
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
...
<!-- fetch spring DM framework module, excluding transitive dependencies false : -->
<execution>
<id>${project.artifactId}-fetch-springDM</id>
<phase>generate-resources</phase>
<goals><goal>copy-dependencies</goal></goals>
<configuration>
<outputDirectory>${stagingDir}</outputDirectory>
<stripVersion>true</stripVersion>
<includeGroupIds>org.springframework.osgi</includeGroupIds>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
<execution>
<id>${project.artifactId}-fetch-spring</id>
<phase>generate-resources</phase>
<goals><goal>copy-dependencies</goal></goals>
<configuration>
<outputDirectory>${stagingDir}</outputDirectory>
<stripVersion>true</stripVersion>
<includeGroupIds>org.springframework,org.aopalliance,org.slf4j</includeGroupIds>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
3. Add the required dependency in the Projects for the springDM jars
<dependencies>
...
<!-- spring DM -->
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-extender</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>log4j.osgi</artifactId>
<version>1.2.15-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>cglib-nodep.osgi</artifactId>
<version>2.1.3-SNAPSHOT</version>
</dependency>
<!-- spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>2.5.6.A</version>
</dependency>
<dependency>
<groupId>org.aopalliance</groupId>
<artifactId>com.springsource.org.aopalliance</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>com.springsource.slf4j.log4j</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
b) Modify /fuji/packaging/release-packages/felix/conf/config.properties
This will ensure that all the springDM bundles are started, once the Fuji-Felix is started
felix.auto.start.1= \
...
...
file:bundle/spring-osgi-core.jar \
file:bundle/cglib-nodep.osgi.jar \
file:bundle/com.springsource.org.aopalliance.jar \
file:bundle/com.springsource.slf4j.api.jar \
file:bundle/com.springsource.slf4j.log4j.jar \
file:bundle/com.springsource.slf4j.org.apache.commons.logging.jar \
file:bundle/org.springframework.aop.jar \
file:bundle/org.springframework.beans.jar \
file:bundle/org.springframework.context.jar \
file:bundle/org.springframework.core.jar \
file:bundle/spring-osgi-extender.jar \
file:bundle/spring-osgi-io.jar \
file:bundle/log4j.osgi.jar
c) GO to FUJI SRC_ROOT , mvn clean install
i.e build Fuji-Felix bundle
Create SpringDM application
Maven archetype for creating springDM app
mvn archetype:create
-DarchetypeGroupId=org.springframework.osgi
-DarchetypeArtifactId=spring-osgi-bundle-archetype
-DarchetypeVersion=1.1.0
-DgroupId=org.foo
-DartifactId=org.foo.springdm.fuji
-Dversion=1.0
how does the project created look like
Add Fuji Simple Service API dependency, add the following to the pom.xml of springDM app
<dependency>
<groupId>open-esb.fuji</groupId>
<artifactId>api</artifactId>
<version>1.0-M9-SNAPSHOT</version>
</dependency>
now we are all set to make use of the Fuji Simple Service API
Create Service Provider and export the same as osgi service, with the required service properties, Note how this can be easily done declaratively using the springDM, One does not have to write code for bundle activation or service registration.
a) POJO Implementation: The example below shows a simple service provider
package org.foo;
import org.glassfish.openesb.api.service.ServiceMessage;
import org.glassfish.openesb.api.service.ServiceMessageFactory;
import org.glassfish.openesb.api.service.ServiceProvider;
public class MyProvider implements ServiceProvider {
ServiceMessageFactory msgFactory_;
public ServiceMessage invoke(ServiceMessage message) throws Exception {
// Print out the content of the message
String payload = (String)message.getPayload();
System.out.println(payload);
// One way exchange, so there's no need to return anything
return null;
}
public void setMessageFactory(ServiceMessageFactory factory) {
msgFactory_ = factory;
}
public void setAnotherBean (AnotherBeanI bean){
//make use the bean
}
}
b) Define the bean in the spring config file
<?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 name="anotherBean" class="org.foo.AnotherBeanImpl" />
<bean name="fujiService" class="org.foo.MyProvider">
<property name="anotherBean" ref="anotherBean" />
</bean>
</beans>
c) Define the bean to be exported as osgi service in the spring-osgi config file
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="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">
<service id="fujiServiceOsgi" ref="fujiService"
interface="org.glassfish.openesb.api.service.ServiceProvider">
<service-properties>
<beans:entry key="org.glassfish.openesb.serviceName" value="xyzService" />
<beans:entry key="org.glassfish.openesb.endpointName" value="endpoint1" />
</service-properties>
</service>
</beans:beans>
d) Build and depoy the springDM application
- mvn clean install
- go to felix console and install/start the springDM app buundle
Service Consumer: Now let us create a service consumer which consumes JBI services, The example below shows a simple Service Consumer
a) POJO Implementation: The example below shows a simple bean using the service consumer
package org.foo;
import org.glassfish.openesb.api.service.ServiceConsumer;
import org.glassfish.openesb.api.service.ServiceMessage;
import org.osgi.framework.ServiceReference;
public class BeanWithJbiServiceReference {
private ServiceReference serviceReference;
private ServiceConsumer service;
public void setServiceReference(ServiceReference serviceReference) {
this.serviceReference = serviceReference;
}
public void setService(ServiceConsumer service) {
this.service = service;
}
public void invokeJBIService(Object payload) throws Exception{
ServiceMessage message = service.getServiceMessageFactory().createMessage();
message.setPayload(payload);
service.invoke(message);
}
}
b)spring config file, define the bean, this uses dependency injection to inject the service reference
<bean id="someBean" class="org.foo.BeanWithJbiServiceReference" lazy-init="false">
<property name="serviceReference" ref="jbiService" />
<property name="service" ref="jbiService" />
</bean>
c)spring osgi config file, define the service reference , which initiated based on the filter
<reference id="jbiService"
interface="org.glassfish.openesb.api.service.ServiceConsumer"
filter="(org.glassfish.openesb.serviceName=abcService)" />
Sample Application
attached here
is sample application which include for both service provider and service consumer
Future Enhancement
The plans include seamless integration for existing spring applications and native spring support, more info