use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.
the class RpcUtilsTest method testAttachInvocationIdIfAsync_forceAttache.
/**
* scenario: explicitly configure to add attachment
* verify: id attribute added in attachment
*/
@Test
public void testAttachInvocationIdIfAsync_forceAttache() {
URL url = URL.valueOf("dubbo://localhost/?" + Constants.AUTO_ATTACH_INVOCATIONID_KEY + "=true");
Invocation inv = new RpcInvocation("test", new Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
Assert.assertNotNull(RpcUtils.getInvocationId(inv));
}
use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.
the class RpcUtilsTest method testAttachInvocationIdIfAsync_forceNotAttache.
/**
* scenario: explicitly configure to not add attachment
* verify: no id attribute added in attachment
*/
@Test
public void testAttachInvocationIdIfAsync_forceNotAttache() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true&" + Constants.AUTO_ATTACH_INVOCATIONID_KEY + "=false");
Invocation inv = new RpcInvocation("test", new Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
Assert.assertNull(RpcUtils.getInvocationId(inv));
}
use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.
the class RpcUtilsTest method testAttachInvocationIdIfAsync_sync.
/**
* scenario: sync invocation, no attachment added by default
* verify: no id attribute added in attachment
*/
@Test
public void testAttachInvocationIdIfAsync_sync() {
URL url = URL.valueOf("dubbo://localhost/");
Invocation inv = new RpcInvocation("test", new Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
Assert.assertNull(RpcUtils.getInvocationId(inv));
}
use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.
the class RpcUtilsTest method testAttachInvocationIdIfAsync_nullAttachments.
/**
* scenario: async invocation, add attachment by default
* verify: no error report when the original attachment is null
*/
@Test
public void testAttachInvocationIdIfAsync_nullAttachments() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true");
Invocation inv = new RpcInvocation("test", new Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
Assert.assertTrue(RpcUtils.getInvocationId(inv) >= 0l);
}
use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.
the class CallbackServiceCodec method exportOrunexportCallbackService.
/**
* export or unexport callback service on client side
*
* @param channel
* @param url
* @param clazz
* @param inst
* @param export
* @throws IOException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static String exportOrunexportCallbackService(Channel channel, URL url, Class clazz, Object inst, Boolean export) throws IOException {
int instid = System.identityHashCode(inst);
Map<String, String> params = new HashMap<String, String>(3);
// no need to new client again
params.put(Constants.IS_SERVER_KEY, Boolean.FALSE.toString());
// mark it's a callback, for troubleshooting
params.put(Constants.IS_CALLBACK_SERVICE, Boolean.TRUE.toString());
String group = url.getParameter(Constants.GROUP_KEY);
if (group != null && group.length() > 0) {
params.put(Constants.GROUP_KEY, group);
}
// add method, for verifying against method, automatic fallback (see dubbo protocol)
params.put(Constants.METHODS_KEY, StringUtils.join(Wrapper.getWrapper(clazz).getDeclaredMethodNames(), ","));
Map<String, String> tmpmap = new HashMap<String, String>(url.getParameters());
tmpmap.putAll(params);
// doesn't need to distinguish version for callback
tmpmap.remove(Constants.VERSION_KEY);
tmpmap.put(Constants.INTERFACE_KEY, clazz.getName());
URL exporturl = new URL(DubboProtocol.NAME, channel.getLocalAddress().getAddress().getHostAddress(), channel.getLocalAddress().getPort(), clazz.getName() + "." + instid, tmpmap);
// no need to generate multiple exporters for different channel in the same JVM, cache key cannot collide.
String cacheKey = getClientSideCallbackServiceCacheKey(instid);
String countkey = getClientSideCountKey(clazz.getName());
if (export) {
// one channel can have multiple callback instances, no need to re-export for different instance.
if (!channel.hasAttribute(cacheKey)) {
if (!isInstancesOverLimit(channel, url, clazz.getName(), instid, false)) {
Invoker<?> invoker = proxyFactory.getInvoker(inst, clazz, exporturl);
// should destroy resource?
Exporter<?> exporter = protocol.export(invoker);
// this is used for tracing if instid has published service or not.
channel.setAttribute(cacheKey, exporter);
logger.info("export a callback service :" + exporturl + ", on " + channel + ", url is: " + url);
increaseInstanceCount(channel, countkey);
}
}
} else {
if (channel.hasAttribute(cacheKey)) {
Exporter<?> exporter = (Exporter<?>) channel.getAttribute(cacheKey);
exporter.unexport();
channel.removeAttribute(cacheKey);
decreaseInstanceCount(channel, countkey);
}
}
return String.valueOf(instid);
}
Aggregations