From 1ea816a60e01d02ab98bc2995625b4aed61294ba Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 29 Aug 2016 13:50:05 +0800 Subject: [PATCH] --- src/org/redkale/boot/Application.java | 37 ++++---------- src/org/redkale/boot/GroupInfo.java | 74 +++++++++++++++++++++++++++ src/org/redkale/boot/NodeServer.java | 12 +++-- 3 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 src/org/redkale/boot/GroupInfo.java diff --git a/src/org/redkale/boot/Application.java b/src/org/redkale/boot/Application.java index 7f4e48d71..f904a0133 100644 --- a/src/org/redkale/boot/Application.java +++ b/src/org/redkale/boot/Application.java @@ -65,11 +65,7 @@ public final class Application { final Map globalNodes = new HashMap<>(); - final Map> globalGroups = new HashMap<>(); - - final Map globalGroupKinds = new HashMap<>(); - - final Map globalGroupProtocols = new HashMap<>(); + final Map globalGroups = new HashMap<>(); final InetAddress localAddress; @@ -79,8 +75,6 @@ public final class Application { final List servers = new CopyOnWriteArrayList<>(); - CountDownLatch servicecdl; //会出现两次赋值 - final ObjectPool transportBufferPool; final ExecutorService transportExecutor; @@ -89,6 +83,8 @@ public final class Application { final ResourceFactory resourceFactory = ResourceFactory.root(); + CountDownLatch servicecdl; //会出现两次赋值 + //-------------------------------------------------------------------------------------------- private final boolean singletonrun; @@ -333,16 +329,14 @@ public final class Application { if (!"TCP".equalsIgnoreCase(protocol) && !"UDP".equalsIgnoreCase(protocol)) { throw new RuntimeException("Not supported Transport Protocol " + conf.getValue("protocol")); } - Set addrs = globalGroups.get(group); - if (addrs == null) { - addrs = new LinkedHashSet<>(); - globalGroupProtocols.put(group, protocol); - globalGroupKinds.put(group, conf.getValue("kind", "")); - globalGroups.put(group, addrs); + GroupInfo ginfo = globalGroups.get(group); + if (ginfo == null) { + ginfo = new GroupInfo(group, protocol, conf.getValue("kind", ""), new LinkedHashSet<>()); + globalGroups.put(group, ginfo); } for (AnyValue node : conf.getAnyValues("node")) { final InetSocketAddress addr = new InetSocketAddress(node.getValue("addr"), node.getIntValue("port")); - addrs.add(addr); + ginfo.addrs.add(addr); String oldgroup = globalNodes.get(addr); if (oldgroup != null) throw new RuntimeException(addr + " had one more group " + (globalNodes.get(addr))); globalNodes.put(addr, group); @@ -575,20 +569,9 @@ public final class Application { return null; } - String findGroupProtocol(String group) { + GroupInfo findGroupInfo(String group) { if (group == null) return null; - return globalGroupProtocols.get(group); - } - - String findGroupKind(String group) { - if (group == null) return null; - return globalGroupKinds.get(group); - } - - Set findGlobalGroup(String group) { - if (group == null) return null; - Set set = globalGroups.get(group); - return set == null ? null : new LinkedHashSet<>(set); + return globalGroups.get(group); } private void shutdown() throws Exception { diff --git a/src/org/redkale/boot/GroupInfo.java b/src/org/redkale/boot/GroupInfo.java new file mode 100644 index 000000000..612f23724 --- /dev/null +++ b/src/org/redkale/boot/GroupInfo.java @@ -0,0 +1,74 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.boot; + +import java.net.InetSocketAddress; +import java.util.*; + +/** + * + *

+ * 详情见: http://redkale.org + * + * @author zhangjx + */ +public class GroupInfo { + + protected String name; + + protected String protocol; + + protected String kind; + + protected Set addrs; + + public GroupInfo() { + } + + public GroupInfo(String name, String protocol, String kind, Set addrs) { + this.name = name; + this.protocol = protocol; + this.kind = kind; + this.addrs = addrs; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public String getKind() { + return kind; + } + + public void setKind(String kind) { + this.kind = kind; + } + + public Set getAddrs() { + return addrs; + } + + public Set copyAddrs() { + return addrs == null ? null : new LinkedHashSet<>(addrs); + } + + public void setAddrs(Set addrs) { + this.addrs = addrs; + } + +} diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index 99ed70e51..bd83a305c 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -402,8 +402,9 @@ public abstract class NodeServer { Set addrs = new HashSet(); transports.forEach(t -> addrs.addAll(Arrays.asList(t.getRemoteAddresses()))); Transport first = transports.get(0); - Transport newTransport = new Transport(groupid, application.findGroupProtocol(first.getName()), application.getWatchFactory(), - application.findGroupKind(first.getName()), application.transportBufferPool, application.transportChannelGroup, this.sncpAddress, addrs); + GroupInfo ginfo = application.findGroupInfo(first.getName()); + Transport newTransport = new Transport(groupid, ginfo.getProtocol(), application.getWatchFactory(), + ginfo.getKind(), application.transportBufferPool, application.transportChannelGroup, this.sncpAddress, addrs); synchronized (application.resourceFactory) { transport = application.resourceFactory.find(groupid, Transport.class); if (transport == null) { @@ -425,10 +426,11 @@ public abstract class NodeServer { } return transport; } - Set addrs = application.findGlobalGroup(group); + GroupInfo ginfo = application.findGroupInfo(group); + Set addrs = ginfo.copyAddrs(); if (addrs == null) throw new RuntimeException("Not found = " + group + " on "); - transport = new Transport(group, application.findGroupProtocol(group), application.getWatchFactory(), - application.findGroupKind(group), application.transportBufferPool, application.transportChannelGroup, this.sncpAddress, addrs); + transport = new Transport(group, ginfo.getProtocol(), application.getWatchFactory(), + ginfo.getKind(), application.transportBufferPool, application.transportChannelGroup, this.sncpAddress, addrs); application.resourceFactory.register(group, transport); } return transport;