Search in sources :

Example 41 with URL

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));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 42 with URL

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));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 43 with URL

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));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 44 with URL

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);
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 45 with URL

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);
}
Also used : HashMap(java.util.HashMap) Exporter(com.alibaba.dubbo.rpc.Exporter) URL(com.alibaba.dubbo.common.URL)

Aggregations

URL (com.alibaba.dubbo.common.URL)297 Test (org.junit.Test)169 ArrayList (java.util.ArrayList)73 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)64 HashMap (java.util.HashMap)45 Result (com.alibaba.dubbo.rpc.Result)37 Invoker (com.alibaba.dubbo.rpc.Invoker)36 Map (java.util.Map)36 List (java.util.List)30 Invocation (com.alibaba.dubbo.rpc.Invocation)29 ConcurrentMap (java.util.concurrent.ConcurrentMap)29 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)28 RpcException (com.alibaba.dubbo.rpc.RpcException)22 NotifyListener (com.alibaba.dubbo.registry.NotifyListener)20 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)18 RpcResult (com.alibaba.dubbo.rpc.RpcResult)17 DemoService (com.alibaba.dubbo.rpc.support.DemoService)12 Set (java.util.Set)12 ConcurrentHashSet (com.alibaba.dubbo.common.utils.ConcurrentHashSet)11 Protocol (com.alibaba.dubbo.rpc.Protocol)11