Java Code Examples for com.alibaba.dubbo.rpc.RpcInvocation#setAttachment()

The following examples show how to use com.alibaba.dubbo.rpc.RpcInvocation#setAttachment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 测试mock provider下发
 */
@Test
public void testNotify_MockProviderOnly() {
	RegistryDirectory registryDirectory = getRegistryDirectory();
	
	List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
	
    RpcInvocation mockinvocation = new RpcInvocation();
    mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    invokers = registryDirectory.list(mockinvocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 2
Source File: CallbackServiceCodec.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public static Object encodeInvocationArgument(Channel channel, RpcInvocation inv, int paraIndex) throws IOException {
    // get URL directly
    URL url = inv.getInvoker() == null ? null : inv.getInvoker().getUrl();
    byte callbackstatus = isCallBack(url, inv.getMethodName(), paraIndex);
    Object[] args = inv.getArguments();
    Class<?>[] pts = inv.getParameterTypes();
    switch (callbackstatus) {
        case CallbackServiceCodec.CALLBACK_NONE:
            return args[paraIndex];
        case CallbackServiceCodec.CALLBACK_CREATE:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true));
            return null;
        case CallbackServiceCodec.CALLBACK_DESTROY:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false));
            return null;
        default:
            return args[paraIndex];
    }
}
 
Example 3
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
/**
 * Test mock provider distribution
 */
@Test
public void testNotify_MockProviderOnly() {
    RegistryDirectory registryDirectory = getRegistryDirectory();

    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    RpcInvocation mockinvocation = new RpcInvocation();
    mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    invokers = registryDirectory.list(mockinvocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 4
Source File: CallbackServiceCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static Object encodeInvocationArgument(Channel channel, RpcInvocation inv, int paraIndex) throws IOException{
    //encode时可直接获取url
    URL url = inv.getInvoker() == null ? null : inv.getInvoker().getUrl();
    byte callbackstatus = isCallBack(url, inv.getMethodName(), paraIndex);
    Object[] args = inv.getArguments();
    Class<?>[] pts = inv.getParameterTypes();
    switch (callbackstatus) {
        case CallbackServiceCodec.CALLBACK_NONE:
            return args[paraIndex];
        case CallbackServiceCodec.CALLBACK_CREATE:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex , exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true));
            return null;
        case CallbackServiceCodec.CALLBACK_DESTROY:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false));
            return null;
        default:
            return args[paraIndex];
    }
}
 
Example 5
Source File: RegistryDirectoryTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
/**
 * 测试mock provider下发
 */
@Test
public void testNotify_MockProviderOnly() {
	RegistryDirectory registryDirectory = getRegistryDirectory();
	
	List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
	
    RpcInvocation mockinvocation = new RpcInvocation();
    mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    invokers = registryDirectory.list(mockinvocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 6
Source File: CallbackServiceCodec.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public static Object encodeInvocationArgument(Channel channel, RpcInvocation inv, int paraIndex) throws IOException{
    //encode时可直接获取url
    URL url = inv.getInvoker() == null ? null : inv.getInvoker().getUrl();
    byte callbackstatus = isCallBack(url, inv.getMethodName(), paraIndex);
    Object[] args = inv.getArguments();
    Class<?>[] pts = inv.getParameterTypes();
    switch (callbackstatus) {
        case CallbackServiceCodec.CALLBACK_NONE:
            return args[paraIndex];
        case CallbackServiceCodec.CALLBACK_CREATE:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex , exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true));
            return null;
        case CallbackServiceCodec.CALLBACK_DESTROY:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false));
            return null;
        default:
            return args[paraIndex];
    }
}
 
Example 7
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 测试mock provider下发
 */
