Java Netty 4.0-向不同EventLoop上的通道发送数据
我不知道怎么做,我认为我的方法是错误的-有人能给我一个提示吗 我制作了一个与示例非常相似的代理服务器 . 其目的不是转储流量,而是通过代理操作数据解析,这一部分工作得非常完美。让我们称之为代理部分 在同一个程序中,我启动了另一个线程,在另一个端口上使用另一个ServerBootstrap进行监听,这有它自己的eventloop等。当我在这个监听端口上收到一些东西时,我想将这些数据发送到代理部分的一个通道,我想动态地更改这个通道。当我将数据发送到其中一个代理通道时,会出现以下错误: 2013年4月29日晚上10:05:10后端列表Handler例外 警告:来自下游的意外异常。 java.lang.IllegalStateException:从eventLoop外部调用NextToutBoundByteBuffer()Java Netty 4.0-向不同EventLoop上的通道发送数据,java,netty,Java,Netty,我不知道怎么做,我认为我的方法是错误的-有人能给我一个提示吗 我制作了一个与示例非常相似的代理服务器 . 其目的不是转储流量,而是通过代理操作数据解析,这一部分工作得非常完美。让我们称之为代理部分 在同一个程序中,我启动了另一个线程,在另一个端口上使用另一个ServerBootstrap进行监听,这有它自己的eventloop等。当我在这个监听端口上收到一些东西时,我想将这些数据发送到代理部分的一个通道,我想动态地更改这个通道。当我将数据发送到其中一个代理通道时,会出现以下错误: 2013年4月
@Sharable
public class BackendListenHandler extends ChannelInboundByteHandlerAdapter {
private Channel outboundChannel;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.read();
ctx.flush();
}
@Override
public void inboundBufferUpdated(final ChannelHandlerContext ctx, ByteBuf in) throws Exception {
outboundChannel = Proxy.connectionTable.frontendListenChannel;
if (!outboundChannel.isActive()) {
System.out.println("channel id=" + outboundChannel.id() + " is NOT active...");
} else if (outboundChannel.isActive()) {
ByteBuf out = outboundChannel.outboundByteBuffer();
out.writeBytes(in);
outboundChannel.flush().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// was able to flush out data, start to read the next chunk
ctx.channel().read();
} else {
future.channel().close();
}
}
});
}
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (outboundChannel != null) {
closeOnFlush(outboundChannel);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.log(
Level.WARNING,
"Unexpected exception from downstream.", cause);
ctx.close();
}
static void closeOnFlush(Channel ch) {
if (ch.isActive()) {
ch.flush().addListener(ChannelFutureListener.CLOSE);
}
}
}
为了进行测试,我保留并更改此静态变量中的代理通道:
Proxy.connectionTable.frontendListenChannel;
在为“出站”连接创建引导时使用相同的EventLoop,如HexDump示例所示。这将确保从相同的IO线程处理所有内容。 见[1] [1] 我没有使用“outboundChannel.outboundByteBuffer()”——而是使用“unsoled.copiedBuffer()”从另一个EventLoop向通道写入字节
@Override
public void inboundBufferUpdated(final ChannelHandlerContext ctx, ByteBuf in) throws Exception {
outboundChannel = null;
if (Proxy.connectionTable.channelId != 0) {
outboundChannel = Proxy.allChannels.find(Proxy.connectionTable.channelId);
if (outboundChannel.isActive()) {
System.out.println("NOTIFY channel id=" + outboundChannel.id() + " is active...");
Rewrite rewrite = new Rewrite(byteBufConverter.byteBufToString(in), 2);
in.clear();
in = byteBufConverter.stringToByteBuf(rewrite.getFixedMessage());
outboundChannel.write(in);
outboundChannel.flush().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
ctx.channel().read();
} else {
future.channel().close();
}
}
});
}
}
}
其中“byteBufConverter.stringToByteBuf(rewrite.getFixedMessage())”返回“unmooled.copiedBuffer(strFixed.getBytes())”我无法在HexDumpProxyFrontendHandler.java中的channelActive中启动新的服务器引导,因为每次有人试图连接到HexDumpProxyFrontendHandler时,我都会启动服务器引导。我不认为这是可能的?我的意思是,每次在HexDumpProxyFrontendHandler上建立连接时,我不需要启动新的服务器引导程序。我不遵循。。。您不会每次都启动一个新的服务器引导。您要做的是每次构造一个新的引导,这很好,因为它是轻量级的,并且与ServerBootstrap共享EventLoopGroup。这样就不需要额外的资源。我可能认为我对情况的解释不是很清楚。实际上,我在两个不同的tcp端口上监听,并在那里启动两个ServerBootstrap。当客户端在第一个端口上建立连接时,就会建立代理—就像HexDumpProxy中使用引导和共享EventLoopGroup的HexDumpProxy一样。有时,第三个源连接到第二个列表端口并建立连接。我需要通过客户端代理出站将此入站数据发送到客户端。不管怎样,我想我现在。。。谢谢你的帮助:-)我将发布我所做的。。。