From d4fbefefac456ce5c2d26805dd4e5f4b6eae1ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=97=E7=A3=8A?= Date: Fri, 22 Jan 2016 11:15:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84netty=20cli?= =?UTF-8?q?ent=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/shinemo/mpush/cs/client/Main.java | 6 +----- .../com/shinemo/mpush/netty/client/NettyClientFactory.java | 4 +++- .../main/java/com/shinemo/mpush/ps/GatewayClientManage.java | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java index 4e28b5d8..4ba91304 100644 --- a/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java +++ b/mpush-cs/src/main/java/com/shinemo/mpush/cs/client/Main.java @@ -21,13 +21,9 @@ public static void main(String[] args) throws Exception { int index = (int) ((Math.random() % serverList.size()) * serverList.size()); ConnectionServerApplication server = serverList.get(index); - List clientList = Lists.newArrayList(); - for(int i = 0;i<100;i++){ ClientChannelHandler handler = new ClientChannelHandler(); - - final Client client = NettyClientFactory.INSTANCE.get(server.getIp(), server.getPort(), handler, true); - clientList.add(client); + NettyClientFactory.INSTANCE.createClient(server.getIp(), server.getPort(), handler, true); } LockSupport.park(); diff --git a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java index ad8a9e68..56efa29f 100644 --- a/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java +++ b/mpush-netty/src/main/java/com/shinemo/mpush/netty/client/NettyClientFactory.java @@ -26,7 +26,7 @@ public class NettyClientFactory { private final Map channel2Client = Maps.newConcurrentMap(); - public void createClient(String host, int port, final ChannelHandler handler,boolean security) { + public Client createClient(String host, int port, final ChannelHandler handler,boolean security) { final Bootstrap bootstrap = new Bootstrap(); EventLoopGroup workerGroup = new NioEventLoopGroup(); bootstrap.group(workerGroup)// @@ -59,10 +59,12 @@ public void initChannel(SocketChannel ch) throws Exception { client = new NettyClient(host,port, channel); } channel2Client.put(channel, client); + return client; } else { future.cancel(true); future.channel().close(); log.warn("[remoting] failure to connect:" + host+","+port); + return null; } } diff --git a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java index 26a97574..ca97c8e7 100644 --- a/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java +++ b/mpush-ps/src/main/java/com/shinemo/mpush/ps/GatewayClientManage.java @@ -19,7 +19,7 @@ public class GatewayClientManage extends GatewayServerManage{ public void addOrUpdate(String fullPath, GatewayServerApplication application) { super.addOrUpdate(fullPath, application); try{ - Client client = NettyClientFactory.INSTANCE.get(application.getIp(), application.getPort(), new GatewayClientChannelHandler(), false); + Client client = NettyClientFactory.INSTANCE.createClient(application.getIp(), application.getPort(), new GatewayClientChannelHandler(), false); application2Client.put(application, client); ip2Client.put(application.getIp()+":"+application.getPort(), client); }catch(Exception e){