我对内蒂来说是个新手。
我在找一些样品。(最好使用Camel Netty组件和Spring,但不是必须的)
特别是一个使用TCP消息的示例Netty应用程序。
另外,我如何编写一个JUnit测试来测试这个网络应用程序呢?
谢谢,达尔
发布于 2011-12-17 00:07:14
我假设您仍然希望与Camel集成。我首先看一下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;
}}
发布于 2015-01-27 14:07:59
camel发行版有很多示例,但没有针对netty组件的简单示例。
Netty组件可以用来设置一个socket服务器来消费消息并向客户端返回响应。在网上搜索了一段时间后,我创建了自己的 tutorial using netty component in camel作为一个简单的Camel-Netty hello world示例来展示:
https://stackoverflow.com/questions/8419245
复制相似问题