Enterprise Integration Zone is brought to you in partnership with:

Ouertani Slim was born in Tunisia in 1981. Now he is a software engineer since 2004 and he is Java 6 and Spring certified with 100% and 94% respectively. He is very interested in Java technology, Scala and open source projects. He believes that programming is one of the hardest jobs and most beautiful in the world. Slim has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

SCA, Newton and Spring DM

02.04.2009
| 22210 views |
  • submit to reddit

Consuming The Service :

Let's use Spring annotations to consume this service in the client project
  • Add spring-osgi-annotation to pom XML dependency :

<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-annotation</artifactId>
<version>1.1.1</version>
</dependency>
Register Activator bean in bundle-context.xml and enable annotation processor
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-init-method="init">


<bean id="activator" class="com.jtunisie.osgi.sca.client.Activator"/>


<!-- annotation processor -->
<bean class="org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor"/>
</beans>
 
init() method will called as default-init-method.
  • No need to bundle-context-osgi.xml, we can delete it.
  • In the Activator class we add the Spring annotation as follows:

@ServiceReference(filter="(newton.sca.reference=service_cons)")
public void setService(IService service) {
this.service = service;
}
Note: Filter is used to enable Newton.
  • In the composite file we add a service reference named service_ref_rmi

 <reference name="service_ref_rmi" multiplicity="0..1">
<interface.java interface="com.jtunisie.osgi.sca.IService" />
<binding.rmi/>
</reference>
  • Declare a component named serv-test with the reference name as the newton.sca.reference filter value

  <component name="serv-test">
<description>spring configuration</description>
<reference name="service_cons" />
<sdm:implementation.spring />
</component>
  • Wire reference to component :

 <wire>
<source.uri>serv-test/service_cons</source.uri>
<target.uri>service_ref_rmi</target.uri>
</wire>
  • We have finished our second composite :

<?xml version="1.0" encoding="UTF-8"?>
<composite name="service_test"
xmlns:sdm="http://newton.cauldron.org/springdm">

<reference name="service_ref_rmi" multiplicity="0..1">
<interface.java interface="com.jtunisie.osgi.sca.IService" />
<binding.rmi/>
</reference>
<component name="serv_test">
<description>spring configuration</description>
<reference name="service_cons" />
<sdm:implementation.spring />
</component>

<wire>
<source.uri>serv_test/service_cons</source.uri>
<target.uri>service_ref_rmi</target.uri>
</wire>

</composite>

Running The SCA composite :

Before starting this final step,  we need to add to folders and two files.
  • ls
bin  etc       hello                LICENSE.txt  publish-springdm-1.2.0.xml  sdk                   springdm
doc  examples  hello-config.script  lib          NOTICE.txt                  README.html           var

  • Springdm directory contains Spring 1.2 bundles and the hello bundles :

springdm
|-- aopalliance.osgi-1.0-SNAPSHOT.jar
|-- jcl104-over-slf4j-1.4.3.jar
|-- log4j.osgi-1.2.15-SNAPSHOT.jar
|-- slf4j-api-1.4.3.jar
|-- slf4j-api-1.5.2.jar
|-- slf4j-log4j12-1.4.3.jar
|-- slf4j-log4j12-1.5.2.jar
|-- spring-2.5.5.jar
|-- spring-aop-2.5.5.jar
|-- spring-beans-2.5.5.jar
|-- spring-context-2.5.5.jar
|-- spring-context-support-2.5.5.jar
|-- spring-core-2.5.5.jar
|-- spring-osgi-annotation-1.2.0-m1-SNAPSHOT.jar
|-- spring-osgi-core-1.2.0-m1.jar
|-- spring-osgi-extender-1.2.0-m1.jar
`-- spring-osgi-io-1.2.0-m1.jar
hello
|-- client-1.0-SNAPSHOT.jar
`-- impl-1.0-SNAPSHOT.jar

  • publish-springdm-1.2.0.xml will be used to publish the Spring bundles

