Apache Karaf Cellar and DOSGi
Distributed OSGi enables the distribution of OSGi services across the Cellar nodes.
The purpose of the Cellar DOSGi is to leverage the Cellar resources (Hazelcast instances, distributed map, etc), and to be very easy to use.
Karaf Cellar DOSGi installation
DOSGi is provided by installing the optional feature cellar-dosgi.
karaf@root> feature:repo-add mvn:org.apache.karaf.cellar/apache-karaf-cellar/3.0.3/xml/features
karaf@root> feature:install cellar
List Distributed services
You can see all OSGi services as distributed using the cluster:list-service command:
karaf@root()> cluster:service-list
DOSGi Example
- Service provider bundle is installed in node A and “flag” a distributed service
- Client bundle is installed on node B and will use the provider service
Provider bundle
The provider bundle expose an OSGi service, exposed as a distributed service.
The Service is doing simple echo.
Interface:
package com.vimal.karaf.cellar.sample;
public interface SampleService {
String process(String message);
}
Implementation:
package com.vimal.karaf.cellar.sample;
public class SampleServiceImpl implements SampleService {
public String process(String message) {
return "Distributed service sample: " + message;
}
}
To expose the service in Karaf, we create a blueprint descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="sampleService" class="com.vimal.karaf.cellar.sample.SampleServiceImpl"/>
<service ref="sampleService" interface="com.vimal.karaf.cellar.sample.SampleService">
<service-properties>
<entry key="service.exported.interfaces" value="*"/>
</service-properties>
</service>
</blueprint>
The only “Important” part is that we added the service.exported.interfaces
property which is just a flag to define the interface/service to define as distributed. Means, it’s really easy to turn an existing service as a distributed service.
Client Bundle
The client bundle will get a reference to the sampleService. The reference will be a kind of proxy to the service implementation located remotely, on another node.
package com.vimal.karaf.cellar.sample.client;
public class SampleClient {
private SampleService sampleService;
public void trigger() throws Exception {
int i = 0;
while (true) {
System.out.println(sampleService.process("Count" + i));
Thread.sleep(10000);
i++;
}
}
public SampleService getSampleService() {
return this.sampleService;
}
public void setSampleService(SampleService sampleService) {
this.sampleService = sampleService;
}
}
Blueprint:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<reference id="sampleService" interface="com.vimal.karaf.cellar.sample.SampleService"/>
<bean id="sampleClient" class="com.vimal.karaf.cellar.sample.client.SampleClient" init-method="trigger">
<property name="sampleService" ref="sampleService"/>
</bean>
</blueprint>
If a local sampleService is available, the OSGi framework will bind the reference to this service, else Cellar will look for a distributed service (on all nodes) exporting the SampleService interface and bind a proxy to the distributed service.
Leave your comments......
No comments:
Post a Comment