From 28020994bd4af4973e566e2f2667f965104187e2 Mon Sep 17 00:00:00 2001 From: "Kyle K. Lin" Date: Thu, 20 Dec 2018 14:29:10 +0800 Subject: [PATCH] add logs --- uia.comm/pom.xml | 2 +- .../src/main/java/uia/comm/SocketClient.java | 22 +++++++++---------- .../java/uia/comm/SocketDataController.java | 2 ++ .../src/main/java/uia/comm/SocketServer.java | 20 ++++++++++++----- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/uia.comm/pom.xml b/uia.comm/pom.xml index ca0465c..fdb4911 100644 --- a/uia.comm/pom.xml +++ b/uia.comm/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.uia.solution uia-comm - 0.3.1 + 0.3.2 jar uia-comm https://github.com/uia4j/uia-comm diff --git a/uia.comm/src/main/java/uia/comm/SocketClient.java b/uia.comm/src/main/java/uia/comm/SocketClient.java index 9a303ab..8c260ff 100644 --- a/uia.comm/src/main/java/uia/comm/SocketClient.java +++ b/uia.comm/src/main/java/uia/comm/SocketClient.java @@ -98,15 +98,15 @@ public SocketClient(final Protocol protocol, final Message * @param clientPort Client port. */ public SocketClient(final Protocol protocol, final MessageManager manager, String aliasName, int clientPort) { - this.clientPort = clientPort; - this.aliasName = aliasName; this.protocol = protocol; this.protocol.addMessageHandler(this); this.manager = manager; this.callIns = new HashMap>(); this.callOuts = new ConcurrentHashMap(); this.started = false; + this.aliasName = aliasName; this.maxCache = 20 * 1024; // 20K + this.clientPort = clientPort; } public int getMaxCache() { @@ -117,11 +117,6 @@ public void setMaxCache(int maxCache) { this.maxCache = Math.max(16, maxCache); } - @Override - public String getName() { - return this.aliasName; - } - /** * Get address. * @return Address. @@ -266,6 +261,11 @@ public synchronized boolean tryConnect() { } } + @Override + public String getName() { + return this.aliasName; + } + @Override public Protocol getProtocol() { return this.protocol; @@ -318,10 +318,10 @@ public boolean send(final byte[] data, int times) throws SocketException { return this.controller.send(data, times); } catch (Exception ex) { - logger.error(String.format("%s> send %s failed. ex:%s", + logger.error(String.format("%s> send %s failed", this.aliasName, - ByteUtils.toHexString(data, 100), - ex.getMessage())); + ByteUtils.toHexString(data, 100)), + ex); return false; } } @@ -369,7 +369,7 @@ public boolean send(final byte[] data, final MessageCallOut callOut, long timeou } @Override - public boolean send(final byte[] data, MessageCallOut callOut, long timeout, int retry) throws SocketException { + public boolean send(final byte[] data, final MessageCallOut callOut, long timeout, int retry) throws SocketException { if (!this.started) { throw new SocketException(this.aliasName + "> is not started."); } diff --git a/uia.comm/src/main/java/uia/comm/SocketDataController.java b/uia.comm/src/main/java/uia/comm/SocketDataController.java index 9798a57..a95298c 100644 --- a/uia.comm/src/main/java/uia/comm/SocketDataController.java +++ b/uia.comm/src/main/java/uia/comm/SocketDataController.java @@ -24,6 +24,7 @@ import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Arrays; +import java.util.Date; import java.util.Iterator; import org.apache.log4j.Logger; @@ -149,6 +150,7 @@ public void run() { } void lastUpdate() { + logger.info(this.name + "> lastUpdate: " + new Date()); this.lastUpdate = System.currentTimeMillis(); } diff --git a/uia.comm/src/main/java/uia/comm/SocketServer.java b/uia.comm/src/main/java/uia/comm/SocketServer.java index 0c2b591..f07af5f 100644 --- a/uia.comm/src/main/java/uia/comm/SocketServer.java +++ b/uia.comm/src/main/java/uia/comm/SocketServer.java @@ -112,7 +112,7 @@ public SocketServer(Protocol protocol, int port, MessageMa this.controllers = new ConcurrentHashMap(); this.listeners = new ArrayList(); - this.idleTime = 60000; + this.idleTime = 300000; this.port = port; this.maxCache = 20 * 1024; // 20K } @@ -316,7 +316,10 @@ public void run() { MessageCallOut out = callOutsRef.remove(tx); if (out != null) { try { - logger.info(String.format("%s> tx:%s callOut timeout", clientName, out.getTxId())); + logger.info(String.format("%s> %s> tx:%s callOut timeout", + SocketServer.this.aliasName, + clientName, + out.getTxId())); out.timeout(); } catch(Exception ex) { @@ -406,7 +409,7 @@ public void disconnect(String clientName) { this.clientCallouts.remove(clientName); if (controller != null) { - logger.info(String.format("%s> %s disconnected, count:%s", this.aliasName, clientName, this.controllers.size())); + logger.info(String.format("%s> %s> disconnected, count:%s", this.aliasName, clientName, this.controllers.size())); SelectionKey key = controller.getChannel().keyFor(this.serverSelector); if (key != null) { key.cancel(); @@ -583,6 +586,7 @@ public void messageError(ProtocolMonitor monitor, Protocol private void polling() { if (this.started) { + logger.info(this.aliasName + "> polling"); ArrayList keys = new ArrayList(); Collection controllers = this.controllers.values(); synchronized (this.controllers) { @@ -594,6 +598,9 @@ private void polling() { } for (String key : keys) { + logger.info(String.format("%s> %s> try to disconnect(polling)", + this.aliasName, + key)); disconnect(key); } } @@ -608,7 +615,7 @@ private void running() { this.serverSelector.select(); // wait NIO event. } catch (Exception ex) { - logger.error("comm> ", ex); + logger.error(this.aliasName + "> NIO failed", ex); continue; } @@ -631,6 +638,9 @@ private void running() { else if (key.isReadable()) { SocketDataController controller = (SocketDataController) key.attachment(); if (!controller.receive()) { + logger.info(String.format("%s> %s> try to disconnect(running)", + this.aliasName, + key)); disconnect(controller.getName()); } } @@ -678,7 +688,7 @@ private void clientConnected(SocketChannel client) { // use server selector client.register(this.serverSelector, SelectionKey.OP_READ, controller); - logger.info(String.format("%s> %s connected, count:%s", this.aliasName, clientId, this.controllers.size())); + logger.info(String.format("%s> %s> connected, count:%s", this.aliasName, clientId, this.controllers.size())); new Thread(new Runnable() {