From 2c8d9c3cc3f355996691ca763102e1909d929fb8 Mon Sep 17 00:00:00 2001 From: melin Date: Tue, 9 Dec 2014 11:05:54 +0800 Subject: [PATCH] fixbug --- .../client/PropertiesConfiguration.java | 8 +++++-- .../diamond/client/netty/Netty4Client.java | 4 ++++ .../client/netty/Netty4ClientHandler.java | 21 ++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/super-diamond-client/src/main/java/com/github/diamond/client/PropertiesConfiguration.java b/super-diamond-client/src/main/java/com/github/diamond/client/PropertiesConfiguration.java index e925592..473f4c9 100644 --- a/super-diamond-client/src/main/java/com/github/diamond/client/PropertiesConfiguration.java +++ b/super-diamond-client/src/main/java/com/github/diamond/client/PropertiesConfiguration.java @@ -57,6 +57,8 @@ public class PropertiesConfiguration extends EventSource { private static String _profile; private static String _modules; + private static final long FIRST_CONNECT_TIMEOUT = 2; + /** * 从jvm参数中获取 projCode、profile、host和port值 * @@ -139,14 +141,16 @@ protected void connectServer(String host, int port, final String projCode, final client = new Netty4Client(host, port, new ClientChannelInitializer(clientMsg)); if(client.isConnected()) { - String message = client.receiveMessage(); + String message = client.receiveMessage(FIRST_CONNECT_TIMEOUT); - if(message != null) { + if(StringUtils.isNotBlank(message)) { String versionStr = message.substring(0, message.indexOf("\r\n")); LOGGER.info("加载配置信息,项目编码:{},Profile:{}, Version:{}", projCode, profile, versionStr.split(" = ")[1]); FileUtils.saveData(projCode, profile, message); load(new StringReader(message), false); + } else { + throw new ConfigurationRuntimeException("从服务器端获取配置信息为空,Client 请求信息为:" + clientMsg); } } else { String message = FileUtils.readConfigFromLocal(projCode, profile); diff --git a/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4Client.java b/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4Client.java index 2a1ad7a..70a57da 100644 --- a/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4Client.java +++ b/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4Client.java @@ -78,6 +78,10 @@ public Netty4Client(String host, int port, ClientChannelInitializer channelIniti public String receiveMessage() { return channelInitializer.getClientHandler().getMessage(); } + + public String receiveMessage(long timeout) { + return channelInitializer.getClientHandler().getMessage(timeout); + } private void doOpen() throws Throwable { bootstrap = new Bootstrap(); diff --git a/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4ClientHandler.java b/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4ClientHandler.java index 31ef758..e02ff44 100644 --- a/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4ClientHandler.java +++ b/super-diamond-client/src/main/java/com/github/diamond/client/netty/Netty4ClientHandler.java @@ -7,7 +7,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; -import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,10 +22,10 @@ public class Netty4ClientHandler extends SimpleChannelInboundHandler { private static final Logger logger = LoggerFactory.getLogger(Netty4ClientHandler.class); - private final LinkedBlockingQueue queue; + private final SynchronousQueue queue; public Netty4ClientHandler() { - queue = new LinkedBlockingQueue(); + queue = new SynchronousQueue(); } @Override @@ -46,4 +47,18 @@ public String getMessage() { } return message; } + + /** + * + * @param timeout 超时时间,单位秒 + * @return + */ + public String getMessage(long timeout) { + String message = null; + try { + message = queue.poll(timeout, TimeUnit.SECONDS); + } catch (InterruptedException e) { + } + return message; + } } \ No newline at end of file