我如何使用 Netty 接收 LogRecord?
How can I receive LogRecord with Netty?
我有一个通过 TCP 发送 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();
}
魔法就在这class
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) {
}
}
}
我有一个通过 TCP 发送 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();
}
魔法就在这class
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) {
}
}
}