2016-06-16 3 views
0

TCind를 통해 sendind LogRecord라는 응용 프로그램을 가지고 있는데이 로그를 캡처하고 싶습니다. 나는 Netty를 처음 접했고 LogRecord 객체를 읽을 수없는 몇 가지 예를 들려서 이야기했다.Netty로 LogRecord를 받으려면 어떻게해야합니까?

개체를 deserialize하기 위해 바이트 배열을 가져 오려고했지만 오류가 발생합니다. 누군가 나에게 좋은 모범이나 팁을 지적 할 수 있습니다. 여기

@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) { 

     } 
    } 
} 
관련 문제