com.alipay.remoting.exception.RemotingException Java Examples

The following examples show how to use com.alipay.remoting.exception.RemotingException. 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: 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 #2
Source File: RpcRemoting.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * Synchronous rpc invocation.<br>
 * Notice! DO NOT modify the request object concurrently when this method is called.
 * 
 * @param conn
 * @param request
 * @param invokeContext 
 * @param timeoutMillis
 * @return
 * @throws RemotingException
 * @throws InterruptedException
 */
public Object invokeSync(final Connection conn, final Object request,
                         final InvokeContext invokeContext, final int timeoutMillis)
                                                                                    throws RemotingException,
                                                                                    InterruptedException {
    RemotingCommand requestCommand = toRemotingCommand(request, conn, invokeContext,
        timeoutMillis);
    preProcessInvokeContext(invokeContext, requestCommand, conn);
    ResponseCommand responseCommand = (ResponseCommand) super.invokeSync(conn, requestCommand,
        timeoutMillis);
    responseCommand.setInvokeContext(invokeContext);

    Object responseObject = RpcResponseResolver.resolveResponseObject(responseCommand,
        RemotingUtil.parseRemoteAddress(conn.getChannel()));
    return responseObject;
}
 
Example #3
Source File: BoltClient.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
@Override
public Channel connect(URL url) {
    if (url == null) {
        throw new IllegalArgumentException("Create connection url can not be null!");
    }
    try {
        Connection connection = getBoltConnection(rpcClient, url);
        BoltChannel channel = new BoltChannel();
        channel.setConnection(connection);
        return channel;

    } catch (RemotingException e) {
        LOGGER
            .error("Bolt client connect server got a RemotingException! target url:" + url, e);
        throw new RuntimeException("Bolt client connect server got a RemotingException!", e);
    }
}
 
Example #4
Source File: BoltClient.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
protected Connection getBoltConnection(RpcClient rpcClient, URL url) throws RemotingException {
    Url boltUrl = createBoltUrl(url);
    try {
        Connection connection = rpcClient.getConnection(boltUrl, connectTimeout);
        if (connection == null || !connection.isFine()) {
            if (connection != null) {
                connection.close();
            }
            throw new RemotingException("Get bolt connection failed for boltUrl: " + boltUrl);
        }
        return connection;
    } catch (InterruptedException e) {
        throw new RuntimeException(
            "BoltClient rpcClient.getConnection InterruptedException! target boltUrl:"
                    + boltUrl, e);
    }
}
 
