SCA, Newton and Spring DM
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>Register Activator bean in bundle-context.xml and enable annotation processor
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-annotation</artifactId>
<version>1.1.1</version>
</dependency>
<?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>
- 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;
}
- 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 :
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
> 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 : 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.
(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