<?xml version="1.0" encoding="iso-8859-1"?>
<publish>
<item name="spring-2.5.5.jar" path="springdm/spring-2.5.5.jar" />
<item name="spring-aop-2.5.5.jar" path="springdm/spring-aop-2.5.5.jar" />
<item name="spring-beans-2.5.5.jar" path="springdm/spring-beans-2.5.5.jar" />
<item name="spring-context-2.5.5.jar" path="springdm/spring-context-2.5.5.jar" />
<item name="spring-context-support-2.5.5.jar" path="springdm/spring-context-support-2.5.5.jar" />
<item name="spring-core-2.5.5.jar" path="springdm/spring-core-2.5.5.jar" />
<item name="spring-osgi-annotation-1.2.0-m1-SNAPSHOT.jar" path="springdm/spring-osgi-annotation-1.2.0-m1-SNAPSHOT.jar" />
<item name="spring-osgi-core-1.2.0-m1.jar" path="springdm/spring-osgi-core-1.2.0-m1.jar" />
<item name="spring-osgi-extender-1.2.0-m1.jar" path="springdm/spring-osgi-extender-1.2.0-m1.jar" />
<item name="spring-osgi-io-1.2.0-m1.jar" path="springdm/spring-osgi-io-1.2.0-m1.jar" />
<item name="aopalliance.osgi-1.0-SNAPSHOT.jar" path="springdm/aopalliance.osgi-1.0-SNAPSHOT.jar" />
<item name="jcl104-over-slf4j-1.4.3.jar" path="springdm/jcl104-over-slf4j-1.4.3.jar" />
<item name="slf4j-api-1.4.3.jar" path="springdm/slf4j-api-1.4.3.jar" />
<item name="slf4j-log4j12-1.4.3.jar" path="springdm/slf4j-log4j12-1.4.3.jar" />

</publish>



  • hello-config.script contains script routines :
exec etc/scripts/single-jvm-dist-infra 
# install jini browser (optional)
installer install etc/instances/jinibrowser.composite


installer install etc/instances/server-cds.composite
system manage etc/systems/remote-container.system 


# load bundles into cds
cds scan remote /springdm
cds publish remote  publish-springdm-1.2.0.xml . 
cds scan remote /hello
  • Open two terminals and in the first one we execute :
 bin/container -fabricName=test

on the second one
 bin/container -fabricName=test instance=1

If 2 machines are used

 bin/container -fabricName=test -bindAddress=machineIP(1) -serviceLookupURLs=jini:machineIP(1)
 bin/container -fabricName=test -bindAddress=machineIP(2) -serviceLookupURLs=jini:machineIP(1) -instance=1

  • Execute hello-config.script, type it on two terminals.

>exec hello-config.script


  • On the first jvm, we will install the service implementation :

>installer install composite:sca_service

the Service is now installed and you can verify it on the jini  browser.

  • On the other jvm, we will install the client . Let's verify it presence :

>cds find ser*
>Content[service_test:{bundle.symbolic.name=client-1.0-SNAPSHOT, type=component.template, version=1.0.0.SNAPSHOT, zone=remote}]
  • Install it
> installer install composite:service_test
> Hello world!!!!
  • We get our first service message. In fact, the client bundle is downloaded in the client jvm.
  • Verify Bundles

On the client console type :

>eq ss
.....
109 RESOLVED impl-1.0-SNAPSHOT_1.0.0.SNAPSHOT
110 RESOLVED org.springframework.bundle.osgi.io_1.2.0.m1
111 RESOLVED org.springframework.bundle.osgi.core_1.2.0.m1
112 RESOLVED org.springframework.bundle.osgi.extensions.annotations_1.2.0.m1-SNAPSHOT
113 ACTIVE org.springframework.bundle.osgi.extender_1.2.0.m1
114 ACTIVE client-1.0-SNAPSHOT_1.0.0.SNAPSHOT

Impl bundle is downloaded too but not started. Impl bundle is downloaded because it exports the api interfaces.
On the server console type :

> eq ss 
....
108 ACTIVE impl-1.0-SNAPSHOT_1.0.0.SNAPSHOT
109 RESOLVED org.springframework.bundle.spring_2.5.5
110 RESOLVED org.springframework.bundle.osgi.io_1.2.0.m1
111 RESOLVED org.springframework.bundle.osgi.core_1.2.0.m1
112 RESOLVED org.springframework.bundle.osgi.extensions.annotations_1.2.0.m1-SNAPSHOT
113 ACTIVE org.springframework.bundle.osgi.extender_1.2.0.m1
114 ACTIVE com.sun.jini.browser_2.1.0
Note : The implementation is deployed on the server and is activated.
Note : If we have split the impl on two bundles api and impl, only api bundle will be downloaded and an RMI exception may be thrown.
Published at DZone with permission of its author, Slim Ouertani.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Wassim ABID replied on Wed, 2009/02/04 - 11:48am

Good work :-)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.