Example #5
Source File: BasicUsage_AsyncProcessor_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 {
            client.oneway(addr, req);
            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: HeartBeatDisableTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientHeartBeatNotTrigger() throws InterruptedException {
    server.getRpcServer().registerProcessor(RpcProtocol.PROTOCOL_CODE,
        CommonCommandCode.HEARTBEAT, heartBeatProcessor);
    try {
        client.createStandaloneConnection(addr, 1000);
    } catch (RemotingException e) {
        logger.error("", e);
    }
    Thread.sleep(500);
    Assert.assertEquals(0, heartBeatProcessor.getHeartBeatTimes());
    Assert.assertEquals(1, clientConnectProcessor.getConnectTimes());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(0, clientDisConnectProcessor.getDisConnectTimes());// not closed,because heartbeat disabled
    Assert.assertEquals(0, serverDisConnectProcessor.getDisConnectTimes());// not closed,because heartbeat disabled
}
 
Example #7
Source File: BasicUsageTest.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 {
            client.oneway(addr, req);
            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 #8
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 #9
Source File: BasicUsageDemoByJunit.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 {
            client.oneway(addr, req);
            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 #10
Source File: BasicUsageTest.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 {
            client.oneway(addr, req);
            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 #11
Source File: RpcClientRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.rpc.RpcRemoting#oneway(com.alipay.remoting.Url, java.lang.Object, InvokeContext)
 */
@Override
public void oneway(Url url, Object request, InvokeContext invokeContext)
                                                                        throws RemotingException,
                                                                        InterruptedException {
    final Connection conn = getConnectionAndInitInvokeContext(url, invokeContext);
    this.connectionManager.check(conn);
    this.oneway(conn, request, invokeContext);
}
 
Example #12
Source File: RpcResponseFuture.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * get result with timeout specified
 * 
 * if request done, resolve normal responseObject
 * if request not done, throws InvokeTimeoutException
 */
public Object get(int timeoutMillis) throws InvokeTimeoutException, RemotingException,
                                    InterruptedException {
    this.future.waitResponse(timeoutMillis);
    if (!isDone()) {
        throw new InvokeTimeoutException("Future get result timeout!");
    }
    ResponseCommand responseCommand = (ResponseCommand) this.future.waitResponse();
    responseCommand.setInvokeContext(this.future.getInvokeContext());
    return RpcResponseResolver.resolveResponseObject(responseCommand, addr);
}
 
Example #13
Source File: RpcClientRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.rpc.RpcRemoting#invokeWithFuture(com.alipay.remoting.Url, java.lang.Object, InvokeContext, int)
 */
@Override
public RpcResponseFuture invokeWithFuture(Url url, Object request, InvokeContext invokeContext,
                                          int timeoutMillis) throws RemotingException,
                                                            InterruptedException {
    final Connection conn = getConnectionAndInitInvokeContext(url, invokeContext);
    this.connectionManager.check(conn);
    return this.invokeWithFuture(conn, request, invokeContext, timeoutMillis);
}
 
Example #14
Source File: RpcClientRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.rpc.RpcRemoting#invokeSync(com.alipay.remoting.Url, java.lang.Object, InvokeContext, int)
 */
@Override
public Object invokeSync(Url url, Object request, InvokeContext invokeContext, int timeoutMillis)
                                                                                                 throws RemotingException,
                                                                                                 InterruptedException {
    final Connection conn = getConnectionAndInitInvokeContext(url, invokeContext);
    this.connectionManager.check(conn);
    return this.invokeSync(conn, request, invokeContext, timeoutMillis);
}
 
Example #15
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public Object invokeSync(final String address, final Object request,
                         final InvokeContext invokeContext, final int timeoutMillis)
                                                                                    throws RemotingException,
                                                                                    InterruptedException {
    return this.rpcRemoting.invokeSync(address, request, invokeContext, timeoutMillis);
}
 
Example #16
Source File: RpcAddressParserTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testParserConnectTimeout() throws RemotingException {
    String url = "127.0.0.1:1111?_CONNECTTIMEOUT=2000&_TIMEOUT=3000&_SERIALIZETYPE=hessian2&_PROTOCOL=1";
    RpcAddressParser parser = new RpcAddressParser();
    Url btUrl = parser.parse(url);
    Assert.assertEquals("127.0.0.1", btUrl.getIp());
    Assert.assertEquals(1111, btUrl.getPort());
    Assert.assertEquals("1", btUrl.getProperty(RpcConfigs.URL_PROTOCOL));
    Assert.assertEquals("2000", btUrl.getProperty(RpcConfigs.CONNECT_TIMEOUT_KEY));
}
 
Example #17
Source File: ServerBasicUsageTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneway() throws InterruptedException, RemotingException {
    client.getConnection(addr, 1000);

    RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
    for (int i = 0; i < invokeTimes; i++) {
        try {
            // only when client invoked, the remote address can be get by UserProcessor
            // otherwise, please use ConnectionEventProcessor
            String remoteAddr = serverUserProcessor.getRemoteAddr();
            Assert.assertNull(remoteAddr);
            remoteAddr = serverConnectProcessor.getRemoteAddr();
            Assert.assertNotNull(remoteAddr);
            server.getRpcServer().oneway(remoteAddr, req);
            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(0, serverUserProcessor.getInvokeTimes());
    Assert.assertEquals(invokeTimes, clientUserProcessor.getInvokeTimes());
}
 
Example #18
Source File: RpcServerRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.rpc.RpcRemoting#oneway(com.alipay.remoting.Url, java.lang.Object, InvokeContext)
 */
@Override
public void oneway(Url url, Object request, InvokeContext invokeContext)
                                                                        throws RemotingException {
    Connection conn = this.connectionManager.get(url.getUniqueKey());
    if (null == conn) {
        throw new RemotingException("Client address [" + url.getOriginUrl()
                                    + "] not connected yet!");
    }
    this.connectionManager.check(conn);
    this.oneway(conn, request, invokeContext);
}
 
Example #19
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 #20
Source File: ClientConnection.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke sync object.
 *
 * @param request the request  
 * @return the object  
 * @throws RemotingException the remoting exception  
 * @throws InterruptedException the interrupted exception
 */
@Override
public Object invokeSync(Object request) throws RemotingException, InterruptedException {
    if (!isConnected()) {
        throw new IllegalStateException("Not connected");
    }

    return client.invokeSync(clientConnection, request, config.getInvokeTimeout());
}
 
Example #21
Source File: DefaultConnectionManager.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.ConnectionManager#create(java.lang.String, int)
 */
@Override
public Connection create(String address, int connectTimeout) throws RemotingException {
    Url url = this.addressParser.parse(address);
    url.setConnectTimeout(connectTimeout);
    return create(url);
}
 
Example #22
Source File: RpcConnectionManagerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateUsingUrl() {
    try {
        Connection conn = cm.create(url);
        Assert.assertNotNull(conn);
    } catch (RemotingException e) {
        Assert.fail("should not reach here!");
    }

}
 
Example #23
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public RpcResponseFuture invokeWithFuture(final String address, final Object request,
                                          final InvokeContext invokeContext,
                                          final int timeoutMillis) throws RemotingException,
                                                                  InterruptedException {
    return this.rpcRemoting.invokeWithFuture(address, request, invokeContext, timeoutMillis);
}
 
Example #24
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public RpcResponseFuture invokeWithFuture(final Url url, final Object request,
                                          final InvokeContext invokeContext,
                                          final int timeoutMillis) throws RemotingException,
                                                                  InterruptedException {
    return this.rpcRemoting.invokeWithFuture(url, request, invokeContext, timeoutMillis);
}
 
Example #25
Source File: DefaultConnectionManager.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.ConnectionManager#create(com.alipay.remoting.Url)
 */
@Override
public Connection create(Url url) throws RemotingException {
    Connection conn;
    try {
        conn = this.connectionFactory.createConnection(url);
    } catch (Exception e) {
        throw new RemotingException("Create connection failed. The address is "
                                    + url.getOriginUrl(), e);
    }
    return conn;
}
 
Example #26
Source File: ServerInvokeExceptionTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnClose() throws InterruptedException, RemotingException {
    client.getConnection(addr, 1000);

    RequestBody req = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
    for (int i = 0; i < invokeTimes; i++) {
        try {
            // only when client invoked, the remote address can be get by UserProcessor
            // otherwise, please use ConnectionEventProcessor
            String remoteAddr = serverUserProcessor.getRemoteAddr();
            Assert.assertNull(remoteAddr);
            remoteAddr = serverConnectProcessor.getRemoteAddr();
            Assert.assertNotNull(remoteAddr);
            Connection serverConn = serverConnectProcessor.getConnection();
            String clientres = (String) server.getRpcServer().invokeSync(remoteAddr, req, 1000);
            Assert.assertEquals(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR);
            Assert.assertTrue(server.getRpcServer().isConnected(remoteAddr));
            serverConn.close();
            Thread.sleep(100);
            Assert.assertFalse(server.getRpcServer().isConnected(remoteAddr));
            clientres = (String) server.getRpcServer().invokeSync(remoteAddr, req, 1000);
            Assert.fail("Connection removed! Should throw exception here.");
        } catch (RemotingException e) {
            logger.error(e.getMessage());
            Assert.assertTrue(e.getMessage().contains("not connected yet!"));
        }
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(0, serverUserProcessor.getInvokeTimes());
    Assert.assertEquals(invokeTimes, clientUserProcessor.getInvokeTimes());
}
 
Example #27
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public void invokeWithCallback(final String address, final Object request,
                               final InvokeContext invokeContext,
                               final InvokeCallback invokeCallback, final int timeoutMillis)
                                                                                            throws RemotingException,
                                                                                            InterruptedException {
    this.rpcRemoting.invokeWithCallback(address, request, invokeContext, invokeCallback,
        timeoutMillis);
}
 
Example #28
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public void invokeWithCallback(final Url url, final Object request,
                               final InvokeCallback invokeCallback, final int timeoutMillis)
                                                                                            throws RemotingException,
                                                                                            InterruptedException {
    this.rpcRemoting.invokeWithCallback(url, request, null, invokeCallback, timeoutMillis);
}
 
Example #29
Source File: ScheduledDisconnectStrategyTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisconnectStrategy_byUserSetting() throws InterruptedException,
                                                  RemotingException {
    System.setProperty(Configs.CONN_MONITOR_SWITCH, "true");
    System.setProperty(Configs.CONN_MONITOR_INITIAL_DELAY, "2000");
    System.setProperty(Configs.CONN_MONITOR_PERIOD, "100");
    System.setProperty(Configs.CONN_THRESHOLD, "0");
    doInit(false, true);
    String addr = "127.0.0.1:" + port + "?zone=RZONE&_CONNECTIONNUM=8";
    Url url = addressParser.parse(addr);

    for (int i = 0; i < 8; i++) {
        client.getConnection(url, 1000);
    }
    Thread.sleep(2100);
    Assert.assertTrue(0 <= clientDisConnectProcessor.getDisConnectTimes());
    Thread.sleep(200);
    Assert.assertTrue(2 <= clientDisConnectProcessor.getDisConnectTimes());
    Thread.sleep(400);
    Assert.assertTrue(4 <= clientDisConnectProcessor.getDisConnectTimes());
    Thread.sleep(200);
    Assert.assertTrue(5 <= clientDisConnectProcessor.getDisConnectTimes());
    Thread.sleep(200);
    Assert.assertTrue(5 <= clientDisConnectProcessor.getDisConnectTimes());
    Thread.sleep(100);
    Assert.assertTrue(6 <= clientDisConnectProcessor.getDisConnectTimes());
}
 
Example #30
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public void invokeWithCallback(final Connection conn, final Object request,
                               final InvokeContext invokeContext,
                               final InvokeCallback invokeCallback, final int timeoutMillis)
                                                                                            throws RemotingException {
    this.rpcRemoting.invokeWithCallback(conn, request, invokeContext, invokeCallback,
        timeoutMillis);
}