The netty component is a socket based Camel 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.
Start by creating a new Camel project with Maven:
mvn archetype:generate \ -DarchetypeGroupId=org.apache.camel.archetypes \ -DarchetypeArtifactId=camel-archetype-java \ -DarchetypeVersion=3.0.0 \ -DgroupId=com.mastertheboss.camel \ -DartifactId=camel-netty \ -Dversion=1.0-SNAPSHOT
You can use this project to create both producer and consumer endpoints. Let’s see start from the main Camel class:
package com.mastertheboss.camel; import org.apache.camel.main.Main; public class MainApp { public static void main(String...args) throws Exception { // use Camels Main class Main main = new Main(); main.addRouteBuilder(MyRouteBuilder.class); main.run(args); } }
And here is the code for the Route:
package com.mastertheboss.camel; import org.apache.camel.builder.RouteBuilder; public class MyRouteBuilder extends RouteBuilder { public void configure() { from("netty:tcp://localhost:8001?textline=true&sync=false") .to("log:?level=INFO&showBody=true"); } }
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:
<?xml version="1.0" encoding="UTF-8"?><project> <dependencyManagement> <dependencies> <!-- Camel BOM --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-parent</artifactId> <version>3.0.0</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-main</artifactId> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-netty</artifactId> </dependency> <!-- logging --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <scope>runtime</scope> </dependency> <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
Now you can add to your project 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 Cameln"); clientSocket.close(); } }
The expected outcome, is to find the message (“Hello Camel”), on the Camel output console.
NettyEventExecutorGroup] INFO Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel]
Source code for this tutorial: https://github.com/fmarchioni/masterspringboot/tree/master/camel/camel-netty