List of usage examples for io.netty.util.concurrent Promise setSuccess
Promise<V> setSuccess(V result);
From source file:io.vertx.core.dns.impl.fix.DnsNameResolver.java
License:Apache License
/** * Hook designed for extensibility so one can pass a different cache on each resolution attempt * instead of using the global one.//from ww w. j ava 2s . co m */ protected void doResolveAll(String inetHost, Promise<List<InetAddress>> promise, DnsCache resolveCache) throws Exception { final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(inetHost); if (bytes != null) { // The unresolvedAddress was created via a String that contains an ipaddress. promise.setSuccess(Collections.singletonList(InetAddress.getByAddress(bytes))); return; } final String hostname = hostname(inetHost); InetAddress hostsFileEntry = resolveHostsFileEntry(hostname); if (hostsFileEntry != null) { promise.setSuccess(Collections.singletonList(hostsFileEntry)); return; } if (!doResolveAllCached(hostname, promise, resolveCache)) { doResolveAllUncached(hostname, promise, resolveCache); } }
From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java
License:Apache License
private void setSuccess(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) { parent.queryContextManager.remove(nameServerAddr(), id); // Cancel the timeout task. final ScheduledFuture<?> timeoutFuture = this.timeoutFuture; if (timeoutFuture != null) { timeoutFuture.cancel(false);//from w w w.ja va2 s . c om } Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise = this.promise; if (promise.setUncancellable()) { @SuppressWarnings("unchecked") AddressedEnvelope<DnsResponse, InetSocketAddress> castResponse = (AddressedEnvelope<DnsResponse, InetSocketAddress>) envelope .retain(); promise.setSuccess(castResponse); } }
From source file:me.ferrybig.javacoding.teamspeakconnector.util.FutureUtil.java
License:Open Source License
private static <T, R> Future<R> delegateFutureResult(Future<T> future, Promise<R> prom, Function<T, R> map) { future.addListener(ignored -> {/*w w w . j a v a2 s . c om*/ assert ignored == future; try { if (future.isSuccess()) { prom.setSuccess(map.apply(future.getNow())); } else { prom.setFailure(future.cause()); } } catch (Throwable e) { prom.setFailure(e); } }); return prom; }
From source file:org.apache.hadoop.hdfs.server.datanode.web.dtp.Http2ResponseHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception { Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text()); if (streamId == null) { System.err.println("HttpResponseHandler unexpected message received: " + msg); return;/*w ww .j av a2s . c o m*/ } if (streamId.intValue() == 1) { // this is the upgrade response message, just ignore it. return; } Promise<FullHttpResponse> promise; synchronized (this) { promise = streamId2Promise.get(streamId); } if (promise == null) { System.err.println("Message received for unknown stream id " + streamId); } else { // Do stuff with the message (for now just print it) promise.setSuccess(msg.retain()); } }
From source file:org.asynchttpclient.resolver.JdkNameResolver.java
License:Open Source License
@Override public Future<List<InetSocketAddress>> resolve(String name, int port) { Promise<List<InetSocketAddress>> promise = ImmediateEventExecutor.INSTANCE.newPromise(); try {//from w w w.j a va 2s . c o m InetAddress[] resolved = InetAddress.getAllByName(name); List<InetSocketAddress> socketResolved = new ArrayList<InetSocketAddress>(resolved.length); for (InetAddress res : resolved) { socketResolved.add(new InetSocketAddress(res, port)); } return promise.setSuccess(socketResolved); } catch (UnknownHostException e) { return promise.setFailure(e); } }
From source file:org.asynchttpclient.resolver.RequestHostnameResolver.java
License:Open Source License
public Future<List<InetSocketAddress>> resolve(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) { Uri uri = request.getUri();//www . j a v a 2 s . c o m final Promise<List<InetSocketAddress>> promise = ImmediateEventExecutor.INSTANCE.newPromise(); if (request.getAddress() != null) { List<InetSocketAddress> resolved = Collections .singletonList(new InetSocketAddress(request.getAddress(), uri.getExplicitPort())); return promise.setSuccess(resolved); } // don't notify on explicit address final AsyncHandlerExtensions asyncHandlerExtensions = request.getAddress() == null ? toAsyncHandlerExtensions(asyncHandler) : null; final String name; final int port; if (proxy != null && !proxy.isIgnoredForHost(uri.getHost())) { name = proxy.getHost(); port = uri.isSecured() ? proxy.getSecuredPort() : proxy.getPort(); } else { name = uri.getHost(); port = uri.getExplicitPort(); } if (asyncHandlerExtensions != null) asyncHandlerExtensions.onHostnameResolutionAttempt(name); final Future<List<InetAddress>> whenResolved = request.getNameResolver().resolveAll(name); whenResolved.addListener(new SimpleFutureListener<List<InetAddress>>() { @Override protected void onSuccess(List<InetAddress> value) throws Exception { ArrayList<InetSocketAddress> socketAddresses = new ArrayList<>(value.size()); for (InetAddress a : value) { socketAddresses.add(new InetSocketAddress(a, port)); } if (asyncHandlerExtensions != null) { asyncHandlerExtensions.onHostnameResolutionSuccess(name, socketAddresses); } promise.trySuccess(socketAddresses); } @Override protected void onFailure(Throwable t) throws Exception { if (asyncHandlerExtensions != null) { asyncHandlerExtensions.onHostnameResolutionFailure(name, t); } promise.tryFailure(t); } }); return promise; }
From source file:org.asynchttpclient.resolver.RequestNameResolver.java
License:Open Source License
public Future<List<InetSocketAddress>> resolve(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) { Uri uri = request.getUri();//from w ww .ja v a2 s .co m if (request.getAddress() != null) { List<InetSocketAddress> resolved = Collections .singletonList(new InetSocketAddress(request.getAddress(), uri.getExplicitPort())); Promise<List<InetSocketAddress>> promise = ImmediateEventExecutor.INSTANCE.newPromise(); return promise.setSuccess(resolved); } // don't notify on explicit address final AsyncHandlerExtensions asyncHandlerExtensions = request.getAddress() == null ? toAsyncHandlerExtensions(asyncHandler) : null; final String name; final int port; if (proxy != null && !proxy.isIgnoredForHost(uri.getHost())) { name = proxy.getHost(); port = uri.isSecured() ? proxy.getSecuredPort() : proxy.getPort(); } else { name = uri.getHost(); port = uri.getExplicitPort(); } if (asyncHandlerExtensions != null) asyncHandlerExtensions.onDnsResolution(name); final Future<List<InetSocketAddress>> whenResolved = request.getNameResolver().resolve(name, port); if (asyncHandlerExtensions == null) return whenResolved; Promise<List<InetSocketAddress>> promise = ImmediateEventExecutor.INSTANCE.newPromise(); whenResolved.addListener(new SimpleGenericFutureListener<List<InetSocketAddress>>() { @Override protected void onSuccess(List<InetSocketAddress> addresses) throws Exception { asyncHandlerExtensions.onDnsResolutionSuccess(name, addresses); promise.setSuccess(addresses); } @Override protected void onFailure(Throwable t) throws Exception { asyncHandlerExtensions.onDnsResolutionFailure(name, t); promise.setFailure(t); } }); return promise; }
From source file:org.opendaylight.protocol.framework.ServerTest.java
License:Open Source License
private SimpleDispatcher getServerDispatcher(final Promise<Boolean> p) { return new SimpleDispatcher( new SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener>() { @Override// w ww.ja v a 2 s .co m public SessionNegotiator<SimpleSession> getSessionNegotiator( final SessionListenerFactory<SimpleSessionListener> factory, final Channel channel, final Promise<SimpleSession> promise) { p.setSuccess(true); return new SimpleSessionNegotiator(promise, channel); } }, null, serverLoopGroup); }
From source file:org.redisson.CommandBatchExecutorService.java
License:Apache License
public Future<List<?>> executeAsync() { if (executed) { throw new IllegalStateException("Batch already executed!"); }/*from w w w . ja v a 2s . c o m*/ if (commands.isEmpty()) { return connectionManager.getGroup().next().newSucceededFuture(null); } executed = true; Promise<Void> voidPromise = connectionManager.newPromise(); final Promise<List<?>> promise = connectionManager.newPromise(); voidPromise.addListener(new FutureListener<Void>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (!future.isSuccess()) { promise.setFailure(future.cause()); return; } List<CommandEntry> entries = new ArrayList<CommandEntry>(); for (Entry e : commands.values()) { entries.addAll(e.getCommands()); } Collections.sort(entries); List<Object> result = new ArrayList<Object>(); for (CommandEntry commandEntry : entries) { result.add(commandEntry.getCommand().getPromise().getNow()); } promise.setSuccess(result); commands = null; } }); for (java.util.Map.Entry<Integer, Entry> e : commands.entrySet()) { execute(e.getValue(), e.getKey(), voidPromise, new AtomicInteger(commands.size()), 0); } return promise; }
From source file:org.redisson.CommandBatchExecutorService.java
License:Apache License
public void execute(final Entry entry, final int slot, final Promise<Void> mainPromise, final AtomicInteger slots, final int attempt) { if (!connectionManager.getShutdownLatch().acquire()) { mainPromise.setFailure(new IllegalStateException("Redisson is shutdown")); return;// w w w . j a v a 2 s . com } final Promise<Void> attemptPromise = connectionManager.newPromise(); final AtomicReference<RedisException> ex = new AtomicReference<RedisException>(); final TimerTask retryTimerTask = new TimerTask() { @Override public void run(Timeout timeout) throws Exception { if (attemptPromise.isDone()) { return; } if (attempt == connectionManager.getConfig().getRetryAttempts()) { attemptPromise.setFailure(ex.get()); return; } attemptPromise.cancel(true); int count = attempt + 1; execute(entry, slot, mainPromise, slots, count); } }; try { org.redisson.client.RedisConnection connection; if (entry.isReadOnlyMode()) { connection = connectionManager.connectionReadOp(slot); } else { connection = connectionManager.connectionWriteOp(slot); } ArrayList<CommandData<?, ?>> list = new ArrayList<CommandData<?, ?>>(entry.getCommands().size()); for (CommandEntry c : entry.getCommands()) { list.add(c.getCommand()); } ChannelFuture future = connection.send(new CommandsData(attemptPromise, list)); ex.set(new RedisTimeoutException()); final Timeout timeout = connectionManager.getTimer().newTimeout(retryTimerTask, connectionManager.getConfig().getTimeout(), TimeUnit.MILLISECONDS); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { timeout.cancel(); ex.set(new WriteRedisConnectionException("channel: " + future.channel() + " closed")); connectionManager.getTimer().newTimeout(retryTimerTask, connectionManager.getConfig().getRetryInterval(), TimeUnit.MILLISECONDS); } } }); if (entry.isReadOnlyMode()) { attemptPromise.addListener(connectionManager.createReleaseReadListener(slot, connection, timeout)); } else { attemptPromise.addListener(connectionManager.createReleaseWriteListener(slot, connection, timeout)); } } catch (RedisException e) { ex.set(e); connectionManager.getTimer().newTimeout(retryTimerTask, connectionManager.getConfig().getRetryInterval(), TimeUnit.MILLISECONDS); } attemptPromise.addListener(new FutureListener<Void>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (future.isCancelled()) { return; } if (future.cause() instanceof RedisMovedException) { RedisMovedException ex = (RedisMovedException) future.cause(); execute(entry, ex.getSlot(), mainPromise, slots, attempt); return; } if (future.isSuccess()) { if (slots.decrementAndGet() == 0) { mainPromise.setSuccess(future.getNow()); } } else { mainPromise.setFailure(future.cause()); } } }); }