Search in sources :

Example 41 with Invocation

use of com.alibaba.dubbo.rpc.Invocation in project dubbo by alibaba.

the class TpsLimitFilterTest method testWithoutCount.

@Test
public void testWithoutCount() throws Exception {
    URL url = URL.valueOf("test://test");
    url = url.addParameter(Constants.INTERFACE_KEY, "com.alibaba.dubbo.rpc.file.TpsService");
    url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 5);
    Invoker<TpsLimitFilterTest> invoker = new MyInvoker<TpsLimitFilterTest>(url);
    Invocation invocation = new MockInvocation();
    filter.invoke(invoker, invocation);
}
Also used : MockInvocation(com.alibaba.dubbo.rpc.support.MockInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) MockInvocation(com.alibaba.dubbo.rpc.support.MockInvocation) MyInvoker(com.alibaba.dubbo.rpc.support.MyInvoker) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 42 with Invocation

use of com.alibaba.dubbo.rpc.Invocation in project dubbo by alibaba.

the class RpcUtilsTest method testAttachInvocationIdIfAsync_normal.

/**
 * regular scenario: async invocation in URL
 * verify: 1. whether invocationId is set correctly, 2. idempotent or not
 */
@Test
public void testAttachInvocationIdIfAsync_normal() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true");
    Map<String, String> attachments = new HashMap<String, String>();
    attachments.put("aa", "bb");
    Invocation inv = new RpcInvocation("test", new Class[] {}, new String[] {}, attachments);
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    long id1 = RpcUtils.getInvocationId(inv);
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    long id2 = RpcUtils.getInvocationId(inv);
    // verify if it's idempotent
    Assert.assertTrue(id1 == id2);
    Assert.assertTrue(id1 >= 0);
    Assert.assertEquals("bb", attachments.get("aa"));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) HashMap(java.util.HashMap) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 43 with Invocation

use of com.alibaba.dubbo.rpc.Invocation in project dubbo by alibaba.

the class MemcachedProtocol method refer.

