use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class RegistryDirectoryTest method testDestroy.
/**
* When destroying, RegistryDirectory should: 1. be disconnected from Registry 2. destroy all invokers
*/
@Test
public void testDestroy() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));
registryDirectory.notify(serviceUrls);
List<Invoker> invokers = registryDirectory.list(invocation);
Assert.assertEquals(true, registryDirectory.isAvailable());
Assert.assertEquals(true, invokers.get(0).isAvailable());
registryDirectory.destroy();
Assert.assertEquals(false, registryDirectory.isAvailable());
Assert.assertEquals(false, invokers.get(0).isAvailable());
registryDirectory.destroy();
Map<String, List<Invoker<RegistryDirectoryTest>>> methodInvokerMap = registryDirectory.getMethodInvokerMap();
Map<String, Invoker<RegistryDirectoryTest>> urlInvokerMap = registryDirectory.getUrlInvokerMap();
Assert.assertTrue(methodInvokerMap == null);
Assert.assertEquals(0, urlInvokerMap.size());
// List<U> urls = mockRegistry.getSubscribedUrls();
RpcInvocation inv = new RpcInvocation();
try {
registryDirectory.list(inv);
fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("already destroyed"));
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class RegistryDirectoryTest method testEmptyNotifyCauseForbidden.
/**
* Empty notify cause forbidden, non-empty notify cancels forbidden state
*/
@Test
public void testEmptyNotifyCauseForbidden() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List invokers = null;
List<URL> serviceUrls = new ArrayList<URL>();
registryDirectory.notify(serviceUrls);
RpcInvocation inv = new RpcInvocation();
try {
invokers = registryDirectory.list(inv);
} catch (RpcException e) {
Assert.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode());
Assert.assertEquals(false, registryDirectory.isAvailable());
}
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));
registryDirectory.notify(serviceUrls);
inv.setMethodName("getXXX2");
invokers = registryDirectory.list(inv);
Assert.assertEquals(true, registryDirectory.isAvailable());
Assert.assertEquals(2, invokers.size());
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class RedisRegistry method doUnregister.
@Override
public void doUnregister(URL url) {
String key = toCategoryPath(url);
String value = url.toFullString();
RpcException exception = null;
boolean success = false;
for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
JedisPool jedisPool = entry.getValue();
try {
Jedis jedis = jedisPool.getResource();
try {
jedis.hdel(key, value);
jedis.publish(key, Constants.UNREGISTER);
success = true;
if (!replicate) {
// If the server side has synchronized data, just write a single machine
break;
}
} finally {
jedis.close();
}
} catch (Throwable t) {
exception = new RpcException("Failed to unregister service to redis registry. registry: " + entry.getKey() + ", service: " + url + ", cause: " + t.getMessage(), t);
}
}
if (exception != null) {
if (success) {
logger.warn(exception.getMessage(), exception);
} else {
throw exception;
}
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class ZookeeperRegistry method doSubscribe.
protected void doSubscribe(final URL url, final NotifyListener listener) {
try {
if (Constants.ANY_VALUE.equals(url.getServiceInterface())) {
String root = toRootPath();
ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
if (listeners == null) {
zkListeners.putIfAbsent(url, new ConcurrentHashMap<NotifyListener, ChildListener>());
listeners = zkListeners.get(url);
}
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
listeners.putIfAbsent(listener, new ChildListener() {
public void childChanged(String parentPath, List<String> currentChilds) {
for (String child : currentChilds) {
child = URL.decode(child);
if (!anyServices.contains(child)) {
anyServices.add(child);
subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, Constants.CHECK_KEY, String.valueOf(false)), listener);
}
}
}
});
zkListener = listeners.get(listener);
}
zkClient.create(root, false);
List<String> services = zkClient.addChildListener(root, zkListener);
if (services != null && !services.isEmpty()) {
for (String service : services) {
service = URL.decode(service);
anyServices.add(service);
subscribe(url.setPath(service).addParameters(Constants.INTERFACE_KEY, service, Constants.CHECK_KEY, String.valueOf(false)), listener);
}
}
} else {
List<URL> urls = new ArrayList<URL>();
for (String path : toCategoriesPath(url)) {
ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
if (listeners == null) {
zkListeners.putIfAbsent(url, new ConcurrentHashMap<NotifyListener, ChildListener>());
listeners = zkListeners.get(url);
}
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
listeners.putIfAbsent(listener, new ChildListener() {
public void childChanged(String parentPath, List<String> currentChilds) {
ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds));
}
});
zkListener = listeners.get(listener);
}
zkClient.create(path, false);
List<String> children = zkClient.addChildListener(path, zkListener);
if (children != null) {
urls.addAll(toUrlsWithEmpty(url, path, children));
}
}
notify(url, listener, urls);
}
} catch (Throwable e) {
throw new RpcException("Failed to subscribe " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class ThriftCodecTest method testDecodeExceptionResponse.
@Test
public void testDecodeExceptionResponse() throws Exception {
URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.class.getName());
Channel channel = new MockedChannel(url);
RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
Request request = createRequest();
DefaultFuture future = new DefaultFuture(channel, request, 10);
TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId());
TTransport transport = new TIOStreamTransport(bos);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
TApplicationException exception = new TApplicationException();
int messageLength, headerLength;
// prepare
protocol.writeI16(ThriftCodec.MAGIC);
protocol.writeI32(Integer.MAX_VALUE);
protocol.writeI16(Short.MAX_VALUE);
protocol.writeByte(ThriftCodec.VERSION);
protocol.writeString(Demo.class.getName());
protocol.writeI64(request.getId());
protocol.getTransport().flush();
headerLength = bos.size();
protocol.writeMessageBegin(message);
exception.write(protocol);
protocol.writeMessageEnd();
protocol.getTransport().flush();
int oldIndex = messageLength = bos.size();
try {
bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
protocol.writeI32(messageLength);
bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
protocol.writeI16((short) (0xffff & headerLength));
} finally {
bos.setWriteIndex(oldIndex);
}
// prepare
ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));
Object obj = codec.decode((Channel) null, bis);
Assert.assertNotNull(obj);
Assert.assertTrue(obj instanceof Response);
Response response = (Response) obj;
Assert.assertTrue(response.getResult() instanceof RpcResult);
RpcResult result = (RpcResult) response.getResult();
Assert.assertTrue(result.hasException());
Assert.assertTrue(result.getException() instanceof RpcException);
}
Aggregations