use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.
the class ConnectionPool method createConnection.
private CompletableFuture<ClientCnx> createConnection(InetSocketAddress address, int connectionKey) {
if (log.isDebugEnabled()) {
log.debug("Connection for {} not found in cache", address);
}
final CompletableFuture<ClientCnx> cnxFuture = new CompletableFuture<ClientCnx>();
// Trigger async connect to broker
bootstrap.connect(address).addListener((ChannelFuture future) -> {
if (!future.isSuccess()) {
cnxFuture.completeExceptionally(new PulsarClientException(future.cause()));
cleanupConnection(address, connectionKey, cnxFuture);
return;
}
log.info("[{}] Connected to server", future.channel());
future.channel().closeFuture().addListener(v -> {
if (log.isDebugEnabled()) {
log.debug("Removing closed connection from pool: {}", v);
}
cleanupConnection(address, connectionKey, cnxFuture);
});
// We are connected to broker, but need to wait until the connect/connected handshake is
// complete
final ClientCnx cnx = (ClientCnx) future.channel().pipeline().get("handler");
if (!future.channel().isActive() || cnx == null) {
if (log.isDebugEnabled()) {
log.debug("[{}] Connection was already closed by the time we got notified", future.channel());
}
cnxFuture.completeExceptionally(new ChannelException("Connection already closed"));
return;
}
cnx.connectionFuture().thenRun(() -> {
if (log.isDebugEnabled()) {
log.debug("[{}] Connection handshake completed", cnx.channel());
}
cnxFuture.complete(cnx);
}).exceptionally(exception -> {
log.warn("[{}] Connection handshake failed: {}", cnx.channel(), exception.getMessage());
cnxFuture.completeExceptionally(exception);
cleanupConnection(address, connectionKey, cnxFuture);
cnx.ctx().close();
return null;
});
});
return cnxFuture;
}
use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.
the class PartitionedConsumerImpl method internalReceiveAsync.
@Override
protected CompletableFuture<Message> internalReceiveAsync() {
CompletableFuture<Message> result = new CompletableFuture<Message>();
Message message;
try {
lock.writeLock().lock();
message = incomingMessages.poll(0, TimeUnit.SECONDS);
if (message == null) {
pendingReceives.add(result);
} else {
resumeReceivingFromPausedConsumersIfNeeded();
result.complete(message);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
result.completeExceptionally(new PulsarClientException(e));
} finally {
lock.writeLock().unlock();
}
return result;
}
use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.
the class PartitionedConsumerImpl method unsubscribeAsync.
@Override
public CompletableFuture<Void> unsubscribeAsync() {
if (getState() == State.Closing || getState() == State.Closed) {
return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Partitioned Consumer was already closed"));
}
setState(State.Closing);
AtomicReference<Throwable> unsubscribeFail = new AtomicReference<Throwable>();
AtomicInteger completed = new AtomicInteger(numPartitions);
CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
for (Consumer consumer : consumers) {
if (consumer != null) {
consumer.unsubscribeAsync().handle((unsubscribed, ex) -> {
if (ex != null) {
unsubscribeFail.compareAndSet(null, ex);
}
if (completed.decrementAndGet() == 0) {
if (unsubscribeFail.get() == null) {
setState(State.Closed);
unsubscribeFuture.complete(null);
log.info("[{}] [{}] Unsubscribed Partitioned Consumer", topic, subscription);
} else {
setState(State.Failed);
unsubscribeFuture.completeExceptionally(unsubscribeFail.get());
log.error("[{}] [{}] Could not unsubscribe Partitioned Consumer", topic, subscription, unsubscribeFail.get().getCause());
}
}
return null;
});
}
}
return unsubscribeFuture;
}
use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.
the class PartitionedProducerImpl method closeAsync.
@Override
public CompletableFuture<Void> closeAsync() {
if (getState() == State.Closing || getState() == State.Closed) {
return CompletableFuture.completedFuture(null);
}
setState(State.Closing);
AtomicReference<Throwable> closeFail = new AtomicReference<Throwable>();
AtomicInteger completed = new AtomicInteger(numPartitions);
CompletableFuture<Void> closeFuture = new CompletableFuture<>();
for (Producer producer : producers) {
if (producer != null) {
producer.closeAsync().handle((closed, ex) -> {
if (ex != null) {
closeFail.compareAndSet(null, ex);
}
if (completed.decrementAndGet() == 0) {
if (closeFail.get() == null) {
setState(State.Closed);
closeFuture.complete(null);
log.info("[{}] Closed Partitioned Producer", topic);
client.cleanupProducer(this);
} else {
setState(State.Failed);
closeFuture.completeExceptionally(closeFail.get());
log.error("[{}] Could not close Partitioned Producer", topic, closeFail.get().getCause());
}
}
return null;
});
}
}
return closeFuture;
}
use of java.util.concurrent.CompletableFuture in project cassandra by apache.
the class ValidatorTest method registerOutgoingMessageSink.
private CompletableFuture<MessageOut> registerOutgoingMessageSink() {
final CompletableFuture<MessageOut> future = new CompletableFuture<>();
MessagingService.instance().addMessageSink(new IMessageSink() {
public boolean allowOutgoingMessage(MessageOut message, int id, InetAddress to) {
future.complete(message);
return false;
}
public boolean allowIncomingMessage(MessageIn message, int id) {
return false;
}
});
return future;
}
Aggregations