Browse Source

增加日志

seyason 5 years ago
parent
commit
116498d4d1

+ 31 - 26
cloud-socket/src/com/js/kbt/socket/CustomDecoder.java

@@ -1,26 +1,31 @@
-package com.js.kbt.socket;
-
-import java.util.List;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.string.StringDecoder;
-
-public class CustomDecoder extends StringDecoder {
-	@Override
-	protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
-	    String HEXES = "0123456789ABCDEF";
-	    byte[] req = new byte[msg.readableBytes()];
-	    msg.readBytes(req);
-	    final StringBuilder hex = new StringBuilder(2 * req.length);
-
-	    for (int i = 0; i < req.length; i++) {
-	        byte b = req[i];
-	        hex.append(HEXES.charAt((b & 0xF0) >> 4))
-	                .append(HEXES.charAt((b & 0x0F)));
-	    }
-	    System.out.println("decode="+hex.toString());
-	    out.add(hex.toString());
-
-	}
-}
+package com.js.kbt.socket;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.string.StringDecoder;
+
+public class CustomDecoder extends StringDecoder {
+
+	private static final Logger logger = Logger.getLogger(CustomDecoder.class);
+
+	@Override
+	protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
+	    String HEXES = "0123456789ABCDEF";
+	    byte[] req = new byte[msg.readableBytes()];
+	    msg.readBytes(req);
+	    final StringBuilder hex = new StringBuilder(2 * req.length);
+	    logger.info("接收到Data");
+	    for (int i = 0; i < req.length; i++) {
+	        byte b = req[i];
+	        hex.append(HEXES.charAt((b & 0xF0) >> 4))
+	                .append(HEXES.charAt((b & 0x0F)));
+	    }
+	    logger.info("decode="+hex.toString());
+	    out.add(hex.toString());
+
+	}
+}

+ 82 - 78
cloud-socket/src/com/js/kbt/socket/HelloServerInitializer.java

@@ -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);
+	}
 }