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);
}
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"));
}
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);
}
}
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);
}
}
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;
}
Aggregations