Using Camel Netty Components to manage socket routes

The netty component is a socket based Caamel communication component, which relies on the Netty project. Netty is a client server framework designed around Java NIO API which enables fast and simple development of network applications either servers or clients. The advantage of using Netty is that it greatly simplifies network programming by means of API running on the top of TCP and UDP protocols.

You can use this component to create both producer and consumer endpoints. Let’s see an example in Java DSL.

package com.sample;

 
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

 
public class TCPServer {

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.addRouteBuilder(new MyRouteTCPServer());
        main.run(args);
    }
}

class MyRouteTCPServer extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("netty:tcp://localhost:8001?textline=true&sync=false").to("stream:out");

    }
}

The above example starts a TCP server listening on port 8001, using a text based format (textline=true), setting the endpoint as request-response communication (sync=false). In order to compile and execute the above Camel class, you will need netty dependencies in your project:

<dependencies>
	<dependency>
	  <groupId>org.apache.camel</groupId>
	  <artifactId>camel-core</artifactId>
	  <version>${camel.version}</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.camel</groupId>
	  <artifactId>camel-spring</artifactId>
	  <version>${camel.version}</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.camel</groupId>
	  <artifactId>camel-netty</artifactId>
	  <version>${camel.version}</version>
	</dependency>
</dependencies>

Now you can add a simple Java Client class to send some sample messages to our Server:

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String argv[]) throws Exception {

        Socket clientSocket = new Socket("localhost", 8001);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        outToServer.writeBytes("Hello Camel\n");

        clientSocket.close();
    }
}

The expected outcome, is to find the message (“Hello Camel”), on the Camel output console.


Recommended Articles

Integrate Apache Camel in WildFly Application Server for Mini Service Bus

Learn how to integrate Apache Camel into your WildFly application server and turn it into a mini service bus. Get started with the WildFly-camel bundle and configure routes to move files between folders.

Migrate JBoss ESB Services to Apache Camel: A Step-by-Step Guide

Learn how to migrate a service from JBoss ESB to Apache Camel, including key benefits and migration steps. #Java #Middleware #CloudNative

Using Camel with WildFly 31 - A Step-by-Step Guide to CDI Integration

Learn how to integrate Apache Camel with WildFly using Java EE CDI, including creating a Bootstrap class and adding CDI annotated beans to your routes.

Invoke jBPM 6 Process from Camel Spring: A Step-by-Step Guide

Learn how to integrate jBPM 6 with Apache Camel using the jbpm component and interact with your business process management instance over REST.