use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.
the class ConsumerContextClusterInterceptorTest method testAfter.
@Test
public void testAfter() {
RpcContext contextBefore = RpcContext.getContext();
interceptor.after(null, null);
RpcContext contextAfter = RpcContext.getContext();
Assertions.assertNotSame(contextBefore, contextAfter);
}
use of org.apache.dubbo.rpc.RpcContext 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.RpcContext in project dubbo by alibaba.
the class ZoneAwareClusterInterceptorTest method testBefore.
@Test
public void testBefore() {
RpcInvocation invocation = new RpcInvocation();
interceptor.before(null, invocation);
Assertions.assertEquals("mock_zone", invocation.getAttachment(REGISTRY_ZONE));
Assertions.assertEquals("mock_force", invocation.getAttachment(REGISTRY_ZONE_FORCE));
RpcContext context = RpcContext.getContext();
context.setAttachment(REGISTRY_ZONE, "context_zone");
context.setAttachment(REGISTRY_ZONE_FORCE, "context_force");
interceptor.before(null, invocation);
Assertions.assertEquals("context_zone", invocation.getAttachment(REGISTRY_ZONE));
Assertions.assertEquals("context_force", invocation.getAttachment(REGISTRY_ZONE_FORCE));
}
use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.
the class MetricsFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (exported.compareAndSet(false, true)) {
this.protocolName = invoker.getUrl().getParameter(METRICS_PROTOCOL) == null ? DEFAULT_PROTOCOL : invoker.getUrl().getParameter(METRICS_PROTOCOL);
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
this.port = invoker.getUrl().getParameter(METRICS_PORT) == null ? protocol.getDefaultPort() : Integer.parseInt(invoker.getUrl().getParameter(METRICS_PORT));
Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
try {
protocol.export(metricsInvoker);
} catch (RuntimeException e) {
logger.error("Metrics Service need to be configured" + " when multiple processes are running on a host" + e.getMessage());
}
}
RpcContext context = RpcContext.getContext();
boolean isProvider = context.isProviderSide();
long start = System.currentTimeMillis();
try {
// proceed invocation chain
Result result = invoker.invoke(invocation);
long duration = System.currentTimeMillis() - start;
reportMetrics(invoker, invocation, duration, "success", isProvider);
return result;
} catch (RpcException e) {
long duration = System.currentTimeMillis() - start;
String result = "error";
if (e.isTimeout()) {
result = "timeoutError";
}
if (e.isBiz()) {
result = "bisError";
}
if (e.isNetwork()) {
result = "networkError";
}
if (e.isSerialization()) {
result = "serializationError";
}
reportMetrics(invoker, invocation, duration, result, isProvider);
throw e;
}
}
use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.
the class RmiRemoteInvocation method invoke.
/**
* Need to restore context on provider side (Though context will be overridden by Invocation's attachment
* when ContextFilter gets executed, we will restore the attachment when Invocation is constructed, check more
* from {@link org.apache.dubbo.rpc.proxy.InvokerInvocationHandler}
*/
@SuppressWarnings("unchecked")
@Override
public Object invoke(Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
RpcContext context = RpcContext.getContext();
context.setObjectAttachments((Map<String, Object>) getAttribute(DUBBO_ATTACHMENTS_ATTR_NAME));
String generic = (String) getAttribute(GENERIC_KEY);
if (StringUtils.isNotEmpty(generic)) {
context.setAttachment(GENERIC_KEY, generic);
}
try {
return super.invoke(targetObject);
} finally {
context.setObjectAttachments(null);
}
}
Aggregations