Java Code Examples for com.alipay.remoting.Connection#close()

The following examples show how to use com.alipay.remoting.Connection#close() . 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: 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 2
Source File: ReconnectManagerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Test
public void testReconnectionByUserSetting() throws InterruptedException, RemotingException {
    doInit(false, true);
    client.enableReconnectSwitch();

    String addr = "127.0.0.1:2014?zone=RZONE&_CONNECTIONNUM=1";
    Url url = addressParser.parse(addr);

    Connection connection = client.getConnection(url, 1000);
    Assert.assertEquals(0, clientDisConnectProcessor.getDisConnectTimes());
    Assert.assertEquals(1, clientConnectProcessor.getConnectTimes());
    connection.close();
    Thread.sleep(2000);
    Assert.assertEquals(1, clientDisConnectProcessor.getDisConnectTimes());
    Assert.assertEquals(2, clientConnectProcessor.getConnectTimes());
}
 
Example 3
Source File: RpcConnectionManagerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectionCloseAndConnectionManagerRemove() throws RemotingException,
                                                           InterruptedException {
    final Url addr = new Url(ip, port);

    this.addressParser.initUrlArgs(addr);
    Connection conn = null;
    try {
        conn = cm.getAndCreateIfAbsent(addr);
    } catch (InterruptedException e) {
        Assert.fail("InterruptedException!");
    }

    Assert.assertEquals(1, cm.count(addr.getUniqueKey()));
    conn.close();
    Thread.sleep(100);
    Assert.assertEquals(0, cm.count(addr.getUniqueKey()));
}
 
Example 4
Source File: BoltServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
@Override
public void close(Channel channel) {
    if (null != channel) {
        channels.remove(NetUtil.toAddressString(channel.getRemoteAddress()));
        BoltChannel boltChannel = (BoltChannel) channel;
        Connection connection = boltChannel.getConnection();
        if (null != connection && connection.isFine()) {
            connection.close();
        }
    }
}
 
Example 5
Source File: DataServerNodeFactory.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * remove dataserver by specific datacenter and ip
 *
 * @param dataCenter
 * @param ip
 */
public static void remove(String dataCenter, String ip, DataServerConfig dataServerConfig) {
    if (MAP.containsKey(dataCenter)) {
        Map<String, DataServerNode> map = MAP.get(dataCenter);
        if (map != null) {
            DataServerNode dataServerNode = map.get(ip);
            Connection connection = dataServerNode.getConnection();
            if (connection != null && connection.isFine()) {
                connection.close();
            }
            map.remove(ip);
        }
    }
    refreshConsistent(dataCenter, dataServerConfig);
}
 
Example 6
Source File: MetaServerConnectionFactory.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param dataCenter
 */
public void remove(String dataCenter) {
    Map<String, Connection> map = getConnections(dataCenter);
    if (!map.isEmpty()) {
        for (Connection connection : map.values()) {
            if (connection.isFine()) {
                connection.close();
            }
        }
    }
    MAP.remove(dataCenter);
}
 
Example 7
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 8
Source File: ReconnectManagerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testReconnectionBySysetmSetting() throws InterruptedException, RemotingException {
    doInit(true, false);
    String addr = "127.0.0.1:2014?zone=RZONE&_CONNECTIONNUM=1";
    Url url = addressParser.parse(addr);

    Connection connection = client.getConnection(url, 1000);
    Assert.assertEquals(0, clientDisConnectProcessor.getDisConnectTimes());
    Assert.assertEquals(1, clientConnectProcessor.getConnectTimes());
    connection.close();
    Thread.sleep(2000);
    Assert.assertEquals(1, clientDisConnectProcessor.getDisConnectTimes());
    Assert.assertEquals(2, clientConnectProcessor.getConnectTimes());
}
 
Example 9
Source File: ClientConnectionTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckConnection() throws InterruptedException {
    Connection conn = null;
    try {
        conn = client.getConnection(addr, 3000);
    } catch (RemotingException e) {
        Assert.fail("should not reach here");
    }
    Assert.assertTrue(client.checkConnection(addr));
    conn.close();
    Thread.sleep(100);
    Assert.assertFalse(client.checkConnection(addr));
}
 
Example 10
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
@Override
public void closeStandaloneConnection(Connection conn) {
    if (null != conn) {
        conn.close();
    }
}