2.2. Create a Router Project
Overview
This section describes how to generate a router project, which acts as a proxy for the WS server described in Section 2.1, “Create a Web Services Project”. The starting point for this project is the
camel-archetype-blueprint
Maven archetype.
Prerequisites
This project depends on the
cxf-basic
project and requires that you have already generated and built the cxf-basic
project, as described in Section 2.1, “Create a Web Services Project”.
Create project from the command line
Open a command prompt and change directory to the
get-started
directory. You can now use the archetype:generate
goal to invoke the camel-archetype-blueprint
archetype, which generates a simple Apache Camel demonstration, as follows:
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-blueprint -DarchetypeVersion=2.10.0.redhat-60024 -DgroupId=org.fusesource.example -DartifactId=camel-basic -Dversion=1.0-SNAPSHOT
Note
The arguments of the preceding command are shown on separate lines for readability, but when you are actually entering the command, the entire command must be entered on a single line.
You will be prompted to confirm the project settings, with a message similar to this one:
[INFO] Using property: groupId = org.fusesource.example [INFO] Using property: artifactId = camel-basic [INFO] Using property: version = 1.0-SNAPSHOT [INFO] Using property: package = org.fusesource.example [INFO] Using property: camel-version = 2.9.0.fuse-7-032 [INFO] Using property: log4j-version = 1.2.16 [INFO] Using property: maven-bundle-plugin-version = 2.3.4 [INFO] Using property: maven-compiler-plugin-version = 2.3.2 [INFO] Using property: maven-surefire-plugin-version = 2.11 [INFO] Using property: slf4j-version = 1.6.1 Confirm properties configuration: groupId: org.fusesource.example artifactId: camel-basic version: 1.0-SNAPSHOT package: org.fusesource.example camel-version: 2.9.0.fuse-7-032 log4j-version: 1.2.16 maven-bundle-plugin-version: 2.3.4 maven-compiler-plugin-version: 2.3.2 maven-surefire-plugin-version: 2.11 slf4j-version: 1.6.1 Y: :
Type Return to accept the settings and generate the project. When the command finishes, you should find a new Maven project in the
get-started/camel-basic
directory.
Modify the route
You are going to modify the default route generated by the archetype and change it into a route that implements a HTTP bridge. This bridge will be interposed between the WS client and Web service, enabling us to apply some routing logic to the WSDL messages that pass through the route.
Using your favorite text editor, open
camel-basic/src/main/resources/OSGI-INF/blueprint/blueprint.xml
. Remove the existing bean
element and the camelContext
element and replace them with the camelContext
element highlighted in the following example:
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/blueprint" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <camelContext id="blueprintContext" trace="false" xmlns="http://camel.apache.org/schema/blueprint"> <route id="httpBridge"> <from uri="jetty:http://0.0.0.0:8282/cxf/PersonServiceCF?matchOnUriPrefix=true"/> <delay><constant>5000</constant></delay> <to uri="jetty:http://localhost:8181/cxf/PersonServiceCF?bridgeEndpoint=true&throwExceptionOnFailure=false"/> </route> </camelContext> </blueprint>
The
from
element defines a new HTTP server port, which listens on IP port 8282. The to
element defines a HTTP client endpoint that attempts to connect to the real Web service, which is listening on IP port 8181. To make the route a little more interesting, we add a delay
element, which imposes a five second (5000 millisecond) delay on all requests passing through the route.
For a detailed discussion and explanation of the HTTP bridge, see Proxying with HTTP.
Disable the test
The generated project includes a built-in unit test, which employs the
camel-test
testing toolkit. The Apache Camel testing toolkit is a useful and powerful testing library, but it will not be used in this example.
To disable the test, open the
RouteTest.java
file from the src/test/java/org/fusesource/example
directory using a text editor and look for the @Test
annotation, as shown in the following snippet:
// Java ... public class RouteTest extends CamelBlueprintTestSupport { ... @Test public void testRoute() throws Exception { ...
Now comment out the
@Test
annotation, as shown in the following snippet, and save the modified RouteTest.java
file.
... public class RouteTest extends CamelBlueprintTestSupport { ... // @Test // Disable test! public void testRoute() throws Exception { ...
Add the required Maven dependency
Because the route uses the Apache Camel Jetty component, you must add a Maven dependency on the
camel-jetty
artifact, so that the requisite JAR files are added to the classpath. To add the dependency, edit the camel-basic/pom.xml
file and add the following highlighted dependency as a child of the dependencies
element:
<?xml version="1.0" encoding="UTF-8"?> <project ...> ... <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.10.0.redhat-60024</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-blueprint</artifactId> <version>2.10.0.redhat-60024</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jetty</artifactId> <version>2.10.0.redhat-60024</version> </dependency> ... </dependencies> ... </project>
Build the router project
Build the router project and install the generated JAR file into your local Maven repository. From a command prompt, enter the following commands:
cd camel-basic mvn install
Deploy and start the route
If you have not already started the Red Hat JBoss Fuse container and deployed the Web services bundle, you should do so now—see the section called “Deploy and start the WS server”.
To install the
camel-basic
route as an OSGi bundle, enter the following console command:
JBossFuse:karaf@root> install mvn:org.fusesource.example/camel-basic/1.0-SNAPSHOT
If the bundle is successfully resolved and installed, the container responds by giving you the ID of the newly created bundle—for example:
Bundle ID: 230
You can now start up the Web service using the
start
console command, specifying the bundle ID, as follows:
JBossFuse:karaf@root> start 230
Test the route with the WS client
The
cxf-basic
project includes a simple WS client, which you can use to test the deployed route and Web service. In a command prompt, navigate to the cxf-basic
directory and run the simple WS client as follows:
cd ../cxf-basic mvn -Pclient -Dexec.args="http://localhost:8282/cxf/PersonServiceCF"
If the client runs successfully, you should see output like the following:
INFO: Creating Service {http://example.fusesource.org/}PersonService from class org.fusesource.example.Person Invoking getPerson...
After a five second delay, you will see the following response:
getPerson._getPerson_personId=Guillaume getPerson._getPerson_ssn=000-000-0000 getPerson._getPerson_name=Guillaume