Search in sources :

Example 1 with CommunicationConnection

use of com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection 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 CommunicationConnection

use of com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection 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 3 with CommunicationConnection

use of com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection 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 4 with CommunicationConnection

use of com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection 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)

Example 5 with CommunicationConnection

use of com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection in project otter by alibaba.

the class DubboConnectionTest method testSingle.

@Test
public void testSingle() {
    CommunicationConnectionFactory factory = new DubboCommunicationConnectionFactory();
    CommunicationParam param = new CommunicationParam();
    param.setIp("127.0.0.1");
    param.setPort(2088);
    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) DubboCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationConnectionFactory) CommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnectionFactory) DubboCommunicationConnectionFactory(com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationConnectionFactory) Test(org.testng.annotations.Test)

Aggregations

CommunicationConnection (com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection)5 CommunicationParam (com.alibaba.otter.shared.communication.core.model.CommunicationParam)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 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 CommunicationException (com.alibaba.otter.shared.communication.core.exception.CommunicationException)1 DubboCommunicationConnectionFactory (com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationConnectionFactory)1 UnknownHostException (java.net.UnknownHostException)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