Search in sources :

Example 1 with Url

use of com.alipay.remoting.Url in project sofa-rpc by sofastack.

the class BoltClientTransport method convertProviderToUrl.

/**
 * For convert provider to bolt url.
 *
 * @param transportConfig ClientTransportConfig
 * @param providerInfo    ProviderInfo
 * @return Bolt Url
 */
protected Url convertProviderToUrl(ClientTransportConfig transportConfig, ProviderInfo providerInfo) {
    // Url的第一个参数,如果不用事件的话,其实无所谓
    Url boltUrl = new Url(providerInfo.toString(), providerInfo.getHost(), providerInfo.getPort());
    boltUrl.setConnectTimeout(transportConfig.getConnectTimeout());
    // 默认初始化connNum个长连接,为了slb和vip的情况
    final int connectionNum = transportConfig.getConnectionNum();
    if (connectionNum > 0) {
        boltUrl.setConnNum(connectionNum);
    } else {
        boltUrl.setConnNum(1);
    }
    // true的话
    boltUrl.setConnWarmup(false);
    if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(providerInfo.getProtocolType())) {
        boltUrl.setProtocol(RemotingConstants.PROTOCOL_BOLT);
    } else {
        boltUrl.setProtocol(RemotingConstants.PROTOCOL_TR);
    }
    return boltUrl;
}
Also used : Url(com.alipay.remoting.Url)

Example 2 with Url

use of com.alipay.remoting.Url in project sofa-rpc by sofastack.

the class ReuseBoltClientConnectionManagerTest method testConcurrentCreate.

@Test
public void testConcurrentCreate() throws Exception {
    final ReuseBoltClientConnectionManager manager = new ReuseBoltClientConnectionManager(false);
    final ClientTransportConfig config = buildConfig(12222);
    // 并发创建
    final CountDownLatch latch = new CountDownLatch(5);
    List<Thread> threads = new ArrayList<Thread>(5);
    final Url url = buildUrl(config);
    for (int i = 0; i < 5; i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Connection innerConnection = manager.getConnection(rpcClient, config, url);
                    System.out.println("url=" + url + ",connection=" + innerConnection);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    latch.countDown();
                }
            }
        }, "thread" + i);
        threads.add(thread);
    }
    for (Thread thread : threads) {
        thread.start();
    }
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, manager.urlConnectionMap.size());
    Assert.assertEquals(1, manager.connectionRefCounter.size());
    Connection connection = manager.getConnection(rpcClient, config, url);
    Assert.assertNotNull(connection);
    final AtomicInteger atomicInteger = manager.connectionRefCounter.get(connection);
    Assert.assertEquals(1, atomicInteger.get());
    // 检查泄漏
    manager.checkLeak();
    Assert.assertTrue(CommonUtils.isEmpty(manager.urlConnectionMap));
    Assert.assertTrue(CommonUtils.isEmpty(manager.connectionRefCounter));
}
Also used : ArrayList(java.util.ArrayList) Connection(com.alipay.remoting.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Url(com.alipay.remoting.Url) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 3 with Url

use of com.alipay.remoting.Url in project sofa-rpc by sofastack.

the class ReuseBoltClientConnectionManagerTest method buildUrl.

private Url buildUrl(ClientTransportConfig clientTransportConfig) {
    ProviderInfo providerInfo2 = clientTransportConfig.getProviderInfo();
    Url url = new Url(providerInfo2.toString(), providerInfo2.getHost(), providerInfo2.getPort());
    url.setConnectTimeout(4500);
    url.setProtocol(RemotingConstants.PROTOCOL_BOLT);
    // 默认初始化connNum个长连接
    url.setConnNum(1);
    url.setConnWarmup(false);
    return url;
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) Url(com.alipay.remoting.Url)

Aggregations

Url (com.alipay.remoting.Url)3 Connection (com.alipay.remoting.Connection)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)1 ClientTransportConfig (com.alipay.sofa.rpc.transport.ClientTransportConfig)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1