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);
}
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;
}
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);
}
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();
}
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();
}
}
}
Aggregations