2016-06-16 5 views
0

私はTCindでSendind LogRecordというアプリケーションを持っており、このログを取りたいと思っています。私はNettyで新しく、いくつかの例で習った後、LogRecordオブジェクトを読むことができません。NettyでLogRecordを受信するにはどうすればよいですか?

オブジェクトを逆シリアル化するためにバイトの配列を取得しようとしましたが、エラーが発生します。誰かが私に良い事例やヒントを教えることができます。ここで

@Component 
@Qualifier("socketChannelInitializer") 
public class SocketChannelInitializer extends ChannelInitializer<SocketChannel> { 

    private static final ByteArrayDecoder DECODER = new ByteArrayDecoder(); 
    private static final ByteArrayEncoder ENCODER = new ByteArrayEncoder(); 

    @Autowired 
    @Qualifier("socketServerHandler") 
    private ChannelInboundHandlerAdapter socketServerHandler; 

    @Override 
    protected void initChannel(SocketChannel socketChannel) throws Exception { 
     ChannelPipeline pipeline = socketChannel.pipeline(); 

     // Add the text line codec combination first, 
     pipeline.addLast(new DelimiterBasedFrameDecoder(1024 * 1024, Delimiters.lineDelimiter())); 
     // the encoder and decoder are static as these are sharable 
     pipeline.addLast(DECODER); 
     pipeline.addLast(ENCODER); 

     pipeline.addLast(socketServerHandler); 
    } 
} 

はハンドラの一部である:ここで

はコードである

@Override 
protected void channelRead0(ChannelHandlerContext ctx, byte[] msg) throws Exception { 
    ByteBuffer byteBuffer = ByteBuffer.wrap(msg).asReadOnlyBuffer(); 

} 

答えて

0

の魔法は、このクラスである

public class LogRecordDecoder extends ByteToMessageDecoder { 
    @Override 
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { 

     LogRecord logRecord = null; 
     byte[] bytes = new byte[in.readableBytes()]; 
     int readerIndex = in.readerIndex(); 
     in.getBytes(readerIndex, bytes); 

     ObjectInputStream ois = null; 
     ByteArrayInputStream inn = new ByteArrayInputStream(bytes); 

     try { 
      ois = new ObjectInputStream(inn); 
      logRecord = (LogRecord) ois.readObject(); 
      out.add(logRecord); 
     } catch (Exception e) { 

     } 
    } 
} 
関連する問題