diff --git a/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java b/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java index 90a0f11f7..bdd80a90c 100644 --- a/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java +++ b/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java @@ -26,6 +26,6 @@ public interface BenchmarkService { public Map getUserTypes(List uids); - public long[] getLastStausIds(long[] uids); + public long[] getLastStatusIds(long[] uids); } diff --git a/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java b/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java index f299d4193..980fea028 100644 --- a/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java +++ b/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java @@ -36,7 +36,7 @@ public Map getUserTypes(List uids) { } @Override - public long[] getLastStausIds(long[] uids) { + public long[] getLastStatusIds(long[] uids) { return new long[0]; } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java index 9b64e58bc..3c5706f8d 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java @@ -22,7 +22,6 @@ import com.weibo.api.motan.util.CollectionUtil; import com.weibo.api.motan.util.LoggerUtil; import com.weibo.api.motan.util.MathUtil; - import org.apache.commons.lang3.StringUtils; import java.util.*; @@ -202,7 +201,7 @@ Referer next() { String group = randomKeyList.get(ThreadLocalRandom.current().nextInt(randomKeySize)); AtomicInteger ai = cursors.get(group); List> referers = groupReferers.get(group); - return referers.get(MathUtil.getPositive(ai.getAndIncrement()) % referers.size()); + return referers.get(MathUtil.getNonNegative(ai.getAndIncrement()) % referers.size()); } // 求最大公约数 diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java index 24ce21d2a..0e9136a22 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java @@ -16,17 +16,17 @@ package com.weibo.api.motan.cluster.loadbalance; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - import com.weibo.api.motan.common.MotanConstants; import com.weibo.api.motan.core.extension.SpiMeta; import com.weibo.api.motan.rpc.Referer; import com.weibo.api.motan.rpc.Request; import com.weibo.api.motan.util.MathUtil; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + /** * * Use consistent hash to choose referer @@ -89,7 +89,7 @@ private int getHash(Request request) { } else { hashcode = Arrays.hashCode(request.getArguments()); } - return MathUtil.getPositive(hashcode); + return MathUtil.getNonNegative(hashcode); } diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java index ecb3f0b6a..f02f9af16 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java @@ -16,14 +16,14 @@ package com.weibo.api.motan.cluster.loadbalance; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; - import com.weibo.api.motan.core.extension.SpiMeta; import com.weibo.api.motan.rpc.Referer; import com.weibo.api.motan.rpc.Request; import com.weibo.api.motan.util.MathUtil; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + /** * * Round robin loadbalance. @@ -40,7 +40,7 @@ public class RoundRobinLoadBalance extends AbstractLoadBalance { protected Referer doSelect(Request request) { List> referers = getReferers(); - int index = getNextPositive(); + int index = getNextNonNegative(); for (int i = 0; i < referers.size(); i++) { Referer ref = referers.get((i + index) % referers.size()); if (ref.isAvailable()) { @@ -54,7 +54,7 @@ protected Referer doSelect(Request request) { protected void doSelectToHolder(Request request, List> refersHolder) { List> referers = getReferers(); - int index = getNextPositive(); + int index = getNextNonNegative(); for (int i = 0, count = 0; i < referers.size() && count < MAX_REFERER_COUNT; i++) { Referer referer = referers.get((i + index) % referers.size()); if (referer.isAvailable()) { @@ -64,8 +64,8 @@ protected void doSelectToHolder(Request request, List> refersHolder) } } - // get positive int - private int getNextPositive() { - return MathUtil.getPositive(idx.incrementAndGet()); + // get non-negative int + private int getNextNonNegative() { + return MathUtil.getNonNegative(idx.incrementAndGet()); } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java b/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java index 019867151..2582959c7 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java +++ b/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java @@ -70,7 +70,7 @@ public ObjectInput createInput(InputStream in) { } } - protected static synchronized void initAllSerialziation() { + protected static synchronized void initAllSerialization() { if (serializations == null) { serializations = new ConcurrentHashMap(); try { @@ -89,9 +89,9 @@ protected static synchronized void initAllSerialziation() { } } - protected Serialization getSerializaiontByNum(int serializationNum) { + protected Serialization getSerializationByNum(int serializationNum) { if (serializations == null) { - initAllSerialziation(); + initAllSerialization(); } String name = serializations.get(serializationNum); Serialization s = null; diff --git a/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java b/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java index 9bd35d955..a77b38a8f 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java +++ b/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java @@ -54,7 +54,7 @@ public class MotanV2Codec extends AbstractCodec { static { - initAllSerialziation(); + initAllSerialization(); } @Override @@ -233,7 +233,7 @@ public Object decode(Channel channel, String remoteIp, byte[] data) throws IOExc body = ByteUtil.unGzip(body); } //默认自适应序列化 - Serialization serialization = getSerializaiontByNum(header.getSerialize()); + Serialization serialization = getSerializationByNum(header.getSerialize()); obj = new DeserializableObject(serialization, body); } if (header.isRequest()) { diff --git a/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java b/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java index 161a37c3b..a7ffd2325 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java +++ b/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java @@ -36,6 +36,6 @@ public class JdkProxyFactory implements ProxyFactory { @Override @SuppressWarnings("unchecked") public T getProxy(Class clz, List> clusters) { - return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{clz}, new RefererInvocationHandler<>(clz, clusters)); + return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[]{clz}, new RefererInvocationHandler<>(clz, clusters)); } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java b/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java index ca96537b2..a8fa88104 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java +++ b/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java @@ -16,16 +16,15 @@ package com.weibo.api.motan.registry.support.command; -import java.util.HashMap; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.commons.lang3.StringUtils; - import com.weibo.api.motan.registry.NotifyListener; import com.weibo.api.motan.registry.support.FailbackRegistry; import com.weibo.api.motan.rpc.URL; import com.weibo.api.motan.util.LoggerUtil; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; public abstract class CommandFailbackRegistry extends FailbackRegistry { @@ -93,7 +92,7 @@ protected List doDiscover(URL url) { finalResult = discoverService(urlCopy); } - LoggerUtil.info("CommandFailbackRegistry discover size: " + finalResult.size() + ", result:" + finalResult.toString()); + LoggerUtil.info("CommandFailbackRegistry discover size: " + (finalResult == null ? "0" : finalResult.size())); return finalResult; } diff --git a/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java b/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java index 3d913e999..20a6acfa7 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java +++ b/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java @@ -40,6 +40,14 @@ public class SimpleSerialization implements Serialization { @Override public byte[] serialize(Object obj) throws IOException { GrowableByteBuffer buffer = new GrowableByteBuffer(4096); + serialize(obj, buffer); + buffer.flip(); + byte[] result = new byte[buffer.remaining()]; + buffer.get(result); + return result; + } + + private void serialize(Object obj, GrowableByteBuffer buffer) throws IOException { if (obj != null) { if (obj instanceof String) { buffer.put((byte) 1); @@ -48,8 +56,9 @@ public byte[] serialize(Object obj) throws IOException { buffer.put(b); } else if (obj instanceof Map) { buffer.put((byte) 2); + int pos = buffer.position(); int size = 0; - buffer.position(5); + buffer.position(pos + 4); for (Entry entry : ((Map) obj).entrySet()) { if (entry.getKey() != null && entry.getValue() != null && (entry.getKey() instanceof String) && (entry.getValue() instanceof String)) { @@ -57,10 +66,10 @@ public byte[] serialize(Object obj) throws IOException { size += putString(buffer, (String) entry.getValue()); } } - buffer.position(1); + buffer.position(pos); buffer.putInt(size); - buffer.position(5 + size); - } else if(obj instanceof byte[]){ + buffer.position(pos + size + 4); + } else if (obj instanceof byte[]) { buffer.put((byte) 3); byte[] b = (byte[]) obj; buffer.putInt(b.length); @@ -71,15 +80,15 @@ public byte[] serialize(Object obj) throws IOException { } else { buffer.put((byte) 0); } - buffer.flip(); - byte[] result = new byte[buffer.remaining()]; - buffer.get(result); - return result; } @Override public T deserialize(byte[] bytes, Class clz) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(bytes); + return deserialize(buffer, clz); + } + + private T deserialize(ByteBuffer buffer, Class clz) throws IOException { byte type = buffer.get(); switch (type) { case 0: @@ -111,6 +120,7 @@ public T deserialize(byte[] bytes, Class clz) throws IOException { key = getString(buffer); } } + buffer.limit(buffer.capacity()); return (T) map; } else { throw new MotanServiceException("SimpleSerialization not support type:" + clz); @@ -125,30 +135,24 @@ public T deserialize(byte[] bytes, Class clz) throws IOException { @Override public byte[] serializeMulti(Object[] data) throws IOException { - if (data.length == 1) { - return serialize(data[0]); + GrowableByteBuffer buffer = new GrowableByteBuffer(4096); + for (Object o : data) { + serialize(o, buffer); } - //TODO mulit param support - throw new MotanServiceException("SimpleSerialization not support serialize multi Object"); + buffer.flip(); + byte[] result = new byte[buffer.remaining()]; + buffer.get(result); + return result; } @Override public Object[] deserializeMulti(byte[] data, Class[] classes) throws IOException { - if (classes.length == 1) { - return new Object[]{deserialize(data, classes[0])}; - } else { - StringBuilder sb = new StringBuilder(128); - sb.append("["); - for (Class c : classes) { - sb.append(c.getName()).append(","); - } - if (sb.length() > 1) { - sb.deleteCharAt(sb.length() - 1); - } - sb.append("]"); - throw new MotanServiceException("SimpleSerialization not support deserialize multi Object of " + classes); + ByteBuffer buffer = ByteBuffer.wrap(data); + Object[] result = new Object[classes.length]; + for (int i = 0; i < classes.length; i++) { + result[i] = deserialize(buffer, classes[i]); } - + return result; } @Override diff --git a/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java b/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java index af65619b8..9365178b0 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java +++ b/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java @@ -36,7 +36,7 @@ public class LocalSwitcherService implements SwitcherService { private static ConcurrentMap switchers = new ConcurrentHashMap(); - private Map> listenerMap = new ConcurrentHashMap(); + private Map> listenerMap = new ConcurrentHashMap<>(); @Override public Switcher getSwitcher(String name) { diff --git a/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java b/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java index 05f801281..c26597877 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java +++ b/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java @@ -84,7 +84,7 @@ private void createConnections() { } protected Channel getChannel() throws MotanServiceException { - int index = MathUtil.getPositive(idx.getAndIncrement()); + int index = MathUtil.getNonNegative(idx.getAndIncrement()); Channel channel; for (int i = index; i < connections + index; i++) { diff --git a/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java b/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java index 173143adf..d3319300f 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java +++ b/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java @@ -23,6 +23,15 @@ public class MathUtil { + + /** + * 针对int类型字符串进行解析,如果存在格式错误,则返回默认值(defaultValue) + * Parse intStr, return defaultValue when numberFormatException occurs + * + * @param intStr + * @param defaultValue + * @return + */ public static int parseInt(String intStr, int defaultValue) { try { return Integer.parseInt(intStr); @@ -32,6 +41,13 @@ public static int parseInt(String intStr, int defaultValue) { } } + /** + * 针对long类型字符串进行解析,如果存在格式错误,则返回默认值(defaultValue) + * Parse longStr, return defaultValue when numberFormatException occurs + * @param longStr + * @param defaultValue + * @return + */ public static long parseLong(String longStr, long defaultValue){ try { return Long.parseLong(longStr); @@ -39,13 +55,16 @@ public static long parseLong(String longStr, long defaultValue){ return defaultValue; } } - + /** - * return positive int value of originValue + * 通过二进制位操作将originValue转化为非负数: + * 0和正数返回本身 + * 负数通过二进制首位取反转化为正数或0(Integer.MIN_VALUE将转换为0) + * return non-negative int value of originValue * @param originValue * @return positive int */ - public static int getPositive(int originValue){ + public static int getNonNegative(int originValue){ return 0x7fffffff & originValue; } } diff --git a/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java b/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java index 2790a9c94..ef0c6e183 100644 --- a/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java +++ b/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java @@ -54,7 +54,7 @@ public void serialize() throws Exception { assertEquals(entry.getValue(), m2.get(entry.getKey())); } - byte[] bytes = new byte[]{2,34,12,24}; + byte[] bytes = new byte[]{2, 34, 12, 24}; b = serialization.serialize(bytes); assertNotNull(b); assertTrue(b.length > 0); @@ -67,5 +67,37 @@ public void serialize() throws Exception { } } + @Test + public void testSerializeMulti() throws Exception { + SimpleSerialization serialization = new SimpleSerialization(); + Object[] objects = new Object[3]; + objects[0] = "teststring"; + Map map = new HashMap(); + map.put("name", "ray"); + map.put("code", "xxx"); + objects[1] = map; + byte[] bytes = new byte[]{2, 34, 12, 24}; + objects[2] = bytes; + + byte[] b = serialization.serializeMulti(objects); + assertNotNull(b); + assertTrue(b.length > 0); + + Object[] result = serialization.deserializeMulti(b, new Class[]{String.class, Map.class, byte[].class}); + assertEquals(3, result.length); + assertTrue(result[0] instanceof String); + assertEquals(result[0], objects[0]); + assertTrue(result[1] instanceof Map); + Map map2 = (Map) result[1]; + for (Map.Entry entry : map.entrySet()) { + assertEquals(entry.getValue(), map2.get(entry.getKey())); + } + assertTrue(result[2] instanceof byte[]); + + byte[] nbytes = (byte[]) result[2]; + for (int i = 0; i < nbytes.length; i++) { + assertEquals(nbytes[i], bytes[i]); + } + } } \ No newline at end of file diff --git a/motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java b/motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java new file mode 100644 index 000000000..22d42a4f1 --- /dev/null +++ b/motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java @@ -0,0 +1,45 @@ +package com.weibo.api.motan.util; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class MathUtilTest { + + @Test + public void parseInt() { + + assertTrue(MathUtil.parseInt("-1", 0) == -1); + assertTrue(MathUtil.parseInt("0", -1) == 0); + assertTrue(MathUtil.parseInt("1", 0) == 1); + + assertTrue(MathUtil.parseInt("", 0) == 0); + assertTrue(MathUtil.parseInt(null, 0) == 0); + assertTrue(MathUtil.parseInt("Invalid Int String", 0) == 0); + + } + + @Test + public void parseLong() { + + assertTrue(MathUtil.parseLong("-1", 0) == -1); + assertTrue(MathUtil.parseLong("0", -1) == 0); + assertTrue(MathUtil.parseLong("1", 0) == 1); + + assertTrue(MathUtil.parseLong("", 0) == 0); + assertTrue(MathUtil.parseLong(null, 0) == 0); + assertTrue(MathUtil.parseLong("Invalid Int String", 0) == 0); + } + + @Test + public void getNonNegative() { + + assertTrue(MathUtil.getNonNegative(Integer.MIN_VALUE) == 0); + assertTrue(MathUtil.getNonNegative(-1) > 0); + + assertTrue(MathUtil.getNonNegative(0) == 0); + assertTrue(MathUtil.getNonNegative(1) == 1); + assertTrue(MathUtil.getNonNegative(Integer.MAX_VALUE) == Integer.MAX_VALUE); + + } +} \ No newline at end of file diff --git a/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java b/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java index 9a174bbfd..be881f6e6 100644 --- a/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java +++ b/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java @@ -212,13 +212,13 @@ protected String discoverCommand(URL url) { } private ConcurrentHashMap> lookupServiceUpdate(String group) { + ConcurrentHashMap> groupUrls = new ConcurrentHashMap>(); Long lastConsulIndexId = lookupGroupServices.get(group) == null ? 0 : lookupGroupServices.get(group); ConsulResponse> response = lookupConsulService(group, lastConsulIndexId); if (response != null) { List services = response.getValue(); if (services != null && !services.isEmpty() && response.getConsulIndex() > lastConsulIndexId) { - ConcurrentHashMap> groupUrls = new ConcurrentHashMap>(); for (ConsulService service : services) { try { URL url = ConsulUtils.buildUrl(service); @@ -239,7 +239,7 @@ private ConcurrentHashMap> lookupServiceUpdate(String group) { LoggerUtil.info(group + " no need update, lastIndex:" + lastConsulIndexId); } } - return null; + return groupUrls; } private String lookupCommandUpdate(String group) { diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java index fe7f63bb8..d94d86817 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java @@ -1,6 +1,5 @@ package com.weibo.api.motan.config.springsupport.util; -import com.weibo.api.motan.config.ProtocolConfig; import org.springframework.beans.factory.BeanFactory; import java.util.ArrayList; diff --git a/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java b/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java index 0683a45b6..6fed1aade 100644 --- a/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java +++ b/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java @@ -111,8 +111,9 @@ public synchronized boolean open() { return true; } + ChannelFuture channelFuture = null; try { - ChannelFuture channleFuture = nettyClient.getBootstrap().connect( + channelFuture = nettyClient.getBootstrap().connect( new InetSocketAddress(nettyClient.getUrl().getHost(), nettyClient.getUrl().getPort())); long start = System.currentTimeMillis(); @@ -123,11 +124,11 @@ public synchronized boolean open() { MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR); } // 不去依赖于connectTimeout - boolean result = channleFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS); - boolean success = channleFuture.isSuccess(); + boolean result = channelFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS); + boolean success = channelFuture.isSuccess(); if (result && success) { - channel = channleFuture.getChannel(); + channel = channelFuture.getChannel(); if (channel.getLocalAddress() != null && channel.getLocalAddress() instanceof InetSocketAddress) { localAddress = (InetSocketAddress) channel.getLocalAddress(); } @@ -136,22 +137,25 @@ public synchronized boolean open() { return true; } boolean connected = false; - if(channleFuture.getChannel() != null){ - connected = channleFuture.getChannel().isConnected(); + if (channelFuture.getChannel() != null) { + connected = channelFuture.getChannel().isConnected(); } - if (channleFuture.getCause() != null) { - channleFuture.cancel(); + if (channelFuture.getCause() != null) { + channelFuture.cancel(); throw new MotanServiceException("NettyChannel failed to connect to server, url: " - + nettyClient.getUrl().getUri()+ ", result: " + result + ", success: " + success + ", connected: " + connected, channleFuture.getCause()); + + nettyClient.getUrl().getUri() + ", result: " + result + ", success: " + success + ", connected: " + connected, channelFuture.getCause()); } else { - channleFuture.cancel(); + channelFuture.cancel(); throw new MotanServiceException("NettyChannel connect to server timeout url: " + nettyClient.getUrl().getUri() + ", cost: " + (System.currentTimeMillis() - start) + ", result: " + result + ", success: " + success + ", connected: " + connected); } } catch (MotanServiceException e) { throw e; } catch (Exception e) { + if (channelFuture != null) { + channelFuture.getChannel().close(); + } throw new MotanServiceException("NettyChannel failed to connect to server, url: " + nettyClient.getUrl().getUri(), e); } finally {