use of org.springframework.util.concurrent.SettableListenableFuture in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method doBatchedAsyncOperation.
/**
* Perform Pub/Sub operations (ack/nack/modifyAckDeadline) in per-subscription batches.
* <p>The returned {@link ListenableFuture} will complete when either all batches completes successfully or when at
* least one fails.</p>
* <p>
* In case of multiple batch failures, which exception will be in the final {@link ListenableFuture} is
* non-deterministic.
* </p>
* @param acknowledgeablePubsubMessages messages, could be from different subscriptions.
* @param asyncOperation specific Pub/Sub operation to perform.
* @return {@link ListenableFuture} indicating overall success or failure.
*/
private ListenableFuture<Void> doBatchedAsyncOperation(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages, BiFunction<String, List<String>, ApiFuture<Empty>> asyncOperation) {
Map<ProjectSubscriptionName, List<String>> groupedMessages = acknowledgeablePubsubMessages.stream().collect(Collectors.groupingBy(AcknowledgeablePubsubMessage::getProjectSubscriptionName, Collectors.mapping(AcknowledgeablePubsubMessage::getAckId, Collectors.toList())));
Assert.state(groupedMessages.keySet().stream().map(ProjectSubscriptionName::getProject).distinct().count() == 1, "The project id of all messages must match.");
SettableListenableFuture<Void> settableListenableFuture = new SettableListenableFuture<>();
int numExpectedFutures = groupedMessages.size();
AtomicInteger numCompletedFutures = new AtomicInteger();
groupedMessages.forEach((ProjectSubscriptionName psName, List<String> ackIds) -> {
ApiFuture<Empty> ackApiFuture = asyncOperation.apply(psName.toString(), ackIds);
ApiFutures.addCallback(ackApiFuture, new ApiFutureCallback<Empty>() {
@Override
public void onFailure(Throwable throwable) {
processResult(throwable);
}
@Override
public void onSuccess(Empty empty) {
processResult(null);
}
private void processResult(Throwable throwable) {
if (throwable != null) {
settableListenableFuture.setException(throwable);
} else if (numCompletedFutures.incrementAndGet() == numExpectedFutures) {
settableListenableFuture.set(null);
}
}
}, this.ackExecutor);
});
return settableListenableFuture;
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method pullAndAckAsync.
@Override
public ListenableFuture<List<PubsubMessage>> pullAndAckAsync(String subscription, Integer maxMessages, Boolean returnImmediately) {
PullRequest pullRequest = this.subscriberFactory.createPullRequest(subscription, maxMessages, returnImmediately);
final SettableListenableFuture<List<PubsubMessage>> settableFuture = new SettableListenableFuture<>();
this.pullAsync(pullRequest).addCallback(ackableMessages -> {
if (!ackableMessages.isEmpty()) {
ack(ackableMessages);
}
List<PubsubMessage> messages = ackableMessages.stream().map(AcknowledgeablePubsubMessage::getPubsubMessage).collect(Collectors.toList());
settableFuture.set(messages);
}, settableFuture::setException);
return settableFuture;
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-boot-admin by codecentric.
the class StatusUpdateApplicationListenerTest method test_newApplication.
@Test
public void test_newApplication() throws Exception {
StatusUpdater statusUpdater = mock(StatusUpdater.class);
ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
when(scheduler.submit(any(Runnable.class))).then(new Answer<Future<?>>() {
@Override
public Future<?> answer(InvocationOnMock invocation) throws Throwable {
invocation.getArgumentAt(0, Runnable.class).run();
SettableListenableFuture<?> future = new SettableListenableFuture<Void>();
future.set(null);
return future;
}
});
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(statusUpdater, scheduler);
Application application = Application.create("test").withHealthUrl("http://example.com").build();
listener.onClientApplicationRegistered(new ClientApplicationRegisteredEvent(application));
verify(statusUpdater).updateStatus(eq(application));
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.
the class ReactorNettyTcpClient method shutdown.
@Override
public ListenableFuture<Void> shutdown() {
if (this.stopping) {
SettableListenableFuture<Void> future = new SettableListenableFuture<>();
future.set(null);
return future;
}
this.stopping = true;
ChannelGroupFuture close = this.channelGroup.close();
Mono<Void> completion = FutureMono.from(close).doAfterTerminate((x, e) -> {
// TODO: https://github.com/reactor/reactor-netty/issues/24
shutdownGlobalResources();
this.loopResources.dispose();
this.poolResources.dispose();
// TODO: https://github.com/reactor/reactor-netty/issues/25
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// Scheduler after loop resources...
this.scheduler.dispose();
});
return new MonoToListenableFutureAdapter<>(completion);
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.
the class Netty4ClientHttpRequest method executeInternal.
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture<>();
ChannelFutureListener connectionListener = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
Channel channel = future.channel();
channel.pipeline().addLast(new RequestExecuteHandler(responseFuture));
FullHttpRequest nettyRequest = createFullHttpRequest(headers);
channel.writeAndFlush(nettyRequest);
} else {
responseFuture.setException(future.cause());
}
}
};
this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);
return responseFuture;
}
Aggregations