2017-11-17 11 views
0

私はnettyを使いすぎていません。ChannelPipelineのドキュメントについて質問があります。ここでは、それは正確に約あるものです:ビジネスロジックでnettyを使用

static final EventExecutorGroup group = new DefaultEventExecutorGroup(16); 
... 

ChannelPipeline pipeline = ch.pipeline(); 

pipeline.addLast("decoder", new MyProtocolDecoder()); 
pipeline.addLast("encoder", new MyProtocolEncoder()); 

// Tell the pipeline to run MyBusinessLogicHandler's event handler methods 
// in a different thread than an I/O thread so that the I/O thread is not blocked by 
// a time-consuming task. 
// If your business logic is fully asynchronous or finished very quickly, you don't 
// need to specify a group. 
pipeline.addLast(group, "handler", new MyBusinessLogicHandler()); 

私はかなりMyBusinessLogicHandlerクラスを理解していません。それはDuplexChannelHandlerまたは別の何かを実装する必要がありますか?

public class Packet{} 

は、私はいくつかのPacket生成MyBusinessLogicHandlerに与え、Packetにいくつかのバイトシーケンスをデコードします:たとえば、私の特定のケースでは、私は以下のクラスを持っています。それを再びバイトストリームにエンコードしてクライアントに送り返します。私は現在次のように表示しています:

public class MyBusinessLogicHandler extends SimpleChannelInboundHandler<MyModel>{ 
    public void channelRead0(ChannelHandlerContext ctx, Packet msg){ 
     Packet rslt = null; 
     //Do some complicated business logic 
     ctx.write(rslt); 
    } 
} 

これは一般的な方法であるかどうかはわかりません。どうぞお分かりですか?

答えて

2

Yeap。それはMyBusinessLogicHandlerを実装するための完全にうまく正しい方法です。あなたはすでにMyProtocolEncoder(私はChannelOutboundHandlerを実装していると思う)パイプラインのようにDuplexChannelHandlerは必要ありません。 ctx.write(rslt)に電話すると、発信ハンドラの書き込みイベントが発生します。

DuplexChannelHandlerは、エンコーダとデコーダの両方を同じクラスに実装したい場合にのみ便利です。たとえば、MyProtocolEncoderMyProtocolDecoderに参加することで可能です。

+1

ありがとうございました!ありがとう。 –

関連する問題