Java Code Examples for com.alipay.remoting.InvokeContext#put()

The following examples show how to use com.alipay.remoting.InvokeContext#put() . 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: BoltChannelUtilTest.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBoltCustomSerializer() {
    Assert.assertNull(BoltChannelUtil.getBoltCustomSerializer(new MockChannel()));
    BoltChannel boltChannel = new BoltChannel();
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, new Object());
    RemotingContext remotingContext = new RemotingContext(new MockChannelHandlerContext(),
        invokeContext, false, new ConcurrentHashMap<>());
    BizContext bizContext = new DefaultBizContext(remotingContext);
    boltChannel.setBizContext(bizContext);
    boolean isException = false;
    try {
        BoltChannelUtil.getBoltCustomSerializer(boltChannel);
    } catch (Throwable r) {
        isException = true;
    }
    Assert.assertTrue(isException);
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, new Byte("3"));
    Assert.assertEquals(new Byte("3"), BoltChannelUtil.getBoltCustomSerializer(boltChannel));
}
 
Example 2
Source File: BoltRaftRpcFactory.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public ConfigHelper<RpcClient> defaultJRaftClientConfigHelper(final RpcOptions opts) {
    return ins -> {
        final BoltRpcClient client = (BoltRpcClient) ins;
        final InvokeContext ctx = new InvokeContext();
        ctx.put(InvokeContext.BOLT_CRC_SWITCH, opts.isEnableRpcChecksum());
        client.setDefaultInvokeCtx(ctx);
    };
}
 
Example 3
Source File: BasicUsage_InvokeContext_resuable_Test.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneway() throws InterruptedException {
    RequestBody req = new RequestBody(2, "hello world oneway");
    for (int i = 0; i < invokeTimes; i++) {
        try {
            InvokeContext invokeContext = new InvokeContext();
            invokeContext.put(TESTKEY, "TESTVALUE");
            client.oneway(addr, req, invokeContext);
            Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.CLIENT_LOCAL_IP));
            Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.CLIENT_REMOTE_IP));
            Assert.assertNotNull(invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT));
            Assert.assertNotNull(invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT));

            Assert.assertNotNull(invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));
            logger.warn("CLIENT_CONN_CREATETIME:"
                        + invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));

            Assert.assertEquals(invokeContext.get(TESTKEY), "TESTVALUE");
            Thread.sleep(100);
        } catch (RemotingException e) {
            String errMsg = "RemotingException caught in oneway!";
            logger.error(errMsg, e);
            Assert.fail(errMsg);
        }
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 4
Source File: BasicUsage_InvokeContext_resuable_Test.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerSyncUsingConnection() throws Exception {
    Connection clientConn = client.createStandaloneConnection(ip, port, 1000);

    for (int i = 0; i < invokeTimes; i++) {
        InvokeContext invokeContext = new InvokeContext();
        invokeContext.put(TESTKEY, "TESTVALUE");
        RequestBody req1 = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
        String serverres = (String) client.invokeSync(clientConn, req1, invokeContext, 1000);
        Assert.assertEquals(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR);

        invokeContext.clear();
        invokeContext.put(TESTKEY, "TESTVALUE");
        Assert.assertNotNull(serverConnectProcessor.getConnection());
        Connection serverConn = serverConnectProcessor.getConnection();
        RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
        String clientres = (String) server.getRpcServer().invokeSync(serverConn, req,
            invokeContext, 1000);
        Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR);

        Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.SERVER_LOCAL_IP));
        Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.SERVER_REMOTE_IP));
        Assert.assertNotNull(invokeContext.get(InvokeContext.SERVER_LOCAL_PORT));
        Assert.assertNotNull(invokeContext.get(InvokeContext.SERVER_REMOTE_PORT));
        Assert.assertEquals(invokeContext.get(TESTKEY), "TESTVALUE");
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 5
Source File: BasicUsage_InvokeContext_resuable_Test.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerSyncUsingAddress() throws Exception {
    Connection clientConn = client.createStandaloneConnection(ip, port, 1000);
    String remote = RemotingUtil.parseRemoteAddress(clientConn.getChannel());
    String local = RemotingUtil.parseLocalAddress(clientConn.getChannel());
    logger.warn("Client say local:" + local);
    logger.warn("Client say remote:" + remote);

    for (int i = 0; i < invokeTimes; i++) {
        InvokeContext invokeContext = new InvokeContext();
        invokeContext.put(TESTKEY, "TESTVALUE");
        RequestBody req1 = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
        String serverres = (String) client.invokeSync(clientConn, req1, invokeContext, 1000);
        Assert.assertEquals(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR);

        invokeContext.clear();
        invokeContext.put(TESTKEY, "TESTVALUE");
        Assert.assertNotNull(serverConnectProcessor.getConnection());
        // only when client invoked, the remote address can be get by UserProcessor
        // otherwise, please use ConnectionEventProcessor
        String remoteAddr = serverUserProcessor.getRemoteAddr();
        RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
        String clientres = (String) server.getRpcServer().invokeSync(remoteAddr, req,
            invokeContext, 1000);
        Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR);

        Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.SERVER_LOCAL_IP));
        Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.SERVER_REMOTE_IP));
        Assert.assertNotNull(invokeContext.get(InvokeContext.SERVER_LOCAL_PORT));
        Assert.assertNotNull(invokeContext.get(InvokeContext.SERVER_REMOTE_PORT));
        Assert.assertEquals(invokeContext.get(TESTKEY), "TESTVALUE");
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 6
Source File: CustomSerializerCodecTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneway() throws InterruptedException {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody req = new RequestBody(2, "hello world oneway");
    for (int i = 0; i < invokeTimes; i++) {
        try {
            byte testCodec = (byte) i;
            InvokeContext invokeContext = new InvokeContext();
            invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, testCodec);
            client.oneway(addr, req, invokeContext);

            Assert.assertEquals(testCodec, s1.getContentSerializer());
            Assert.assertEquals(-1, s2.getContentSerializer());
            Thread.sleep(100);
        } catch (RemotingException e) {
            String errMsg = "RemotingException caught in oneway!";
            logger.error(errMsg, e);
            Assert.fail(errMsg);
        }
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 7
Source File: BoltClientTransport.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    try {
        beforeSend(context, request);
        boltInvokeContext.put(RemotingConstants.INVOKE_CTX_RPC_CTX, context);
        return doInvokeAsync(request, context, boltInvokeContext, timeout);
    } catch (Exception e) {
        throw convertToRpcException(e);
    } finally {
        afterSend(context, boltInvokeContext, request);
    }
}
 