@Test
public void testNotify_MockProviderOnly() {
	RegistryDirectory registryDirectory = getRegistryDirectory();
	
	List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
	
    RpcInvocation mockinvocation = new RpcInvocation();
    mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    invokers = registryDirectory.list(mockinvocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 8
Source File: CallbackServiceCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static Object encodeInvocationArgument(Channel channel, RpcInvocation inv, int paraIndex) throws IOException{
    //encode时可直接获取url
    URL url = inv.getInvoker() == null ? null : inv.getInvoker().getUrl();
    byte callbackstatus = isCallBack(url, inv.getMethodName(), paraIndex);
    Object[] args = inv.getArguments();
    Class<?>[] pts = inv.getParameterTypes();
    switch (callbackstatus) {
        case CallbackServiceCodec.CALLBACK_NONE:
            return args[paraIndex];
        case CallbackServiceCodec.CALLBACK_CREATE:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex , exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true));
            return null;
        case CallbackServiceCodec.CALLBACK_DESTROY:
            inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false));
            return null;
        default:
            return args[paraIndex];
    }
}
 
Example 9
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 测试mock provider下发
 */
@Test
public void testNotify_MockProviderOnly() {
	RegistryDirectory registryDirectory = getRegistryDirectory();
	
	List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
	
    RpcInvocation mockinvocation = new RpcInvocation();
    mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    invokers = registryDirectory.list(mockinvocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 10
Source File: CicadaDubboFilter.java    From cicada with MIT License 5 votes vote down vote up
private void invokerBefore(final Invocation invocation, final Span span, final Endpoint endpoint, final long start) {
  final RpcContext context = RpcContext.getContext();
  if (context.isConsumerSide()) {
    if (span.isSample()) {
      tracer.clientSendRecord(span, endpoint, start);

      final RpcInvocation rpcInvocation = (RpcInvocation) invocation;
      rpcInvocation.setAttachment(TracerUtils.PARENT_SPAN_ID, span.getParentId());
      rpcInvocation.setAttachment(TracerUtils.SPAN_ID, span.getId());
      rpcInvocation.setAttachment(TracerUtils.TRACE_ID, span.getTraceId());
    }
  } else if (context.isProviderSide()) {
    tracer.serverReceiveRecord(span, endpoint, start);
  }
}
 
Example 11
Source File: DubboProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private Invocation createInvocation(Channel channel, URL url, String methodKey) {
    String method = url.getParameter(methodKey);
    if (method == null || method.length() == 0) {
        return null;
    }
    RpcInvocation invocation = new RpcInvocation(method, new Class<?>[0], new Object[0]);
    invocation.setAttachment(Constants.PATH_KEY, url.getPath());
    invocation.setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
    invocation.setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
    invocation.setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY));
    if (url.getParameter(Constants.STUB_EVENT_KEY, false)){
        invocation.setAttachment(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString());
    }
    return invocation;
}
 
Example 12
Source File: DubboProtocol.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private Invocation createInvocation(Channel channel, URL url, String methodKey) {
    String method = url.getParameter(methodKey);
    if (method == null || method.length() == 0) {
        return null;
    }
    RpcInvocation invocation = new RpcInvocation(method, new Class<?>[0], new Object[0]);
    invocation.setAttachment(Constants.PATH_KEY, url.getPath());
    invocation.setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
    invocation.setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
    invocation.setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY));
    if (url.getParameter(Constants.STUB_EVENT_KEY, false)){
        invocation.setAttachment(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString());
    }
    return invocation;
}
 
Example 13
Source File: DubboOriginRegistryTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultOriginParserSuccess() {
    RpcInvocation invocation = new RpcInvocation();
    String dubboName = "sentinel";
    invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName);
    String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
    Assert.assertEquals(dubboName, origin);
}
 
Example 14
Source File: DataSizeCodecWrapper.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
/**
 * deserialization operation
 * @param channel
 * @param input
 * @return
 * @throws IOException
 */
