Search in sources :

Example 1 with CommunicationParam

use of com.alibaba.otter.shared.communication.core.model.CommunicationParam in project otter by alibaba.

the class RmiConnectionTest method testPool_exhaust.

@Test
public void testPool_exhaust() {
    CommunicationConnectionFactory factory = new RmiCommunicationConnectionFactory();
    CommunicationConnectionFactory poolFactory = new CommunicationConnectionPoolFactory(factory);
    ((CommunicationConnectionPoolFactory) poolFactory).initial();
    CommunicationParam param = new CommunicationParam();
    param.setIp("127.0.0.1");
    param.setPort(1099);
    CommunicationRegistry.regist(PoolEventType.exhaust, new TestPoolService());
    ExecutorService executor = Executors.newCachedThreadPool();
    long start = System.currentTimeMillis();
    final CountDownLatch count = new CountDownLatch(11);
    for (int i = 0; i < 11; i++) {
        final CommunicationConnection connection = poolFactory.createConnection(param);
        final PoolEvent event = new PoolEvent(PoolEventType.exhaust);
        event.setSleep(1000);
        executor.submit(new Callable() {

            public Object call() throws Exception {
                try {
                    Object obj = connection.call(event);
                    count.countDown();
                    return obj;
                } finally {
                    connection.close();
                }
            }
        });
    }
    try {
        count.await();
    } catch (InterruptedException e) {
        want.fail();
    }
    long end = System.currentTimeMillis();
    want.number(end - start).isGe(1500L).isLe(2500L);
}
Also used : CommunicationConnectionPoolFactory(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionPoolFactory) RmiCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory) CountDownLatch(java.util.concurrent.CountDownLatch) RmiCommunicationEndpoint(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationEndpoint) Callable(java.util.concurrent.Callable) CommunicationConnection(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection) CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) ExecutorService(java.util.concurrent.ExecutorService) RmiCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory) CommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionFactory) Test(org.testng.annotations.Test)

Example 2 with CommunicationParam

use of com.alibaba.otter.shared.communication.core.model.CommunicationParam in project otter by alibaba.

the class DefaultCommunicationClientImpl method buildParams.

// ===================== helper method ==================
private CommunicationParam buildParams(String addr) {
    CommunicationParam params = new CommunicationParam();
    String[] strs = StringUtils.split(addr, ":");
    if (strs == null || strs.length != 2) {
        throw new IllegalArgumentException("addr example: 127.0.0.1:1099");
    }
    InetAddress address = null;
    try {
        address = InetAddress.getByName(strs[0]);
    } catch (UnknownHostException e) {
        throw new CommunicationException("addr_error", "addr[" + addr + "] is unknow!");
    }
    params.setIp(address.getHostAddress());
    params.setPort(Integer.valueOf(strs[1]));
    return params;
}
Also used : CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) UnknownHostException(java.net.UnknownHostException) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) InetAddress(java.net.InetAddress)

Example 3 with CommunicationParam

use of com.alibaba.otter.shared.communication.core.model.CommunicationParam in project otter by alibaba.

the class DefaultCommunicationClientImpl method call.

public Object call(final String addr, final Event event) {
    Assert.notNull(this.factory, "No factory specified");
    CommunicationParam params = buildParams(addr);
    CommunicationConnection connection = null;
    int count = 0;
    Throwable ex = null;
    while (count++ < retry) {
        try {
            connection = factory.createConnection(params);
            return connection.call(event);
        } catch (Exception e) {
            logger.error(String.format("call[%s] , retry[%s]", addr, count), e);
            try {
                Thread.sleep(count * retryDelay);
            } catch (InterruptedException e1) {
            // ignore
            }
            ex = e;
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
    logger.error("call[{}] failed , event[{}]!", addr, event.toString());
    throw new CommunicationException("call[" + addr + "] , Event[" + event.toString() + "]", ex);
}
Also used : CommunicationConnection(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection) CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with CommunicationParam

use of com.alibaba.otter.shared.communication.core.model.CommunicationParam in project otter by alibaba.

the class RmiConnectionTest method testSingle.

@Test
public void testSingle() {
    CommunicationConnectionFactory factory = new RmiCommunicationConnectionFactory();
    CommunicationParam param = new CommunicationParam();
    param.setIp("127.0.0.1");
    param.setPort(1099);
    CommunicationConnection connection = factory.createConnection(param);
    Object result = connection.call(new HeartEvent());
    want.object(result).notNull();
}
Also used : CommunicationConnection(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection) HeartEvent(com.alibaba.otter.shared.communication.core.model.heart.HeartEvent) CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) RmiCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory) RmiCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory) CommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionFactory) Test(org.testng.annotations.Test)

Example 5 with CommunicationParam

use of com.alibaba.otter.shared.communication.core.model.CommunicationParam in project otter by alibaba.

the class RmiConnectionTest method testPool.

@Test
public void testPool() {
    CommunicationConnectionFactory factory = new RmiCommunicationConnectionFactory();
    CommunicationConnectionFactory poolFactory = new CommunicationConnectionPoolFactory(factory);
    ((CommunicationConnectionPoolFactory) poolFactory).initial();
    CommunicationParam param = new CommunicationParam();
    param.setIp("127.0.0.1");
    param.setPort(1099);
    CommunicationRegistry.regist(PoolEventType.pool, new TestPoolService());
    CommunicationConnection last = null;
    for (int i = 0; i < 11; i++) {
        CommunicationConnection connection = null;
        try {
            connection = poolFactory.createConnection(param);
            connection.call(new PoolEvent(PoolEventType.pool));
            last = connection;
            if (last != null) {
                // 检查链接是否是重用
                want.object(last).isEqualTo(connection);
            }
        } finally {
            connection.close();
        }
    }
}
Also used : CommunicationConnection(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection) CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) CommunicationConnectionPoolFactory(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionPoolFactory) RmiCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory) RmiCommunicationEndpoint(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationEndpoint) RmiCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory) CommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionFactory) Test(org.testng.annotations.Test)

Aggregations

CommunicationParam (com.alibaba.otter.shared.communication.core.model.CommunicationParam)6 CommunicationConnection (com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection)5 CommunicationConnectionFactory (com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionFactory)4 Test (org.testng.annotations.Test)4 RmiCommunicationConnectionFactory (com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationConnectionFactory)3 CommunicationException (com.alibaba.otter.shared.communication.core.exception.CommunicationException)2 CommunicationConnectionPoolFactory (com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionPoolFactory)2 RmiCommunicationEndpoint (com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationEndpoint)2 HeartEvent (com.alibaba.otter.shared.communication.core.model.heart.HeartEvent)2 UnknownHostException (java.net.UnknownHostException)2 DubboCommunicationConnectionFactory (com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationConnectionFactory)1 InetAddress (java.net.InetAddress)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1