Adding OSGi Capabilities to a Web Service
This chapter shows how to configure a Mavenized web service as an OSGi bundle in Talend ESB, an OSGi container based on Apache Karaf. For more information about Talend ESB, Karaf, and OSGi, see the Introduction to Talend Runtime Container.
Let's package the java_first_jaxws web service created in the previous section as an OSGi bundle. An Apache Felix plugin will be configured in Maven for this. For more information about Felix, visit http://felix.apache.org; for more information on using Felix with Maven, refer to http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html.
In order to package the application as a bundle, you first need to add the Felix Maven dependency to the pom.xml created in the previous section:
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.4.0</version>
</dependency>
Also, we'll need to add the Felix plugin used for creating the bundle. Create a new plugins element under the project's build element (not the build / pluginManagement / plugins element, that is used for configuration outside of Maven's default build process) and add:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>2.3.7</version>
<configuration>
<instructions>
<Bundle-SymbolicName>
${project.groupId}.${project.artifactId}
</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>com.talend.cxf.example.javafirst</Export-Package>
<Bundle-Activator>
com.talend.cxf.example.javafirst.Activator
</Bundle-Activator>
<Require-Bundle>
org.apache.cxf.bundle,org.springframework.beans
</Require-Bundle>
</instructions>
</configuration>
</plugin>
Since you want to package as an OSGi bundle, also change the packaging element at the top of the pom file from war to bundle. As shown above in Felix's maven-bundle-plugin configuration com.talend.cxf.example.javafirst is exported as the bundle name and a CXF dependency is listed. In addition, the "Bundle-Activator" implementation is given. For bundle activation, you will start and stop your service in the Activator's start and stop services respectively. Place the following class in the java_first_jaxws project:
Activator.java:
package com.talend.cxf.example.javafirst;
import javax.xml.ws.Endpoint;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private Endpoint endpoint;
public void start(BundleContext arg0) throws Exception {
try {
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
endpoint = Endpoint.publish(address, implementor);
System.out.println("Server is started...");
} catch (Exception e) {
e.printStackTrace(); throw e;
}
}
public void stop(BundleContext arg0) throws Exception {
try {
endpoint.stop();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
All changes needed have been made. Now let's have Maven create the package:
Select the Run As > Maven Clean and then Run As > Maven Install from the popup menu on pom.xml, or alternatively, mvn clean install from a command prompt located in the project home directory. The application will be packaged and installed into your Maven local repository, by default located in your hidden <user home>/.m2 directory or otherwise as configured by the localRepository field in your ${Maven_HOME}/conf/settings.xml file. You should find it under ${MavenRepository}/ com/talend/cxf/example/ java_first_jaxws/0.0.1-SNAPSHOT/ java_first_jaxws-0.0.1-SNAPSHOT.jar.
You are now ready to deploy the bundle, which can be done as described in Deploying the WSP to Talend ESB (OSGi) except with this install command:
install mvn:com.talend.cxf.example/java_first_jaxws/0.0.1-SNAPSHOT
Run the list command to make sure the bundle has started (check the logfiles in container/log folder for any errors if not) and ensure you can see the service WSDL at http://localhost:9000/helloWorld?wsdl. If so, you are ready to make SOAP calls to the service using SoapUI as shown in the next section.