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

The following examples show how to use com.alipay.remoting.InvokeContext#putIfAbsent() . 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: RpcClientRemoting.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * @see RpcRemoting#preProcessInvokeContext(InvokeContext, RemotingCommand, Connection)
 */
@Override
protected void preProcessInvokeContext(InvokeContext invokeContext, RemotingCommand cmd,
                                       Connection connection) {
    if (null != invokeContext) {
        invokeContext.putIfAbsent(InvokeContext.CLIENT_LOCAL_IP,
            RemotingUtil.parseLocalIP(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.CLIENT_LOCAL_PORT,
            RemotingUtil.parseLocalPort(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.CLIENT_REMOTE_IP,
            RemotingUtil.parseRemoteIP(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.CLIENT_REMOTE_PORT,
            RemotingUtil.parseRemotePort(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.BOLT_INVOKE_REQUEST_ID, cmd.getId());
    }
}
 
Example 2
Source File: RpcClientRemoting.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * Get connection and set init invokeContext if invokeContext not {@code null}
 *
 * @param url target url
 * @param invokeContext invoke context to set
 * @return connection
 */
protected Connection getConnectionAndInitInvokeContext(Url url, InvokeContext invokeContext)
                                                                                            throws RemotingException,
                                                                                            InterruptedException {
    long start = System.currentTimeMillis();
    Connection conn;
    try {
        conn = this.connectionManager.getAndCreateIfAbsent(url);
    } finally {
        if (null != invokeContext) {
            invokeContext.putIfAbsent(InvokeContext.CLIENT_CONN_CREATETIME,
                (System.currentTimeMillis() - start));
        }
    }
    return conn;
}
 
Example 3
Source File: BasicUsage_ProtocolV2_2_Test.java    From sofa-bolt with Apache License 2.0 6 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 {
            String res = null;
            if (i % 2 == 0) {
                client.oneway(addr, req);
            } else {
                InvokeContext invokeContext = new InvokeContext();
                invokeContext.putIfAbsent(InvokeContext.BOLT_CRC_SWITCH, false);
                client.oneway(addr, req, invokeContext);
            }
            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: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test custom serializer using invoke contxt in sync
 *
 * @throws Exception
 */
@Test
public void testInvokeContextCustomSerializer_SYNC() throws Exception {
    NormalRequestBodyCustomSerializer_InvokeContext s1 = new NormalRequestBodyCustomSerializer_InvokeContext();
    NormalStringCustomSerializer_InvokeContext s2 = new NormalStringCustomSerializer_InvokeContext();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE1_value);
    String ret = (String) client.invokeSync(addr, body, invokeContext, 1000);
    Assert.assertEquals(RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM", ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());

    invokeContext.clear();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE2_value);
    ret = (String) client.invokeSync(addr, body, invokeContext, 1000);
    Assert.assertEquals(NormalStringCustomSerializer_InvokeContext.UNIVERSAL_RESP, ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());
}
 
Example 5
Source File: BasicUsage_ProtocolV1_Test.java    From sofa-bolt with Apache License 2.0 6 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 {
            String res = null;
            if (i % 2 == 0) {
                client.oneway(addr, req);
            } else {
                InvokeContext invokeContext = new InvokeContext();
                invokeContext.putIfAbsent(InvokeContext.BOLT_CRC_SWITCH, false);
                client.oneway(addr, req, invokeContext);
            }
            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 6
Source File: BasicUsage_ProtocolV2_1_Test.java    From sofa-bolt with Apache License 2.0 6 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 {
            String res = null;
            if (i % 2 == 0) {
                client.oneway(addr, req);
            } else {
                InvokeContext invokeContext = new InvokeContext();
                invokeContext.putIfAbsent(InvokeContext.BOLT_CRC_SWITCH, false);
                client.oneway(addr, req, invokeContext);
            }
            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: RpcServerRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
protected void preProcessInvokeContext(InvokeContext invokeContext, RemotingCommand cmd,
                                       Connection connection) {
    if (null != invokeContext) {
        invokeContext.putIfAbsent(InvokeContext.SERVER_REMOTE_IP,
            RemotingUtil.parseRemoteIP(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.SERVER_REMOTE_PORT,
            RemotingUtil.parseRemotePort(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.SERVER_LOCAL_IP,
            RemotingUtil.parseLocalIP(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.SERVER_LOCAL_PORT,
            RemotingUtil.parseLocalPort(connection.getChannel()));
        invokeContext.putIfAbsent(InvokeContext.BOLT_INVOKE_REQUEST_ID, cmd.getId());
    }
}
 
Example 8
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * test custom serializer using invoke contxt in future
 * @throws Exception
 */
@Test
public void testInvokeContextCustomSerializer_FUTURE() throws Exception {
    NormalRequestBodyCustomSerializer_InvokeContext s1 = new NormalRequestBodyCustomSerializer_InvokeContext();
    NormalStringCustomSerializer_InvokeContext s2 = new NormalStringCustomSerializer_InvokeContext();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE1_value);
    RpcResponseFuture future = client.invokeWithFuture(addr, body, invokeContext, 1000);
    String ret = (String) future.get(1000);
    Assert.assertEquals(RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM", ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());

    invokeContext.clear();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE2_value);
    future = client.invokeWithFuture(addr, body, invokeContext, 1000);
    ret = (String) future.get(1000);
    Assert.assertEquals(NormalStringCustomSerializer_InvokeContext.UNIVERSAL_RESP, ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());
}