use of org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker in project dubbo by alibaba.
the class CallbackServiceCodec method referOrDestroyCallbackService.
/**
* refer or destroy callback service on server side
*
* @param url
*/
@SuppressWarnings("unchecked")
private static Object referOrDestroyCallbackService(Channel channel, URL url, Class<?> clazz, Invocation inv, int instid, boolean isRefer) {
Object proxy;
String invokerCacheKey = getServerSideCallbackInvokerCacheKey(channel, clazz.getName(), instid);
String proxyCacheKey = getServerSideCallbackServiceCacheKey(channel, clazz.getName(), instid);
proxy = channel.getAttribute(proxyCacheKey);
String countkey = getServerSideCountKey(channel, clazz.getName());
if (isRefer) {
if (proxy == null) {
URL referurl = URL.valueOf("callback://" + url.getAddress() + "/" + clazz.getName() + "?" + INTERFACE_KEY + "=" + clazz.getName());
referurl = referurl.addParametersIfAbsent(url.getParameters()).removeParameter(METHODS_KEY);
if (!isInstancesOverLimit(channel, referurl, clazz.getName(), instid, true)) {
ApplicationModel.getServiceRepository().registerService(clazz);
@SuppressWarnings("rawtypes") Invoker<?> invoker = new ChannelWrappedInvoker(clazz, channel, referurl, String.valueOf(instid));
proxy = PROXY_FACTORY.getProxy(new AsyncToSyncInvoker<>(invoker));
channel.setAttribute(proxyCacheKey, proxy);
channel.setAttribute(invokerCacheKey, invoker);
increaseInstanceCount(channel, countkey);
// convert error fail fast .
// ignore concurrent problem.
Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
if (callbackInvokers == null) {
callbackInvokers = new ConcurrentHashSet<>(1);
channel.setAttribute(CHANNEL_CALLBACK_KEY, callbackInvokers);
}
callbackInvokers.add(invoker);
logger.info("method " + inv.getMethodName() + " include a callback service :" + invoker.getUrl() + ", a proxy :" + invoker + " has been created.");
}
}
} else {
if (proxy != null) {
Invoker<?> invoker = (Invoker<?>) channel.getAttribute(invokerCacheKey);
try {
Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
if (callbackInvokers != null) {
callbackInvokers.remove(invoker);
}
invoker.destroy();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
// cancel refer, directly remove from the map
channel.removeAttribute(proxyCacheKey);
channel.removeAttribute(invokerCacheKey);
decreaseInstanceCount(channel, countkey);
}
}
return proxy;
}
use of org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker in project dubbo by alibaba.
the class ReferenceCountExchangeClientTest method getInvokerClientList.
private List<ExchangeClient> getInvokerClientList(Invoker<?> invoker) {
@SuppressWarnings("rawtypes") DubboInvoker dInvoker = (DubboInvoker) ((AsyncToSyncInvoker) invoker).getInvoker();
try {
Field clientField = DubboInvoker.class.getDeclaredField("clients");
ReflectUtils.makeAccessible(clientField);
ExchangeClient[] clients = (ExchangeClient[]) clientField.get(dInvoker);
List<ExchangeClient> clientList = new ArrayList<ExchangeClient>(clients.length);
for (ExchangeClient client : clients) {
clientList.add(client);
}
// sorting makes it easy to compare between lists
Collections.sort(clientList, Comparator.comparing(c -> Integer.valueOf(Objects.hashCode(c))));
return clientList;
} catch (Exception e) {
e.printStackTrace();
Assertions.fail(e.getMessage());
throw new RuntimeException(e);
}
}
use of org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker in project dubbo by alibaba.
the class DubboInvokerAvailableTest method test_Lazy_ChannelReadOnly.
@Test
public void test_Lazy_ChannelReadOnly() throws Exception {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?lazy=true&connections=1&timeout=10000");
ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);
AsyncToSyncInvoker<?> invoker = (AsyncToSyncInvoker) protocol.refer(IDemoService.class, url);
Assertions.assertTrue(invoker.isAvailable());
ExchangeClient exchangeClient = getClients((DubboInvoker<?>) invoker.getInvoker())[0];
Assertions.assertFalse(exchangeClient.isClosed());
try {
exchangeClient.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
fail();
} catch (IllegalStateException e) {
}
// invoke method --> init client
IDemoService service = (IDemoService) proxy.getProxy(invoker);
Assertions.assertEquals("ok", service.get());
Assertions.assertTrue(invoker.isAvailable());
exchangeClient.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
Assertions.assertFalse(invoker.isAvailable());
}
Aggregations