public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcException {
    try {
        String address = url.getAddress();
        String backup = url.getParameter(Constants.BACKUP_KEY);
        if (backup != null && backup.length() > 0) {
            address += "," + backup;
        }
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(address));
        final MemcachedClient memcachedClient = builder.build();
        final int expiry = url.getParameter("expiry", 0);
        final String get = url.getParameter("get", "get");
        final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");
        final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete");
        return new AbstractInvoker<T>(type, url) {

            protected Result doInvoke(Invocation invocation) throws Throwable {
                try {
                    if (get.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 1) {
                            throw new IllegalArgumentException("The memcached get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        return new RpcResult(memcachedClient.get(String.valueOf(invocation.getArguments()[0])));
                    } else if (set.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 2) {
                            throw new IllegalArgumentException("The memcached set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        memcachedClient.set(String.valueOf(invocation.getArguments()[0]), expiry, invocation.getArguments()[1]);
                        return new RpcResult();
                    } else if (delete.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 1) {
                            throw new IllegalArgumentException("The memcached delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        memcachedClient.delete(String.valueOf(invocation.getArguments()[0]));
                        return new RpcResult();
                    } else {
                        throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in memcached service.");
                    }
                } catch (Throwable t) {
                    RpcException re = new RpcException("Failed to invoke memcached service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t);
                    if (t instanceof TimeoutException || t instanceof SocketTimeoutException) {
                        re.setCode(RpcException.TIMEOUT_EXCEPTION);
                    } else if (t instanceof MemcachedException || t instanceof IOException) {
                        re.setCode(RpcException.NETWORK_EXCEPTION);
                    }
                    throw re;
                }
            }

            public void destroy() {
                super.destroy();
                try {
                    memcachedClient.shutdown();
                } catch (Throwable e) {
                    logger.warn(e.getMessage(), e);
                }
            }
        };
    } catch (Throwable t) {
        throw new RpcException("Failed to refer memcached service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t);
    }
}
Also used : XMemcachedClientBuilder(net.rubyeye.xmemcached.XMemcachedClientBuilder) MemcachedClientBuilder(net.rubyeye.xmemcached.MemcachedClientBuilder) Invocation(com.alibaba.dubbo.rpc.Invocation) RpcResult(com.alibaba.dubbo.rpc.RpcResult) AbstractInvoker(com.alibaba.dubbo.rpc.protocol.AbstractInvoker) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) MemcachedClient(net.rubyeye.xmemcached.MemcachedClient) RpcException(com.alibaba.dubbo.rpc.RpcException) XMemcachedClientBuilder(net.rubyeye.xmemcached.XMemcachedClientBuilder) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) MemcachedException(net.rubyeye.xmemcached.exception.MemcachedException)

Example 44 with Invocation

use of com.alibaba.dubbo.rpc.Invocation in project dubbo by alibaba.

the class ThriftNativeCodec method encodeRequest.

protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException {
    Invocation invocation = (Invocation) request.getData();
    TProtocol protocol = newProtocol(channel.getUrl(), buffer);
    try {
        protocol.writeMessageBegin(new TMessage(invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement()));
        protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args"));
        for (int i = 0; i < invocation.getParameterTypes().length; i++) {
            Class<?> type = invocation.getParameterTypes()[i];
        }
    } catch (TException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : TException(org.apache.thrift.TException) Invocation(com.alibaba.dubbo.rpc.Invocation) TProtocol(org.apache.thrift.protocol.TProtocol) TMessage(org.apache.thrift.protocol.TMessage) IOException(java.io.IOException) TStruct(org.apache.thrift.protocol.TStruct)

Example 45 with Invocation

use of com.alibaba.dubbo.rpc.Invocation in project dubbo by alibaba.

the class ProtocolFilterWrapper method buildInvokerChain.

private static <T> Invoker<T> buildInvokerChain(final Invoker<T> invoker, String key, String group) {
    Invoker<T> last = invoker;
    List<Filter> filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);
    if (!filters.isEmpty()) {
        for (int i = filters.size() - 1; i >= 0; i--) {
            final Filter filter = filters.get(i);
            final Invoker<T> next = last;
            last = new Invoker<T>() {

                public Class<T> getInterface() {
                    return invoker.getInterface();
                }

                public URL getUrl() {
                    return invoker.getUrl();
                }

                public boolean isAvailable() {
                    return invoker.isAvailable();
                }

                public Result invoke(Invocation invocation) throws RpcException {
                    return filter.invoke(next, invocation);
                }

                public void destroy() {
                    invoker.destroy();
                }

                @Override
                public String toString() {
                    return invoker.toString();
                }
            };
        }
    }
    return last;
}
Also used : Invocation(com.alibaba.dubbo.rpc.Invocation) Filter(com.alibaba.dubbo.rpc.Filter) RpcException(com.alibaba.dubbo.rpc.RpcException) URL(com.alibaba.dubbo.common.URL) Result(com.alibaba.dubbo.rpc.Result)

Aggregations

Invocation (com.alibaba.dubbo.rpc.Invocation)47 URL (com.alibaba.dubbo.common.URL)31 Test (org.junit.Test)30 Result (com.alibaba.dubbo.rpc.Result)15 Invoker (com.alibaba.dubbo.rpc.Invoker)14 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)14 RpcResult (com.alibaba.dubbo.rpc.RpcResult)12 DemoService (com.alibaba.dubbo.rpc.support.DemoService)11 MockInvocation (com.alibaba.dubbo.rpc.support.MockInvocation)11 MyInvoker (com.alibaba.dubbo.rpc.support.MyInvoker)10 RpcException (com.alibaba.dubbo.rpc.RpcException)9 ArrayList (java.util.ArrayList)4 Router (com.alibaba.dubbo.rpc.cluster.Router)3 BlockMyInvoker (com.alibaba.dubbo.rpc.support.BlockMyInvoker)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 Cache (com.alibaba.dubbo.cache.Cache)2 CacheFactory (com.alibaba.dubbo.cache.CacheFactory)2 MonitorService (com.alibaba.dubbo.monitor.MonitorService)2