2017-04-02 3 views
0

NettyクライアントからXMLメッセージを受信するサーバーでnetty 4.1.9を使用しています。クライアントはxmlメッセージをサーバーに送信できます。しかし、サーバー側では、一連のバイトではなく、単一のxmlメッセージとしてデコードできる必要があります。私はXMLフレームデコーダーを見ていましたが、最良の方法を理解できませんでした。正しい方向を指していることを感謝します。nettyを使用して4.1.9 xmlメッセージ処理用

イニシャライザ:

@Override 
    public void initChannel(SocketChannel ch) throws Exception { 
     log.info("init channel called"); 
     ChannelPipeline pipeline = ch.pipeline(); 
     //add decoder for combining bytes for xml message 
     pipeline.addLast("decoder", new XmlMessageDecoder()); 

     // handler for business logic. 
     pipeline.addLast("handler", new XmlServerHandler()); 
} 

Iは、XMLフレーム復号器を使用することができませんでした。 mxlメッセージデコーダでxmlフレームデコーダを拡張しようとすると、 "xmlframedecoderで利用可能なデフォルトのコンストラクタがありません"というコンパイルエラーが発生します。

答えて

0

私はチャネルの初期化子でXmlFrameDecoderを使用して終了しました。その出力は、ByteBufからXMLメッセージを読み取ったハンドラに渡されました。

イニシャライザ

@Override 
public void initChannel(SocketChannel ch) throws Exception { 
    ChannelPipeline pipeline = ch.pipeline(); 

    // idle state handler 
    pipeline.addLast("idleStateHandler", new IdleStateHandler(60, 
      30, 0)); 
    pipeline.addLast("myHandler", new IdleHandler()); 

    //add decoder for combining bytes for xml message 
    pipeline.addLast("decoder", new XmlFrameDecoder(1048576)); 

    // handler for business logic. 
    pipeline.addLast("handler", new ServerReceiverHandler()); 

ハンドラ

パブリッククラスServerReceiverHandlerは{

ChannelHandlerContext ctx; 

@Override 
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
    final ByteBuf buffer = (ByteBuf)msg; 
    //prints out String representation of xml doc 
    log.info("read : {}" + buffer.toString((CharsetUtil.UTF_8))); 
    ReferenceCountUtil.release(msg); 
} 
ChannelInboundHandlerAdapterを拡張します
関連する問題