Example 8
Source File: BoltClientTransport.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected InvokeContext createInvokeContext(SofaRequest request) {
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, request.getSerializeType());
    invokeContext.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
    invokeContext.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
    String genericType = (String) request.getRequestProp(RemotingConstants.HEAD_GENERIC_TYPE);
    if (genericType != null) {
        invokeContext.put(RemotingConstants.HEAD_GENERIC_TYPE, genericType);
    }
    return invokeContext;
}
 
Example 9
Source File: CustomSerializerCodecTest.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testServerSyncUsingConnection() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    Connection clientConn = client.createStandaloneConnection(ip, port, 1000);

    for (int i = 0; i < invokeTimes; i++) {
        byte testCodec = (byte) i;
        InvokeContext invokeContext = new InvokeContext();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, testCodec);

        RequestBody req1 = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
        String serverres = (String) client.invokeSync(clientConn, req1, invokeContext, 1000);
        Assert.assertEquals(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec, s1.getContentSerializer());
        Assert.assertEquals(testCodec, s2.getContentSerializer());

        Assert.assertNotNull(serverConnectProcessor.getConnection());
        Connection serverConn = serverConnectProcessor.getConnection();

        invokeContext.clear();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, (byte) (testCodec + 1));
        s1.reset();
        s2.reset();

        RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
        String clientres = (String) server.getRpcServer().invokeSync(serverConn, req,
            invokeContext, 1000);
        Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec + 1, s1.getContentSerializer());
        Assert.assertEquals(testCodec + 1, s2.getContentSerializer());
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example 10
Source File: CustomSerializerCodecTest.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testServerSyncUsingAddress() throws Exception {
    NormalRequestBodyCustomSerializer s1 = new NormalRequestBodyCustomSerializer();
    NormalStringCustomSerializer s2 = new NormalStringCustomSerializer();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    Connection clientConn = client.createStandaloneConnection(ip, port, 1000);
    String remote = RemotingUtil.parseRemoteAddress(clientConn.getChannel());
    String local = RemotingUtil.parseLocalAddress(clientConn.getChannel());
    logger.warn("Client say local:" + local);
    logger.warn("Client say remote:" + remote);

    for (int i = 0; i < invokeTimes; i++) {
        byte testCodec = (byte) i;
        InvokeContext invokeContext = new InvokeContext();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, testCodec);

        RequestBody req1 = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
        String serverres = (String) client.invokeSync(clientConn, req1, invokeContext, 1000);
        Assert.assertEquals(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec, s1.getContentSerializer());
        Assert.assertEquals(testCodec, s2.getContentSerializer());

        invokeContext.clear();
        invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, (byte) (testCodec + 1));
        s1.reset();
        s2.reset();

        Assert.assertNotNull(serverConnectProcessor.getConnection());
        // only when client invoked, the remote address can be get by UserProcessor
        // otherwise, please use ConnectionEventProcessor
        String remoteAddr = serverUserProcessor.getRemoteAddr();
        RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
        String clientres = (String) server.getRpcServer().invokeSync(remoteAddr, req,
            invokeContext, 1000);
        Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR + "RANDOM");

        Assert.assertEquals(testCodec + 1, s1.getContentSerializer());
        Assert.assertEquals(testCodec + 1, s2.getContentSerializer());
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}