use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.
the class ContextFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Map<String, Object> attachments = invocation.getObjectAttachments();
if (attachments != null) {
Map<String, Object> newAttach = new HashMap<>(attachments.size());
for (Map.Entry<String, Object> entry : attachments.entrySet()) {
String key = entry.getKey();
if (!UNLOADING_KEYS.contains(key)) {
newAttach.put(key, entry.getValue());
}
}
attachments = newAttach;
}
RpcContext context = RpcContext.getContext();
context.setInvoker(invoker).setInvocation(invocation).setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort());
String remoteApplication = (String) invocation.getAttachment(REMOTE_APPLICATION_KEY);
if (StringUtils.isNotEmpty(remoteApplication)) {
context.setRemoteApplicationName(remoteApplication);
} else {
context.setRemoteApplicationName((String) context.getAttachment(REMOTE_APPLICATION_KEY));
}
long timeout = RpcUtils.getTimeout(invocation, -1);
if (timeout != -1) {
context.set(TIME_COUNTDOWN_KEY, TimeoutCountDown.newCountDown(timeout, TimeUnit.MILLISECONDS));
}
// we may already added some attachments into RpcContext before this filter (e.g. in rest protocol)
if (attachments != null) {
if (context.getObjectAttachments() != null) {
context.getObjectAttachments().putAll(attachments);
} else {
context.setObjectAttachments(attachments);
}
}
if (invocation instanceof RpcInvocation) {
((RpcInvocation) invocation).setInvoker(invoker);
}
try {
context.clearAfterEachInvoke(false);
return invoker.invoke(invocation);
} finally {
context.clearAfterEachInvoke(true);
// IMPORTANT! For async scenario, we must remove context from current thread, so we always create a new RpcContext for the next invoke for the same thread.
RpcContext.removeContext(true);
RpcContext.removeServerContext();
}
}
use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.
the class ConsumerContextClusterInterceptorTest method testBefore.
@Test
public void testBefore() {
AbstractClusterInvoker mockInvoker = Mockito.mock(AbstractClusterInvoker.class);
RpcContext serverContextBefore = RpcContext.getServerContext();
RpcInvocation rpcInvocation = new RpcInvocation();
interceptor.before(mockInvoker, rpcInvocation);
RpcContext serverContextAfter = RpcContext.getServerContext();
Assertions.assertNotSame(serverContextBefore, serverContextAfter);
Assertions.assertSame(mockInvoker, rpcInvocation.getInvoker());
}
use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.
the class AbstractLoadBalanceTest method testGetWeight.
@Test
public void testGetWeight() {
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("say");
Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly());
URL url1 = new URL("", "", 0, "DemoService", new HashMap<>());
url1 = url1.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - Integer.MAX_VALUE - 1);
given(invoker1.getUrl()).willReturn(url1);
Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly());
URL url2 = new URL("", "", 0, "DemoService", new HashMap<>());
url2 = url2.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - 10 * 60 * 1000L - 1);
given(invoker2.getUrl()).willReturn(url2);
Assertions.assertEquals(balance.getWeight(invoker1, invocation), balance.getWeight(invoker2, invocation));
}
use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.
the class FileRouterEngineTest method initInvocation.
private void initInvocation(String methodName) {
invocation = new RpcInvocation();
((RpcInvocation) invocation).setMethodName(methodName);
}
use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.
the class AbstractClusterInvokerTest method testMockedInvokerSelect.
/**
* Test mock invoker selector works as expected
*/
@Test
public void testMockedInvokerSelect() {
initlistsize5();
invokers.add(mockedInvoker1);
initDic();
RpcInvocation mockedInvocation = new RpcInvocation();
mockedInvocation.setMethodName("sayHello");
mockedInvocation.setAttachment(INVOCATION_NEED_MOCK, "true");
List<Invoker<IHelloService>> mockedInvokers = dic.list(mockedInvocation);
Assertions.assertEquals(1, mockedInvokers.size());
List<Invoker<IHelloService>> invokers = dic.list(invocation);
Assertions.assertEquals(5, invokers.size());
}
Aggregations