use of io.netty.util.concurrent.FutureListener in project netty by netty.
the class DefaultHttp2ConnectionTest method removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs.
@Test
public void removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs() throws Exception {
final Endpoint<Http2RemoteFlowController> remote = client.remote();
final Endpoint<Http2LocalFlowController> local = client.local();
for (int c = 3, s = 2; c < 5000; c += 2, s += 2) {
local.createStream(c, false);
remote.createStream(s, false);
}
final Promise<Void> promise = group.next().newPromise();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.forEachActiveStream(new Http2StreamVisitor() {
@Override
public boolean visit(Http2Stream stream) throws Http2Exception {
// This close call is basically a noop, because the following statement will throw an exception.
client.close(promise);
// Do an invalid operation while iterating.
remote.createStream(3, false);
return true;
}
});
} catch (Http2Exception ignored) {
client.close(promise).addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> future) throws Exception {
assertTrue(promise.isDone());
latch.countDown();
}
});
}
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of io.netty.util.concurrent.FutureListener in project netty by netty.
the class DnsAddressResolverGroupTest method testUseConfiguredEventLoop.
@Test
public void testUseConfiguredEventLoop() throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(1);
final EventLoop loop = group.next();
DefaultEventLoopGroup defaultEventLoopGroup = new DefaultEventLoopGroup(1);
DnsNameResolverBuilder builder = new DnsNameResolverBuilder().eventLoop(loop).channelType(NioDatagramChannel.class);
DnsAddressResolverGroup resolverGroup = new DnsAddressResolverGroup(builder);
try {
final Promise<?> promise = loop.newPromise();
AddressResolver<?> resolver = resolverGroup.getResolver(defaultEventLoopGroup.next());
resolver.resolve(new SocketAddress() {
private static final long serialVersionUID = 3169703458729818468L;
}).addListener(new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) {
try {
assertThat(future.cause(), instanceOf(UnsupportedAddressTypeException.class));
assertTrue(loop.inEventLoop());
promise.setSuccess(null);
} catch (Throwable cause) {
promise.setFailure(cause);
}
}
}).await();
promise.sync();
} finally {
resolverGroup.close();
group.shutdownGracefully();
defaultEventLoopGroup.shutdownGracefully();
}
}
use of io.netty.util.concurrent.FutureListener in project redisson by redisson.
the class ClusterConnectionManager method connect.
private RFuture<RedisConnection> connect(ClusterServersConfig cfg, final URL addr) {
RedisConnection connection = nodeConnections.get(addr);
if (connection != null) {
return newSucceededFuture(connection);
}
RedisClient client = createClient(addr.getHost(), addr.getPort(), cfg.getConnectTimeout(), cfg.getRetryInterval() * cfg.getRetryAttempts());
final RPromise<RedisConnection> result = newPromise();
RFuture<RedisConnection> future = client.connectAsync();
future.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RedisConnection connection = future.getNow();
RPromise<RedisConnection> promise = newPromise();
connectListener.onConnect(promise, connection, null, config);
promise.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RedisConnection connection = future.getNow();
if (connection.isActive()) {
nodeConnections.put(addr, connection);
result.trySuccess(connection);
} else {
connection.closeAsync();
result.tryFailure(new RedisException("Connection to " + connection.getRedisClient().getAddr() + " is not active!"));
}
}
});
}
});
return result;
}
use of io.netty.util.concurrent.FutureListener in project redisson by redisson.
the class LoadBalancerManager method add.
public RFuture<Void> add(final ClientConnectionsEntry entry) {
final RPromise<Void> result = connectionManager.newPromise();
FutureListener<Void> listener = new FutureListener<Void>() {
AtomicInteger counter = new AtomicInteger(2);
@Override
public void operationComplete(Future<Void> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
if (counter.decrementAndGet() == 0) {
addr2Entry.put(entry.getClient().getAddr(), entry);
result.trySuccess(null);
}
}
};
RFuture<Void> slaveFuture = slaveConnectionPool.add(entry);
slaveFuture.addListener(listener);
RFuture<Void> pubSubFuture = pubSubConnectionPool.add(entry);
pubSubFuture.addListener(listener);
return result;
}
use of io.netty.util.concurrent.FutureListener in project redisson by redisson.
the class CommandAsyncService method readAllAsync.
@Override
public <T, R> RFuture<Collection<R>> readAllAsync(RedisCommand<T> command, Object... params) {
final RPromise<Collection<R>> mainPromise = connectionManager.newPromise();
final Set<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
final List<R> results = new ArrayList<R>();
final AtomicInteger counter = new AtomicInteger(nodes.size());
FutureListener<R> listener = new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
if (!future.isSuccess()) {
mainPromise.tryFailure(future.cause());
return;
}
R result = future.getNow();
if (result instanceof Collection) {
synchronized (results) {
results.addAll((Collection) result);
}
} else {
synchronized (results) {
results.add(result);
}
}
if (counter.decrementAndGet() == 0 && !mainPromise.isDone()) {
mainPromise.trySuccess(results);
}
}
};
for (MasterSlaveEntry entry : nodes) {
RPromise<R> promise = connectionManager.newPromise();
promise.addListener(listener);
async(true, new NodeSource(entry), connectionManager.getCodec(), command, params, promise, 0);
}
return mainPromise;
}
Aggregations