@Override
public Object decode(Channel channel, ChannelBuffer input) throws IOException {
    long startTime = System.currentTimeMillis();
    int index = input.readerIndex();
    Object ret = codec.decode(channel, input);
    int size = input.readerIndex() - index;
    long elapsed = System.currentTimeMillis() - startTime;
    if (ret instanceof Request) {
        // server-side deserialize the Request
        Object data = ((Request) ret).getData();
        if (data instanceof RpcInvocation) {
            RpcInvocation invocation = (RpcInvocation) data;
            invocation.setAttachment(AttachmentKeyConstants.SERVER_DESERIALIZE_SIZE,
                String.valueOf(size));
            invocation.setAttachment(AttachmentKeyConstants.SERVER_DESERIALIZE_TIME,
                String.valueOf(elapsed));
        }
    } else if (ret instanceof Response) {
        // client-side deserialize the Response
        Object result = ((Response) ret).getResult();
        if (result instanceof RpcResult) {
            RpcResult rpcResult = (RpcResult) result;
            rpcResult.setAttachment(AttachmentKeyConstants.CLIENT_DESERIALIZE_SIZE,
                String.valueOf(size));
            rpcResult.setAttachment(AttachmentKeyConstants.CLIENT_DESERIALIZE_TIME,
                String.valueOf(elapsed));
        }
    }
    return ret;
}
 
Example 15
Source File: DataSizeCodecWrapper.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
/**
 * @param channel       a long connection
 * @param buffer        buffer
 * @param message       the original Request object
 * @param invocation    Invocation in Request
 * @throws IOException  serialization exception
 */
protected void encodeRequestWithTracer(Channel channel, ChannelBuffer buffer, Object message,
                                       RpcInvocation invocation) throws IOException {
    long startTime = System.currentTimeMillis();
    int index = buffer.writerIndex();
    // serialization
    codec.encode(channel, buffer, message);
    int reqSize = buffer.writerIndex() - index;
    long elapsed = System.currentTimeMillis() - startTime;
    invocation.setAttachment(AttachmentKeyConstants.CLIENT_SERIALIZE_SIZE,
        String.valueOf(reqSize));
    invocation.setAttachment(AttachmentKeyConstants.CLIENT_SERIALIZE_TIME,
        String.valueOf(elapsed));
}
 
Example 16
Source File: DubboProtocol.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private Invocation createInvocation(Channel channel, URL url, String methodKey) {
    String method = url.getParameter(methodKey);
    if (method == null || method.length() == 0) {
        return null;
    }
    RpcInvocation invocation = new RpcInvocation(method, new Class<?>[0], new Object[0]);
    invocation.setAttachment(Constants.PATH_KEY, url.getPath());
    invocation.setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
    invocation.setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
    invocation.setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY));
    if (url.getParameter(Constants.STUB_EVENT_KEY, false)) {
        invocation.setAttachment(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString());
    }
    return invocation;
}
 
Example 17
Source File: DubboConsumerInterceptor.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
private void setAttachment(RpcInvocation invocation, String name, String value) {
    invocation.setAttachment(name, value);
    if (isDebug) {
        logger.debug("Set attachment {}={}", name, value);
    }
}
 
Example 18
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
private Request createRequest() {

        RpcInvocation invocation = new RpcInvocation();

        invocation.setMethodName( "echoString" );

        invocation.setArguments( new Object[]{ "Hello, World!" } );

        invocation.setParameterTypes( new Class<?>[]{ String.class } );

        invocation.setAttachment( Constants.INTERFACE_KEY, Demo.Iface.class.getName() );

        Request request = new Request( 1L );

        request.setData( invocation );

        return request;

    }
 
Example 19
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private Request createRequest() {

        RpcInvocation invocation = new RpcInvocation();

        invocation.setMethodName( "echoString" );

        invocation.setArguments( new Object[]{ "Hello, World!" } );

        invocation.setParameterTypes( new Class<?>[]{ String.class } );

        invocation.setAttachment( Constants.INTERFACE_KEY, Demo.Iface.class.getName() );

        Request request = new Request( 1L );

        request.setData( invocation );

        return request;

    }
 
Example 20
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private Request createRequest() {

        RpcInvocation invocation = new RpcInvocation();

        invocation.setMethodName( "echoString" );

        invocation.setArguments( new Object[]{ "Hello, World!" } );

        invocation.setParameterTypes( new Class<?>[]{ String.class } );

        invocation.setAttachment( Constants.INTERFACE_KEY, Demo.Iface.class.getName() );

        Request request = new Request( 1L );

        request.setData( invocation );

        return request;

    }