2026-05-28 14:30:06 +08:00
|
|
|
package com.netty;
|
|
|
|
|
|
2026-05-28 14:58:00 +08:00
|
|
|
import com.haobang.config.AppConfig;
|
2026-05-28 14:30:06 +08:00
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
|
import io.netty.channel.socket.DatagramChannel;
|
|
|
|
|
import io.netty.handler.codec.string.StringDecoder;
|
|
|
|
|
import io.netty.handler.codec.string.StringEncoder;
|
2026-05-28 14:58:00 +08:00
|
|
|
import java.nio.charset.Charset;
|
2026-05-28 14:30:06 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* UDP 通道初始化器
|
|
|
|
|
*/
|
|
|
|
|
public class SyslogUdpChannelInitializer extends ChannelInitializer<DatagramChannel> {
|
2026-05-28 14:58:00 +08:00
|
|
|
private static final Charset SYSLOG_CHARSET = AppConfig.getSyslogCharset();
|
|
|
|
|
|
2026-05-28 14:30:06 +08:00
|
|
|
@Override
|
|
|
|
|
protected void initChannel(DatagramChannel ch) throws Exception {
|
|
|
|
|
ChannelPipeline pipeline = ch.pipeline();
|
|
|
|
|
|
2026-05-28 14:58:00 +08:00
|
|
|
// 添加字符串解码器和编码器(使用可配置的字符集)
|
|
|
|
|
pipeline.addLast("decoder", new StringDecoder(SYSLOG_CHARSET));
|
|
|
|
|
pipeline.addLast("encoder", new StringEncoder(SYSLOG_CHARSET));
|
2026-05-28 14:30:06 +08:00
|
|
|
|
|
|
|
|
// 添加业务处理器
|
|
|
|
|
pipeline.addLast("handler", new SyslogMessageHandler());
|
|
|
|
|
}
|
|
|
|
|
}
|