|
@@ -1,79 +1,83 @@
|
|
|
-package com.js.kbt.socket;
|
|
|
-
|
|
|
-import io.netty.channel.ChannelHandlerContext;
|
|
|
-import io.netty.channel.ChannelInitializer;
|
|
|
-import io.netty.channel.ChannelPipeline;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
-import io.netty.handler.codec.string.StringDecoder;
|
|
|
-import java.nio.charset.Charset;
|
|
|
-import javax.annotation.Resource;
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-@Service("helloServerInitializer")
|
|
|
-public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
|
|
|
-
|
|
|
- private static final int READ_IDEL_TIME_OUT = 5; // 读超时
|
|
|
- private static final int WRITE_IDEL_TIME_OUT = 10;// 写超时
|
|
|
- private static final int ALL_IDEL_TIME_OUT = 4; // 所有超时
|
|
|
-
|
|
|
-
|
|
|
- @Resource(name="appContextUtil")
|
|
|
- private ApplicationContextUtil appContextUtil;
|
|
|
-
|
|
|
- SocketChannel socketChannel;
|
|
|
-
|
|
|
- UserHandler userHandler;
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel arg0) throws Exception {
|
|
|
- System.out.println("创建channel==========");
|
|
|
-
|
|
|
- socketChannel = arg0;
|
|
|
- //ChannelPipeline 可以理解为消息传送通道 通道一旦建立 持续存在
|
|
|
- ChannelPipeline channelPipeline = arg0.pipeline();
|
|
|
-// channelPipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT,
|
|
|
-// WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS)); // 1
|
|
|
-// channelPipeline.addLast(new HeartbeatServerHandler());
|
|
|
-// ByteBuf delimiter = Unpooled.copiedBuffer("&_".getBytes());
|
|
|
-// channelPipeline.addLast("idleStateHandler", new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT,ALL_IDEL_TIME_OUT));
|
|
|
-// channelPipeline.addLast( new DelimiterBasedFrameDecoder(2048, delimiter));
|
|
|
- //为通道添加功能
|
|
|
- //字符串解码 编码
|
|
|
-// channelPipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
|
|
|
-// channelPipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
|
|
|
-
|
|
|
- channelPipeline.addLast("decoder",new CustomDecoder());
|
|
|
- channelPipeline.addLast("encoder", new StringDecoder(Charset.forName("UTF-8")));
|
|
|
- userHandler = getProcessHandler();
|
|
|
- //添加自主逻辑
|
|
|
- channelPipeline.addLast(userHandler);
|
|
|
- }
|
|
|
-
|
|
|
- private UserHandler getProcessHandler() {
|
|
|
- System.out.println("扫描bean===");
|
|
|
- ApplicationContext appContext=appContextUtil.getContext();
|
|
|
- return (UserHandler)appContext.getBean("userHandler");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
|
|
|
- throws Exception {
|
|
|
- super.exceptionCaught(ctx, cause);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
- // TODO Auto-generated method stub
|
|
|
- super.channelInactive(ctx);
|
|
|
- System.out.println("HelloServerInitialzer channelInactive");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
|
|
- // TODO Auto-generated method stub
|
|
|
- super.handlerRemoved(ctx);
|
|
|
- }
|
|
|
+package com.js.kbt.socket;
|
|
|
+
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
+import io.netty.channel.ChannelInitializer;
|
|
|
+import io.netty.channel.ChannelPipeline;
|
|
|
+import io.netty.channel.socket.SocketChannel;
|
|
|
+import io.netty.handler.codec.string.StringDecoder;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+@Service("helloServerInitializer")
|
|
|
+public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
|
|
|
+
|
|
|
+ private static final int READ_IDEL_TIME_OUT = 5; // 读超时
|
|
|
+ private static final int WRITE_IDEL_TIME_OUT = 10;// 写超时
|
|
|
+ private static final int ALL_IDEL_TIME_OUT = 4; // 所有超时
|
|
|
+
|
|
|
+ private static final Logger logger = Logger.getLogger(HelloServerInitializer.class);
|
|
|
+
|
|
|
+
|
|
|
+ @Resource(name="appContextUtil")
|
|
|
+ private ApplicationContextUtil appContextUtil;
|
|
|
+
|
|
|
+ SocketChannel socketChannel;
|
|
|
+
|
|
|
+ UserHandler userHandler;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void initChannel(SocketChannel arg0) throws Exception {
|
|
|
+ logger.info("创建channel==========");
|
|
|
+
|
|
|
+ socketChannel = arg0;
|
|
|
+ //ChannelPipeline 可以理解为消息传送通道 通道一旦建立 持续存在
|
|
|
+ ChannelPipeline channelPipeline = arg0.pipeline();
|
|
|
+// channelPipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT,
|
|
|
+// WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS)); // 1
|
|
|
+// channelPipeline.addLast(new HeartbeatServerHandler());
|
|
|
+// ByteBuf delimiter = Unpooled.copiedBuffer("&_".getBytes());
|
|
|
+// channelPipeline.addLast("idleStateHandler", new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT,ALL_IDEL_TIME_OUT));
|
|
|
+// channelPipeline.addLast( new DelimiterBasedFrameDecoder(2048, delimiter));
|
|
|
+ //为通道添加功能
|
|
|
+ //字符串解码 编码
|
|
|
+// channelPipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
|
|
|
+// channelPipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
|
|
|
+
|
|
|
+ channelPipeline.addLast("decoder",new CustomDecoder());
|
|
|
+ channelPipeline.addLast("encoder", new StringDecoder(Charset.forName("UTF-8")));
|
|
|
+ userHandler = getProcessHandler();
|
|
|
+ //添加自主逻辑
|
|
|
+ channelPipeline.addLast(userHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserHandler getProcessHandler() {
|
|
|
+ System.out.println("扫描bean===");
|
|
|
+ ApplicationContext appContext=appContextUtil.getContext();
|
|
|
+ return (UserHandler)appContext.getBean("userHandler");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
|
|
|
+ throws Exception {
|
|
|
+ super.exceptionCaught(ctx, cause);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ super.channelInactive(ctx);
|
|
|
+ System.out.println("HelloServerInitialzer channelInactive");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ super.handlerRemoved(ctx);
|
|
|
+ }
|
|
|
}
|