我是Netty的新手。Netty駱駝樣本
我在查找一些樣品。 (最好但不必使用駱駝Netty組件和彈簧)
特別是一個消耗TCP消息的示例Netty應用程序。
另外我該如何編寫一個可以測試這個netty應用程序的JUnit測試?
感謝, 達累斯薩拉姆
我是Netty的新手。Netty駱駝樣本
我在查找一些樣品。 (最好但不必使用駱駝Netty組件和彈簧)
特別是一個消耗TCP消息的示例Netty應用程序。
另外我該如何編寫一個可以測試這個netty應用程序的JUnit測試?
感謝, 達累斯薩拉姆
我想你還是想用駱駝整合。我會先看看camel documentation。之後,你會感到挫敗,你將需要開始嘗試。我有一個例子,我創建了一個Camel處理器作爲Netty服務器。 Netty組件的工作方式使得From端點是一個消耗服務器,To端點是一個產生的客戶端。我需要一個To端點作爲服務器,並且該組件不支持該端點。我只是將一個Camel處理器實現爲一個Spring bean,它在初始化時啓動了一個Netty Server。 JBoss Netty documentation and samples是非常好的。穿過它們是值得的。
這是我瘦下來的例子。這是一個服務器向所有連接的客戶端發送消息。如果你是新來的Netty我強烈建議通過我聯繫到上面的樣品會:
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;
}
}
駱駝版本有很多的例子,但沒有一個簡單的網狀部件。
Netty組件可用於設置套接字服務器以使用消息併產生迴應客戶端的響應。後在網絡上搜索了一段時間,我創造我自己的 tutorial using netty component in camel作爲一個簡單的駱駝Netty中的Hello World例子來說明: