Netty Camel Samples

I am new to Netty.

I am looking for some samples. (Preferred, but not required, using Camel Netty and Spring)

In particular, an example Netty application that uses TCP messages.

Also, how can I write a JUnit test that can test this network application?

Thanks Dar

+4
source share
2 answers

I assume you still want to integrate with Camel. First, I will review the camel documentation . After that, you will be disappointed, you will need to start experimenting. I have one example where I created Camel Processor as Netty Server. Netty components work so that the From endpoint is the server that consumes, and the To endpoint is the client that produces. I need the To endpoint, which was the server, and the component did not support this. I just implemented Camel Processor as a spring bean that started Netty Server when it was initialized. However, the JBoss Netty documentation and samples . It is worth overcoming them.

Here is my example. This is a server that sends a message to all connected clients. If you're new to Netty, I highly recommend going through the samples I linked above:

public class NettyServer implements Processor { private final ChannelGroup channelGroup = new DefaultChannelGroup(); private NioServerSocketChannelFactory serverSocketChannelFactory = null; private final ExecutorService executor = Executors.newCachedThreadPool(); private String listenAddress = "0.0.0.0"; // overridden by spring-osgi value private int listenPort = 51501; // overridden by spring-osgi value @Override public void process(Exchange exchange) throws Exception { byte[] bytes = (byte[]) exchange.getIn().getBody(); // send over the wire sendMessage(bytes); } public synchronized void sendMessage(byte[] message) { ChannelBuffer cb = ChannelBuffers.copiedBuffer(message); //writes to all clients connected. this.channelGroup.write(cb); } private class NettyServerHandler extends SimpleChannelUpstreamHandler { @Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { super.channelOpen(ctx, e); //add client to the group. NettyServer.this.channelGroup.add(e.getChannel()); } // Perform an automatic recon. @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { super.channelConnected(ctx, e); // do something here when a clien connects. } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { // Do something when a message is received... } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { // Log the exception/ } } private class PublishSocketServerPipelineFactory implements ChannelPipelineFactory { @Override public ChannelPipeline getPipeline() throws Exception { // need to set the handler. return Channels.pipeline(new NettyServerHandler()); } } // called by spring to start the server public void init() { try { this.serverSocketChannelFactory = new NioServerSocketChannelFactory(this.executor, this.executor); final ServerBootstrap serverBootstrap = new ServerBootstrap(this.serverSocketChannelFactory); serverBootstrap.setPipelineFactory(new PublishSocketServerPipelineFactory()); serverBootstrap.setOption("reuseAddress", true); final InetSocketAddress listenSocketAddress = new InetSocketAddress(this.listenAddress, this.listenPort); this.channelGroup.add(serverBootstrap.bind(listenSocketAddress)); } catch (Exception e) { } } // called by spring to shut down the server. public void destroy() { try { this.channelGroup.close(); this.serverSocketChannelFactory.releaseExternalResources(); this.executor.shutdown(); } catch (Exception e) { } } // injected by spring public void setListenAddress(String listenAddress) { this.listenAddress = listenAddress; } // injected by spring public void setListenPort(int listenPort) { this.listenPort = listenPort; } 

}

+5
source

There are many examples in the camel release, but without a simple component for netty.

The Netty component can be used to configure the socket server to use the message and receive a response to the client. After some time searching the Internet, I create my own tutorial using the camel's netty component as a simple Camel-Netty camel example to show:

  • Using a camety netty to receive a TCP message
  • Using the POJO class to process a received message and create a response
  • Sending a response to the client.
0
source

Source: https://habr.com/ru/post/1385163/


All Articles