Skip to content

Commit

Permalink
fixbug
Browse files Browse the repository at this point in the history
  • Loading branch information
melin committed Dec 9, 2014
1 parent 07a5d34 commit 2c8d9c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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值
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,10 +22,10 @@ public class Netty4ClientHandler extends SimpleChannelInboundHandler<String> {

private static final Logger logger = LoggerFactory.getLogger(Netty4ClientHandler.class);

private final LinkedBlockingQueue<String> queue;
private final SynchronousQueue<String> queue;

public Netty4ClientHandler() {
queue = new LinkedBlockingQueue<String>();
queue = new SynchronousQueue<String>();
}

@Override
Expand All @@ -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;
}
}

0 comments on commit 2c8d9c3

Please sign in to comment.