This section explains how to generate, build, and run a complete Fuse Service Framework example in the OSGi container, where the starting point code is generated with the help of a Maven archetype.
In order to generate a project using a Fuse ESB Maven archetype, you must have the following prerequisites:
Maven installation—Maven is an open source build tool from Apache. You can download the latest version from http://maven.apache.org/download.html (minimum is 2.0.9).
Internet connection—whilst performing a build, Maven dynamically searches external repositories and downloads the required artifacts on the fly. In order for this to work, your build machine must be connected to the Internet.
fusesource Maven repository is configured—in order to locate the archetypes, Maven's
settings.xml
file must be configured with the location of thefusesource
Maven repository. For details of how to set this up, see Adding the fusesource repository.
The servicemix-cxf-code-first-osgi-bundle
archetype creates a project
for building a Java-first JAX-WS application that can be deployed into the OSGi
container. To generate a Maven project with the coordinates,
org.fusesource.example:cxf-code-first-bundle
, enter the following
command:
mvn archetype:create -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-cxf-code-first-osgi-bundle -DarchetypeVersion=2011.02.1-fuse-00-08 -DgroupId=org.fusesource.example -DartifactId=cxf-code-first-bundle
The result of this command is a directory,
,
containing the files for the generated bundle project.ProjectDir
/cxf-code-first-bundle
Typically, you will need to modify the instructions for the Maven bundle plug-in
in the POM file. In particular, the default Import-Package
element
generated by the servicemix-cxf-code-first-osgi-bundle
archetype is not
configured to scan the project's Java source files. In most cases, however, you
would want the Maven bundle plug-in to perform this automatic scanning in order to
ensure that the bundle imports all of the packages needed by your code.
To enable the Import-Package
scanning feature, simply add the
wildcard, *
, as the last item in the comma-separated list inside the
Import-Package
element, as shown in the following example:
<project ... > ... <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> ... <Import-Package> javax.jws, javax.wsdl, javax.xml.bind, javax.xml.bind.annotation, javax.xml.namespace, javax.xml.ws, META-INF.cxf, META-INF.cxf.osgi, org.apache.cxf.bus, org.apache.cxf.bus.spring, org.apache.cxf.bus.resource, org.apache.cxf.configuration.spring, org.apache.cxf.resource, org.apache.cxf.jaxws, org.apache.cxf.transport.http_osgi, org.springframework.beans.factory.config, * </Import-Package> ... </instructions> </configuration> </plugin> </plugins> </build> ... </project>
To install and run the generated cxf-code-first-bundle
project,
perform the following steps:
Build the project—open a command prompt and change directory to
. Use Maven to build the demonstration by entering the following command:ProjectDir
/cxf-code-first-bundlemvn install
If this command runs successfully, the
directory should contain the bundle file,ProjectDir
/cxf-code-first-bundle/targetcxf-code-first-bundle-1.0-SNAPSHOT.jar
.Install and start the cxf-code-first-bundle bundle—at the Fuse ESB console, enter the following command:
karaf@root> osgi:install -s file:
ProjectDir
/cxf-code-first-bundle/target/cxf-code-first-bundle-1.0-SNAPSHOT.jarWhere
ProjectDir
is the directory containing your Maven projects and the-s
flag directs the container to start the bundle right away. For example, if your project directory isC:\Projects
on a Windows machine, you would enter the following command:karaf@root> osgi:install -s file:C:/Projects/cxf-code-first-bundle/target/cxf-code-first-bundle-1.0-SNAPSHOT.jar
Note On Windows machines, be careful how you format the
file
URL—for details of the syntax understood by thefile
URL handler, see File URL Handler.Alternatively, you could install the bundle from your local Maven repository, using the following PAX
mvn
URL:karaf@root> osgi:install -s mvn:org.fusesource.example/cxf-code-first-bundle/1.0-SNAPSHOT
Test the Web serivce—to test the Web service deployed in the previous step, you can use a web browser to query the service's WSDL. Open your favorite web browser and navigate to the following URL:
http://localhost:8181/cxf/PersonServiceCF?wsdl
When the web service receives the query,
?wsdl
, it returns a WSDL description of the running service.Stop the cxf-code-first-bundle bundle—to stop the
cxf-code-first-bundle
bundle, you first need to discover the relevant bundle number. To find the bundle number, enter the following console command:karaf@root> osgi:list
At the end of the listing, you should see an entry like the following:
[ 191] [Active ] [ ] [ ] [ 60] A CXF Code First OSGi Project (1.0.0.SNAPSHOT)
Where, in this example, the bundle number is 191. To stop this bundle, enter the following console command:
karaf@root> osgi:stop 191