Camel DSL and transport supports for JMS and Mail are brilliant features, one thing I have noticed when I go through the Camel documentation and samples is most of the samples only discuss about CXF integration unlike discussing about more generic web service support.
I’m familiar with Axis2 and I’m happy with its features but in order to try out those camel samples I had to have some CXF experience, I think they should address to broad community with more generalize samples that doesn’t depend on specific implementation.
After reading those samples I wanted to try out for Axis2 with those samples instead of CXF. Camel is developed based on Spring and thanks to the Axis2’s Spring support I could implement those samples with Axis2 without any pain.
Here I describe how to implement “reportincident “sample application with Axis2, but it is important to read the original tutorial from here to get clear idea about the use case before try out for this.
First let’s start with Web service implementation, it is possible to use either contract –first or code-first approach for this, here I used code-first approach to create a service quickly.
public class SimpleService {As you can see only a POJO method here, with Service.xml we can convert this class a s a web service, before going to that we need to integrate CamelContext : entry point for Camel Framework to our service by adding following field and setter/getter methods.
public OutputReportIncident reportIncident(InputReportIncident parameters) {
// return an OK reply
OutputReportIncident out = new OutputReportIncident();
out.setCode("OK");
return out;
}
}
And also you can see we route our incoming message to Camel using ProducerTemplate within our method .
Note : If your are not familiar with POJO based Axis2 web service development please refer this tutorial.
public class SimpleService {
** The context. */
private CamelContext context;
public OutputReportIncident reportIncident(InputReportIncident parameters) {
// create the producer template to use for sending messages
ProducerTemplate producer = context.createProducerTemplate();
// send the body and the filename defined with the special header key
try {
producer.start();
Object mailBody = producer.sendBody("direct:start", parameters);
System.out.println("Body:" + mailBody);
} catch (Exception e) {
e.printStackTrace();
}
// return an OK reply
OutputReportIncident out = new OutputReportIncident();
out.setCode("OK");
return out;
}
public CamelContext getContext() {
return context;
}
public void setContext(CamelContext context) {
this.context = context;
}
}
We have routed our incoming messages to “direct:start” endpoint URI , so what is next …?For Camel routing there are two options available.
- Use Camel java DSL.
- Use Spring configuration.
<!-- create a camel context as to start Camel -->Note: for SMTP URI you have to provide correct values for two email accounts.
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start"/>
<to uri="velocity:MailBody.vm"/>
<to uri="file://files"/>
</route>
<route>
<from uri="file://files"/>
<setOutHeader headerName="subject">
<constant>new incident reported</constant>
</setOutHeader>
<to
uri="smtp://axisClient@localhost:25?password=axisClient&to=axisServer@localhost"/>
</route>
</camelContext>
<bean id="service" class="start.SimpleService">
<property name="context" ref="camel"/>
</bean>
Next step is define our POJO service as a web service using Service.xml , we use following two lines to add Spring bean as a web service.
<parameter name="ServiceObjectSupplier" locked="false">There is one more thing to do, we have to add Spring ContextLoaderListener in to web,xml file in order to load Camel context at startup time .
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
<parameter name="SpringBeanName" locked="false">service</parameter>
<!-- location of spring xml files -->Note : This tutorial describe the approach we used to embed Axis2 in to our web application.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:camel-config.xml</param-value>
</context-param>
<!-- the listener that kick-starts Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You can download complete example from here.
Now using Maven you can start the Jetty server using “jetty:run”
In the http://localhost:8080/services/SimpleService?wsdl you can see the WSDL content for our service.
Now you can run Client.java and check your email to see the email about the reportincident.
As you can see above we integrated SMTP transport to our service using one line in configuration file, if you want change it in to JMS transport , again it is simple as change the URI. Also we have used Velocity temples to format our Email message within configuration file. This is the real power of Camel you can achieve most of the difficult tasks with few lines in the context file, happy ride with Camel...!!
If you want you can use Camel Java DSL to write the router instead of Spring configuration approach , this sample shows how you could do that.
Also note that at this point I have discussed the topic that equivalent with up to part 4 of original tutorial. In there, they have part 5 that discuss how you can use Camel CXF component to enhance this sample application; with that particular component we can implement web service endpoint within the configuration file without having any Java codes but unfortunately there is no such Camel component for Axis2 at the moment so we can’t cover that part.
download link for Spring based example.
download link for Java DSL based example.
