Apache Camel Mina Component

0

Camel uses Apache MINA to communicate with the low-level networking protocols, such as the Transmission Control Protocol(TCP) and the User Datagram Protocol (UDP).

In this article, we will first start a server using Apache MINA to serve requests over TCP. Once the server is up and running, we will run a client program that connects to the Apache MINA Server to request information synchronously.

This example uses the following frameworks:

  1. Maven 3.2.3
  2. Apache Camel 2.15.1
  3. Spring 4.1.5.RELEASE
  4. Eclipse  as the IDE, version Luna 4.4.1.

Dependencies

We are just relying camel’s core components, the spring based components and the logger component in case you want to log something so our pom.xml consists of:

  1. camel-core – camel core components like timer, bean etc
  2. slf4j-api – in case you want to use log
  3. slf4j-log4j12 – if you want to use log4j as the slf4j implementation
  4. camel-stream – for printing the messages to console
  5. camel-mina to access Apache Mina component

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javarticles.camel</groupId>
	<artifactId>camelContentBasedRouting</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.15.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.12</version>
		</dependency>	
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-stream</artifactId>
			<version>2.15.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-mina</artifactId>
			<version>2.15.2</version>
		</dependency>			
	</dependencies>
</project>

Camel MINA URI

The mina: component is a transport for working with Apache MINA. URI options, A MINA component URI will look like this:

mina:transport://hostname[:port][?options]
mina:tcp://localhost:8999

Here we have used tcp transport, other valid transport types are udp, multicast/mcast, or vm.
Through the routing we have set up a TCP server on port 8999 using MINA. In order to receive information from the TCP server, client needs to connect to localhost:8999.
Based on the request one can set the response to the body, this will be returned back to the client.

exchange.getOut().setBody(body + "\nMina")

CamelMinaServer:

package com.javarticles.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelMinaServer {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new RouteBuilder() {
            public void configure() {
                from("mina:tcp://localhost:8999").process(
                        new Processor() {
                            public void process(Exchange exchange)
                                    throws Exception {
                                String body = exchange.getIn().getBody(
                                        String.class);
                                exchange.getOut().setBody(body + "\nMina");
                            }
                        });
            }
        });
        camelContext.start();
        for (;;) {
            Thread.sleep(1000);
        }
    }

}

Output:

15:32| INFO | MinaConsumer.java 68 | Binding to server address: localhost/127.0.0.1:8999 using acceptor: [email protected]
15:32| INFO | DefaultCamelContext.java 3164 | Route: route1 started and consuming from: Endpoint[mina://tcp://localhost:8999]
15:32| INFO | DefaultCamelContext.java 2453 | Total 1 routes, of which 1 is started.
15:32| INFO | DefaultCamelContext.java 2454 | Apache Camel 2.15.2 (CamelContext: camel-1) started in 0.304 seconds

The client code will simply send the request to mina:tcp://localhost:8999?sync=true and get the response. This synchronous mode of communication is set using sync=true. This means
that clients will be able to get a response back from the server.

ProducerTemplate template = camelContext.createProducerTemplate();
System.out.println(template.requestBody("direct:start", "Hello"));

direct:start will re-direct the request to mina:tcp://localhost:8999.

CamelMinaClient:

package com.javarticles.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelMinaClient {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:start")
                   .to("mina:tcp://localhost:8999?sync=true");
                }
            });
            camelContext.start();
            ProducerTemplate template = camelContext.createProducerTemplate();
            System.out.println(template.requestBody("direct:start", "Hello"));
            Thread.sleep(5000);
        } finally {
            camelContext.stop();
        }
    }

}

The server appends a new line and ‘Mina’ to the request sent.

Output:

15:32| INFO | DefaultCamelContext.java 3164 | Route: route1 started and consuming from: Endpoint[direct://start]
15:32| INFO | DefaultCamelContext.java 2453 | Total 1 routes, of which 1 is started.
15:32| INFO | DefaultCamelContext.java 2454 | Apache Camel 2.15.2 (CamelContext: camel-1) started in 0.300 seconds
Hello
Mina
15:32| INFO | DefaultCamelContext.java 2660 | Apache Camel 2.15.2 (CamelContext: camel-1) is shutting down
15:32| INFO | DefaultShutdownStrategy.java 184 | Starting to graceful shutdown 1 routes (timeout 300 seconds)
15:32| INFO | DefaultShutdownStrategy.java 647 | Route: route1 shutdown complete, was consuming from: Endpoint[direct://start]

Download the source code

This was an example about Apache Camel MINA component.

You can download the source code here: camelMinaComponentExample.zip
Share.

Comments are closed.