We have learned about using Camel to do routing inside ServiceMix, but is also possible use Apache Camel's optional components inside ServiceMix to provide even more possibilities than those offered by the standard JBI components.

Using MINA for plain TCP communication

ServiceMix itself doesn't have a JBI component to do plain TCP communications, but Camel does have an Apache MINA component that allows you to do this. This component is available as a separate JAR file, so we will have to first add it to our SU. Afterwards, we can just start using it in our routes.

Adding a dependency

To be able to use camel-mina, we will specify if as a <dependency/> in our tutorial-camel-su's pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <parent>
    <artifactId>parent</artifactId>
    <groupId>org.apache.servicemix.tutorial.camel</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.servicemix.tutorial.camel</groupId>
  <artifactId>tutorial-camel-su</artifactId>
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-mina</artifactId>
      <version>${camel-version}</version>
    </dependency>
    ...
 </dependencies>
  ...
</project>

Coding the new routes

Let's look at the necessary modifications to our RouteBuilder implementation:

public class MyRouteBuilder extends RouteBuilder {

    public void configure() {
    	from("mina:tcp://localhost:10021")                                              //1  
    	    .to("jbi:endpoint:urn:org:apache:servicemix:tutorial:camel:jms:provider");  //2
    	    
    	//...and receive messages sent by other JBI endpoints
    	from("jbi:endpoint:urn:org:apache:servicemix:tutorial:camel:jms:consumer")      
    	    .to("log:tutorial-jbi")
    	    .convertBodyTo(DOMSource.class)
    	    .to("log:tutorial-domsource")
    	    .convertBodyTo(String.class)
    	    .to("log:tutorial-string"); 
    	
    	from("timer://tutorial?fixedRate=true&delay=3000&period=10000")          
    	    .setBody(constant("<message>Hello world!</message>"))      
    	    .to("mina:tcp://localhost:10021");                        // 3
    }
}
  1. The only thing we need to know for using our new component in our Camel routes is a new URI format. This line will start a socket listener on port 10021 of our local machine...
  2. ... and send the incoming message exchanges to our JMS consumer provider endpoint using the jbi: URI we learned about on the previous page. Camel will take care of converting the XML String into a valid JBI MessageExchange once again.
  3. Finally, we also change the final .to() of our timer: route to send the message through the socket listener that was started at (1).

By now, you should have a basic idea of what is possible with ServiceMix and Camel. As a final appetizer, let us take a look at some of the other features before ending the tutorial.

Things to remember

  • You can use Apache Camel components side-by-side with the existing JBI components.
  • Starting to use new Camel component only requires adding the component as a dependency and learning about the URI format it uses.

Further reading