In this tutorial we will demonstrate how to invoke from Camel a JAX-WS Web service running on WildFly, without writing a single line of code.
First of all, here is the Web service implementation:
import javax.jws.WebService; @WebService public class SimpleWebSevice implements Simple { @Override public String hello(String s) { System.out.println("Called Web service with: "+s); return "Hello "+s; } }
And its interface:
public interface Simple { public String hello(String s); }
The Web service is packaged in a Web application named DemoWS.war therefore its WSDL is available as: http://localhost:8080/DemoWS/SimpleWebService?wsdl
Creating the Camel application
Now create a new Camel Maven project as described in this tutorial: Using maven to set up a Camel project
We can describe the Camel route either as Java DSL or via Spring. Using spring, we will create the file camel-context.xml under src/main/resources/META-INF/spring
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:cxf="http://camel.apache.org/schema/cxf" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd"> <cxf:cxfEndpoint id="SimpleWebService" address="http://localhost:8080/DemoWS/SimpleWebService" wsdlURL="http://localhost:8080/DemoWS/SimpleWebService?wsdl" serviceClass="com.sample.SimpleWebService" /> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route id="wsClient"> <from uri="timer:foo?repeatCount=1" /> <setBody> <simple>Hello World!</simple> </setBody> <log message="${body}" /> <to uri="cxf:bean:SimpleWebService?defaultOperationName=hello" /> <to uri="mock:result" /> </route> </camelContext> </beans>
In the first part of the configuration file we have declared the cxf: component, which provides integration with Apache CXF for connecting to JAX-WS services hosted in CXF. Then, our route which:
1) Starts with a Timer which is executed once
2) Sets the message body
3) Logs Message
4) Invokes the hello method of SimpleWebService (passing the Message body)
5) Sends the output to the mock component which cab be used by unit tests
Provided that you have included the Camel Maven plugin in the pom.xml:
<plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> <version>2.14.0</version> </plugin>
Then you can start the route with:
mvn camel:run