本文整理匯總了Java中io.netty.util.concurrent.Promise.addListener方法的典型用法代碼示例。如果您正苦於以下問題:Java Promise.addListener方法的具體用法?Java Promise.addListener怎麽用?Java Promise.addListener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.util.concurrent.Promise
的用法示例。
在下文中一共展示了Promise.addListener方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: retryReadRandomAsync
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
private <R, T> void retryReadRandomAsync(final RedisCommand<T> command, final Promise<R> mainPromise,
final List<Integer> slots, final Object... params) {
final Promise<R> attemptPromise = connectionManager.newPromise();
attemptPromise.addListener(new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
if (future.isSuccess()) {
if (future.getNow() == null) {
if (slots.isEmpty()) {
mainPromise.setSuccess(null);
} else {
retryReadRandomAsync(command, mainPromise, slots, params);
}
} else {
mainPromise.setSuccess(future.getNow());
}
} else {
mainPromise.setFailure(future.cause());
}
}
});
Integer slot = slots.remove(0);
async(true, slot, null, connectionManager.getCodec(), command, params, attemptPromise, 0);
}
示例2: sendRaw
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
/**
* Sends a command. If there is currently a command in progress, this command will be queued and executed when the currently running command finishes.
* It is possible for a command to be queued and then a connection closed before it is actually executed, so it is important to listen to the returned future in order to ensure that the command was completed.
*
* @param imapCommand command to send
* @param <T> Response type
* @return Response future. Will be completed when a tagged response is received for this command.
*/
public synchronized <T extends ImapResponse> CompletableFuture<T> sendRaw(ImapCommand imapCommand) {
final Promise<T> commandPromise = promiseExecutor.next().newPromise();
commandPromise.addListener((f) -> {
writeNext();
});
send(imapCommand, commandPromise);
return NettyCompletableFuture.from(commandPromise);
}
示例3: channelRead0
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
@Override
public void channelRead0(ChannelHandlerContext ctx, Socks4Message socksRequest) {
Socks4CommandRequest command = (Socks4CommandRequest) socksRequest;
if (command.type() != Socks4CommandType.CONNECT) {
NettyUtils.closeOnFlush(ctx.channel());
logger.error("unsupported socks4 command: {}", command.type());
return;
}
Promise<Channel> promise = ctx.executor().newPromise();
Bootstrap bootstrap = initBootStrap(promise, ctx.channel().eventLoop());
bootstrap.connect(command.dstAddr(), command.dstPort()).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
ctx.channel().writeAndFlush(new DefaultSocks4CommandResponse(REJECTED_OR_FAILED));
NettyUtils.closeOnFlush(ctx.channel());
}
});
promise.addListener((FutureListener<Channel>) future -> {
Channel outboundChannel = future.getNow();
if (!future.isSuccess()) {
ctx.channel().writeAndFlush(new DefaultSocks4CommandResponse(REJECTED_OR_FAILED));
NettyUtils.closeOnFlush(ctx.channel());
return;
}
ChannelFuture responseFuture = ctx.channel().writeAndFlush(new DefaultSocks4CommandResponse(SUCCESS));
responseFuture.addListener((ChannelFutureListener) channelFuture -> {
ctx.pipeline().remove(Socks4ProxyHandler.this);
ctx.pipeline().remove(Socks4ServerEncoder.class);
ctx.pipeline().remove(Socks4ServerDecoder.class);
NetAddress address = new NetAddress(command.dstAddr(), command.dstPort());
initTcpProxyHandlers(ctx, address, outboundChannel);
});
});
}
示例4: channelRead0
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpRequest request) {
Promise<Channel> promise = ctx.executor().newPromise();
Bootstrap bootstrap = initBootStrap(promise, ctx.channel().eventLoop());
NetAddress address = Networks.parseAddress(request.uri());
bootstrap.connect(address.getHost(), address.getPort()).addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, BAD_GATEWAY));
NettyUtils.closeOnFlush(ctx.channel());
}
});
promise.addListener((FutureListener<Channel>) future -> {
if (!future.isSuccess()) {
ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, BAD_GATEWAY));
NettyUtils.closeOnFlush(ctx.channel());
return;
}
Channel outboundChannel = future.getNow();
ChannelFuture responseFuture = ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, OK));
responseFuture.addListener((ChannelFutureListener) channelFuture -> {
ctx.pipeline().remove(HttpTunnelProxyHandler.this);
ctx.pipeline().remove(HttpServerCodec.class);
initTcpProxyHandlers(ctx, address, outboundChannel);
});
});
}
示例5: makeContextAwareFutureListener
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
@Test
@SuppressWarnings("deprecation")
public void makeContextAwareFutureListener() {
RequestContext context = createContext();
Promise<String> promise = new DefaultPromise<>(ImmediateEventExecutor.INSTANCE);
promise.addListener(context.makeContextAware((FutureListener<String>) f -> {
assertCurrentContext(context);
assertDepth(1);
assertThat(f.getNow()).isEqualTo("success");
}));
promise.setSuccess("success");
}
示例6: execTxnCmd
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
private <T> Future<T> execTxnCmd(PromiseConverter<T> converter, RedisCommand cmd) {
Promise<Object> rawPromise = eventLoop().newPromise();
channel.writeAndFlush(new TxnRedisRequest(rawPromise, cmd));
Promise<T> promise = converter.newPromise();
rawPromise.addListener(converter.newListener(promise));
return promise;
}
示例7: sendLogout
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
private Future sendLogout() {
Promise<TaggedResponse> logoutPromise = promiseExecutor.next().newPromise();
actuallySend(new BaseImapCommand(ImapCommandType.LOGOUT), logoutPromise);
return logoutPromise.addListener(future1 -> closeNow());
}
示例8: channelRead0
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
@Override
public void channelRead0(ChannelHandlerContext ctx, Socks5Message socksRequest) {
if (socksRequest instanceof Socks5InitialRequest) {
ctx.pipeline().addFirst("socks5-command-decoder", new Socks5CommandRequestDecoder());
ctx.pipeline().remove("socks5-initial-decoder");
ctx.write(new DefaultSocks5InitialResponse(Socks5AuthMethod.NO_AUTH));
return;
}
if (socksRequest instanceof Socks5PasswordAuthRequest) {
ctx.pipeline().addFirst("socks5-command-decoder", new Socks5CommandRequestDecoder());
ctx.write(new DefaultSocks5PasswordAuthResponse(Socks5PasswordAuthStatus.SUCCESS));
return;
}
if (!(socksRequest instanceof Socks5CommandRequest)) {
logger.error("unknown socks5 command: {}", socksRequest.getClass().getName());
NettyUtils.closeOnFlush(ctx.channel());
}
Socks5CommandRequest command = (Socks5CommandRequest) socksRequest;
if (command.type() != Socks5CommandType.CONNECT) {
// only support connect command
logger.error("unsupported socks5 command: {}", command.type());
NettyUtils.closeOnFlush(ctx.channel());
return;
}
Promise<Channel> promise = ctx.executor().newPromise();
Bootstrap bootstrap = initBootStrap(promise, ctx.channel().eventLoop());
bootstrap.connect(command.dstAddr(), command.dstPort()).addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(FAILURE, command.dstAddrType()));
NettyUtils.closeOnFlush(ctx.channel());
}
});
promise.addListener((FutureListener<Channel>) future -> {
if (!future.isSuccess()) {
ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(FAILURE, command.dstAddrType()));
NettyUtils.closeOnFlush(ctx.channel());
return;
}
Channel outboundChannel = future.getNow();
ChannelFuture responseFuture = ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(
Socks5CommandStatus.SUCCESS,
command.dstAddrType(),
command.dstAddr(),
command.dstPort()));
responseFuture.addListener((ChannelFutureListener) f -> {
ctx.pipeline().remove("socks5-server-encoder");
ctx.pipeline().remove("socks5-command-decoder");
ctx.pipeline().remove(Socks5ProxyHandler.this);
NetAddress address = new NetAddress(command.dstAddr(), command.dstPort());
initTcpProxyHandlers(ctx, address, outboundChannel);
});
});
}
示例9: contextAwareEventExecutor
import io.netty.util.concurrent.Promise; //導入方法依賴的package包/類
@Test
public void contextAwareEventExecutor() throws Exception {
when(channel.eventLoop()).thenReturn(eventLoop);
RequestContext context = createContext();
Set<Integer> callbacksCalled = Collections.newSetFromMap(new ConcurrentHashMap<>());
EventExecutor executor = context.contextAwareEventLoop();
CountDownLatch latch = new CountDownLatch(18);
executor.execute(() -> checkCallback(1, context, callbacksCalled, latch));
executor.schedule(() -> checkCallback(2, context, callbacksCalled, latch), 0, TimeUnit.SECONDS);
executor.schedule(() -> {
checkCallback(2, context, callbacksCalled, latch);
return "success";
}, 0, TimeUnit.SECONDS);
executor.scheduleAtFixedRate(() -> checkCallback(3, context, callbacksCalled, latch), 0, 1000,
TimeUnit.SECONDS);
executor.scheduleWithFixedDelay(() -> checkCallback(4, context, callbacksCalled, latch), 0, 1000,
TimeUnit.SECONDS);
executor.submit(() -> checkCallback(5, context, callbacksCalled, latch));
executor.submit(() -> checkCallback(6, context, callbacksCalled, latch), "success");
executor.submit(() -> {
checkCallback(7, context, callbacksCalled, latch);
return "success";
});
executor.invokeAll(makeTaskList(8, 10, context, callbacksCalled, latch));
executor.invokeAll(makeTaskList(11, 12, context, callbacksCalled, latch), 10000, TimeUnit.SECONDS);
executor.invokeAny(makeTaskList(13, 13, context, callbacksCalled, latch));
executor.invokeAny(makeTaskList(14, 14, context, callbacksCalled, latch), 10000, TimeUnit.SECONDS);
Promise<String> promise = executor.newPromise();
promise.addListener(f -> checkCallback(15, context, callbacksCalled, latch));
promise.setSuccess("success");
executor.newSucceededFuture("success")
.addListener(f -> checkCallback(16, context, callbacksCalled, latch));
executor.newFailedFuture(new IllegalArgumentException())
.addListener(f -> checkCallback(17, context, callbacksCalled, latch));
ProgressivePromise<String> progressivePromise = executor.newProgressivePromise();
progressivePromise.addListener(f -> checkCallback(18, context, callbacksCalled, latch));
progressivePromise.setSuccess("success");
latch.await();
eventLoop.shutdownGracefully().sync();
assertThat(callbacksCalled).containsExactlyElementsOf(IntStream.rangeClosed(1, 18).boxed()::iterator);
}