[英]How can I forward a message that received a server to another server in Netty (Java)?
我有一些客戶端,它們與一台服務器通信,我需要該服務器將消息轉發到另一台第二台服務器。 然后,從第二台服務器接收消息並發送給客戶端。
使用這種方法,我實現了連接到第二台服務器,但是它沒有收到消息,並引發了以下異常:
例外:java.nio.channels.NotYetConnectedException。 java.nio.channels.NotYetConnectedException
public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws IOException, Exception {
response = "hola" + "\r\n";
Main.creaLog("Mensaje recibido del conc: " + e.getMessage().toString());
Main.creaLog("Mensaje enviado al servidor : " + response);
ClientBootstrap bootstrap1 = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Configure the pipeline factory.
//bootstrap1.setPipelineFactory(new CLIENTE.ClientePipelineFactory());
bootstrap1.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(new ClienteHandler());
}
});
final ChannelFuture future = bootstrap1.connect(new InetSocketAddress("172.16.10.14", 12355));
Channel channel = future.getChannel();
if (channel.isWritable()) {
ChannelFuture lastWriteFuture = channel.write(e.getMessage().toString() + "\r\n");
}
close = true;
// We do not need to write a ChannelBuffer here.
// We know the encoder inserted at TelnetPipelineFactory will do the conversion.
ChannelFuture future = e.getChannel().write(response + "\r\n");
//CIERRA LA CONEXION
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
如果有人能幫助我,我非常感謝。
現在,您基本上正在嘗試在收到的每條消息上連接到遠程服務器。 這可能不是您想要的。 您可能只想連接到遠程服務器一次(即Netty代理示例中的出站通道),然后將新的傳入消息轉發到該特定通道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.