gpt4 book ai didi

java - Netty Camel 示例

转载 作者:搜寻专家 更新时间:2023-11-01 02:31:27 24 4
gpt4 key购买 nike

我是 Netty 的新手。

我正在寻找一些 sample 。 (最好但不是必须使用 Camel Netty 组件和 Spring)

特别是一个使用 TCP 消息的示例 Netty 应用程序。

另外,我如何编写可以测试此 netty 应用程序的 JUnit 测试?

谢谢,达尔

最佳答案

我假设您仍想与 Camel 集成。我会先看看 camel documentation .在这让你感到沮丧之后,你将需要开始试验。我有一个例子,我创建了一个 Camel 处理器作为 Netty 服务器。 Netty 组件的工作原理是,From 端点是消费的服务器,To 端点是生产的客户端。我需要一个作为服务器的 To 端点,但该组件不支持它。我简单地将 Camel 处理器实现为一个 spring bean,它在初始化时启动了一个 Netty 服务器。 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;
}

关于java - Netty Camel 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8419245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com