use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project redisson by redisson.
the class RedisConnection method async.
public <T, R> RFuture<R> async(long timeout, Codec encoder, RedisCommand<T> command, Object... params) {
CompletableFuture<R> promise = new CompletableFuture<>();
if (timeout == -1) {
timeout = redisClient.getCommandTimeout();
}
if (redisClient.getEventLoopGroup().isShuttingDown()) {
RedissonShutdownException cause = new RedissonShutdownException("Redisson is shutdown");
return RedissonPromise.newFailedFuture(cause);
}
Timeout scheduledFuture = redisClient.getTimer().newTimeout(t -> {
RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for command: " + LogHelper.toString(command, params) + ", Redis client: " + redisClient);
promise.completeExceptionally(ex);
}, timeout, TimeUnit.MILLISECONDS);
promise.whenComplete((res, e) -> {
scheduledFuture.cancel();
});
ChannelFuture writeFuture = send(new CommandData<>(promise, encoder, command, params));
writeFuture.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
promise.completeExceptionally(future.cause());
}
});
return new CompletableFutureWrapper<>(promise);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project redisson by redisson.
the class RedisCommonBatchExecutor method sendCommand.
private void sendCommand(RedisConnection connection, CompletableFuture<Void> attemptPromise, List<CommandData<?, ?>> list) {
boolean isAtomic = options.getExecutionMode() != ExecutionMode.IN_MEMORY;
boolean isQueued = options.getExecutionMode() == ExecutionMode.REDIS_READ_ATOMIC || options.getExecutionMode() == ExecutionMode.REDIS_WRITE_ATOMIC;
CommandData<?, ?> lastCommand = connection.getLastCommand();
if (lastCommand != null && options.isSkipResult()) {
writeFuture = connection.getChannel().newPromise();
lastCommand.getPromise().whenComplete((r, e) -> {
CommandData<?, ?> currentLastCommand = connection.getLastCommand();
if (lastCommand != currentLastCommand && currentLastCommand != null) {
sendCommand(connection, attemptPromise, list);
return;
}
ChannelFuture wf = connection.send(new CommandsData(attemptPromise, list, options.isSkipResult(), isAtomic, isQueued, options.getSyncSlaves() > 0));
wf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
((ChannelPromise) writeFuture).trySuccess(future.getNow());
} else {
((ChannelPromise) writeFuture).tryFailure(future.cause());
}
});
});
return;
}
writeFuture = connection.send(new CommandsData(attemptPromise, list, options.isSkipResult(), isAtomic, isQueued, options.getSyncSlaves() > 0));
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project redisson by redisson.
the class PublishSubscribeService method unsubscribe.
public CompletableFuture<Void> unsubscribe(PubSubType topicType, ChannelName channelName) {
PubSubConnectionEntry entry = name2PubSubConnection.remove(createKey(channelName));
if (entry == null || connectionManager.isShuttingDown()) {
return CompletableFuture.completedFuture(null);
}
AtomicBoolean executed = new AtomicBoolean();
CompletableFuture<Void> result = new CompletableFuture<>();
BaseRedisPubSubListener listener = new BaseRedisPubSubListener() {
@Override
public boolean onStatus(PubSubType type, CharSequence channel) {
if (type == topicType && channel.equals(channelName)) {
executed.set(true);
if (entry.release() == 1) {
MasterSlaveEntry msEntry = getEntry(channelName);
msEntry.returnPubSubConnection(entry.getConnection());
}
result.complete(null);
return true;
}
return false;
}
};
ChannelFuture future;
if (topicType == PubSubType.UNSUBSCRIBE) {
future = entry.unsubscribe(channelName, listener);
} else {
future = entry.punsubscribe(channelName, listener);
}
future.addListener((ChannelFutureListener) f -> {
if (!f.isSuccess()) {
return;
}
connectionManager.newTimeout(timeout -> {
if (executed.get()) {
return;
}
entry.getConnection().onMessage(new PubSubStatusMessage(topicType, channelName));
}, config.getTimeout(), TimeUnit.MILLISECONDS);
});
return result;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project redisson by redisson.
the class PublishSubscribeService method subscribe.
private void subscribe(Codec codec, ChannelName channelName, MasterSlaveEntry entry, CompletableFuture<PubSubConnectionEntry> promise, PubSubType type, AsyncSemaphore lock, AtomicInteger attempts, RedisPubSubListener<?>... listeners) {
PubSubConnectionEntry connEntry = name2PubSubConnection.get(new PubSubKey(channelName, entry));
if (connEntry != null) {
addListeners(channelName, promise, type, lock, connEntry, listeners);
return;
}
Timeout lockTimeout = connectionManager.newTimeout(timeout -> {
promise.completeExceptionally(new RedisTimeoutException("Unable to acquire subscription lock after " + config.getTimeout() + "ms. " + "Increase 'subscriptionsPerConnection' and/or 'subscriptionConnectionPoolSize' parameters."));
}, config.getTimeout(), TimeUnit.MILLISECONDS);
freePubSubLock.acquire(() -> {
if (!lockTimeout.cancel() || promise.isDone()) {
lock.release();
freePubSubLock.release();
return;
}
MasterSlaveEntry msEntry = Optional.ofNullable(connectionManager.getEntry(entry.getClient())).orElse(entry);
PubSubEntry freePubSubConnections = entry2PubSubConnection.getOrDefault(msEntry, new PubSubEntry());
PubSubConnectionEntry freeEntry = freePubSubConnections.getEntries().peek();
if (freeEntry == null) {
freePubSubLock.release();
CompletableFuture<RedisPubSubConnection> connectFuture = connect(codec, channelName, msEntry, promise, type, lock, listeners);
connectionManager.newTimeout(t -> {
if (attempts.get() == config.getRetryAttempts()) {
connectFuture.completeExceptionally(new RedisTimeoutException("Unable to acquire connection for subscription after " + attempts.get() + " attempts. " + "Increase 'subscriptionsPerConnection' and/or 'subscriptionConnectionPoolSize' parameters."));
return;
}
if (connectFuture.cancel(true)) {
subscribe(codec, channelName, entry, promise, type, lock, attempts, listeners);
attempts.incrementAndGet();
}
}, config.getRetryInterval(), TimeUnit.MILLISECONDS);
return;
}
int remainFreeAmount = freeEntry.tryAcquire();
if (remainFreeAmount == -1) {
throw new IllegalStateException();
}
PubSubKey key = new PubSubKey(channelName, msEntry);
PubSubConnectionEntry oldEntry = name2PubSubConnection.putIfAbsent(key, freeEntry);
if (oldEntry != null) {
freeEntry.release();
freePubSubLock.release();
addListeners(channelName, promise, type, lock, oldEntry, listeners);
return;
}
if (remainFreeAmount == 0) {
freePubSubConnections.getEntries().poll();
}
freePubSubLock.release();
CompletableFuture<Void> subscribeFuture = addListeners(channelName, promise, type, lock, freeEntry, listeners);
ChannelFuture future;
if (PubSubType.PSUBSCRIBE == type) {
future = freeEntry.psubscribe(codec, channelName);
} else {
future = freeEntry.subscribe(codec, channelName);
}
future.addListener((ChannelFutureListener) future1 -> {
if (!future1.isSuccess()) {
if (!promise.isDone()) {
subscribeFuture.cancel(false);
}
return;
}
connectionManager.newTimeout(timeout -> {
if (subscribeFuture.completeExceptionally(new RedisTimeoutException("Subscription timeout after " + config.getTimeout() + "ms. " + "Check network and/or increase 'timeout' parameter."))) {
unsubscribe(channelName, type);
}
}, config.getTimeout(), TimeUnit.MILLISECONDS);
});
});
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project pinpoint by naver.
the class NettyIT method writeTest.
@Test
public void writeTest() throws Exception {
final CountDownLatch awaitLatch = new CountDownLatch(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(2);
Bootstrap bootstrap = client(workerGroup);
final ChannelFuture connect = bootstrap.connect(webServer.getHostname(), webServer.getListeningPort());
connect.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
Channel channel = future.channel();
channel.pipeline().addLast(new SimpleChannelInboundHandler() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
awaitLatch.countDown();
}
});
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
future.channel().writeAndFlush(request);
}
}
});
boolean await = awaitLatch.await(3000, TimeUnit.MILLISECONDS);
Assert.assertTrue(await);
final Channel channel = connect.channel();
try {
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
verifier.verifyTrace(event("NETTY", Bootstrap.class.getMethod("connect", SocketAddress.class), annotation("netty.address", webServer.getHostAndPort())));
verifier.verifyTrace(event("NETTY", "io.netty.channel.DefaultChannelPromise.addListener(io.netty.util.concurrent.GenericFutureListener)"));
verifier.verifyTrace(event("ASYNC", "Asynchronous Invocation"));
verifier.verifyTrace(event("NETTY_INTERNAL", "io.netty.util.concurrent.DefaultPromise.notifyListenersNow()"));
verifier.verifyTrace(event("NETTY_INTERNAL", "io.netty.util.concurrent.DefaultPromise.notifyListener0(io.netty.util.concurrent.Future, io.netty.util.concurrent.GenericFutureListener)"));
verifier.verifyTrace(event("NETTY", "io.netty.channel.DefaultChannelPipeline.writeAndFlush(java.lang.Object)"));
verifier.verifyTrace(event("NETTY_HTTP", "io.netty.handler.codec.http.HttpObjectEncoder.encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)", annotation("http.url", "/")));
} finally {
channel.close().sync();
workerGroup.shutdown();
}
}
Aggregations