...
In the OSGI-INF/component.xml
file the AdderServiceImpl is instantiated and registered with the OSGi service registry with the distribution properties. These properties instruct. Distributed OSGi into making the service available on http://localhost:9090/adder.
Code Block | ||||
---|---|---|---|---|
| ||||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="DS Service Sample"> <implementation class="org.apache.cxf.dosgi.samples.ds.impl.AdderServiceImpl"/> <property name="osgiservice.remoteexported.interfaces">*</property> value="*" /> <property name="osgiservice.remoteexported.configuration.typeconfigs" value="pojoorg.apache.cxf.ws" /> <property name="osgiorg.remoteapache.configurationcxf.pojows.address" value="http://localhost:9090/adder" /> <service> <provide interface="org.apache.cxf.dosgi.samples.ds.AdderService"/> </service> </scr:component> |
...
So let's install the server side in Equinox, together with the Equinox DS implementation. You can do this from the Equinox command line, but in this document I'll launch Equinox from within Eclipse (last tried with Eclipse 3.6).
I'm starting off by importing the Single Bundle Distribution as a binary project in Eclipse by going File -> Import | Plug-ins and Fragments and then select the directory that contains the single bundle distribution JAR file. My workspace now looks like this:
Next I'll create an OSGi Framework launch configuration that includes DS. To do this
...
Code Block |
---|
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.0.v20090520
1 ACTIVE org.eclipse.equinox.util_1.0.100.v20090520-1800
2 ACTIVE org.eclipse.osgi.services_3.2.0.v20090520-1800
3 ACTIVE cxf-dosgi-ri-singlebundle-distribution_1.1.0.SNAPSHOT
4 ACTIVE org.eclipse.equinox.ds_1.1.0.v20090520-1800 |
...
Code Block |
---|
osgi> install http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-interface/1.12/cxf-dosgi-ri-samples-ds-interface-1.12.jar Bundle id is 5 osgi> install http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-impl/1.12/cxf-dosgi-ri-samples-ds-impl-1.12.jar Bundle id is 6 osgi> start 6 ... log messages may appear ... |
Tip |
---|
You can also import the maven projects of the DS demo into Eclipse, this would save you from installing it with a URL as above. To do this, check out the CXF/DOSGi source from SVN (http://svn.apache.org/repos/asf/cxf/dosgi/trunk |
...
Code Block | ||||
---|---|---|---|---|
| ||||
public class AdderConsumer { private AdderService adder; public void bindAdder(AdderService a) { adder = a; } public void unbindAdder(AdderService a) { adder = null; } public void start(ComponentContext cc) { System.out.println("Using adder service: 1 + 1 = " + adder.add(1, 1)); } } |
...
As in the Greeter demo, the client side needs to be configured to know where the remote service actually is. This is one in the OSGI-INF/remote-service/remote-services.xml
file:
Code Block | ||||
---|---|---|---|---|
| ||||
<service<endpoint-descriptions xmlns="http://www.osgi.org/xmlns/sdrsa/v1.0.0"> <service<endpoint-description> <provide<property interfacename="orgobjectClass"> <array> <value>org.apache.cxf.dosgi.samples.ds.AdderService" /> AdderService</value> </array> <property name="osgi.remote.interfaces">*</property> <property name="osgi.remote.configuration.type">pojo<endpoint.id">http://localhost:9090/adder</property> <property name="osgi.remote.configuration.pojo.address">http://localhost:9090/adder</service.imported.configs">org.apache.cxf.ws</property> </serviceendpoint-description> </serviceendpoint-descriptions> |
Install and run the consumer side of the demo in a separate Equinox instance (tip: you can duplicate the launch configuration used for the server side in the 'Run Configurations' dialog):
Code Block |
---|
osgi> install http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-interface/1.12/cxf-dosgi-ri-samples-ds-interface-1.12.jar Bundle id is 5 osgi> install http://repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-client/1.12/cxf-dosgi-ri-samples-ds-client-1.12.jar Bundle id is 6 osgi> start 6 ... log messages may appear, after a little while the following message appears: Using adder service: 1 + 1 = 2 |
...
Code Block |
---|
Adder service invoked: 1 + 1 = 2 |
Consumer Note
Some OSGi Declarative Services implementations don't explicitly register interest in the requested services with the OSGi Framework. They rather user a generic Service Tracker or Service Listener to track all available services. This doesn't provide the CXF-DOSGi implementation with the information about what services the consumer is looking for through the ListenerHook and hence it can't register the remote service on-the-fly. A simple workaround to this problem is to add an Activator to a bundle in the client-side framework (this activator could be in any bundle) which registers an explicit ServiceTracker for the remote service the DS component wants to be injected with. An example of such an Activator can be found here.
In the future a more elegant solution to this problem will hopefully be provided.