use of org.springframework.util.concurrent.ListenableFutureCallback in project spring-integration-samples by spring-projects.
the class ListenableFutureTest method testAsyncGateway.
@Test
public void testAsyncGateway() throws Exception {
Random random = new Random();
int[] numbers = new int[100];
int expectedResults = 0;
for (int i = 0; i < 100; i++) {
numbers[i] = random.nextInt(200);
if (numbers[i] > 100) {
expectedResults++;
}
}
final CountDownLatch latch = new CountDownLatch(expectedResults);
final AtomicInteger failures = new AtomicInteger();
for (int i = 0; i < 100; i++) {
final int number = numbers[i];
ListenableFuture<Integer> result = gateway.multiplyByTwo(number);
ListenableFutureCallback<Integer> callback = new ListenableFutureCallback<Integer>() {
@Override
public void onSuccess(Integer result) {
logger.info("Result of multiplication of " + number + " by 2 is " + result);
latch.countDown();
}
@Override
public void onFailure(Throwable t) {
failures.incrementAndGet();
logger.error("Unexpected exception for " + number, t);
latch.countDown();
}
};
result.addCallback(callback);
}
assertTrue(latch.await(60, TimeUnit.SECONDS));
assertEquals(0, failures.get());
logger.info("Finished");
}
use of org.springframework.util.concurrent.ListenableFutureCallback in project dhis2-core by dhis2.
the class FileResourceUploadCallback method newInstance.
public ListenableFutureCallback<String> newInstance(String fileResourceUid) {
return new ListenableFutureCallback<String>() {
DateTime startTime = DateTime.now();
@Override
public void onFailure(Throwable ex) {
log.error("Saving content for file resource '" + fileResourceUid + "' failed", ex);
FileResource fileResource = idObjectManager.get(FileResource.class, fileResourceUid);
if (fileResource != null) {
log.info("File resource '" + fileResource.getUid() + "' storageStatus set to FAILED.");
fileResource.setStorageStatus(FileResourceStorageStatus.FAILED);
idObjectManager.update(fileResource);
}
}
@Override
public void onSuccess(String result) {
Period timeDiff = new Period(startTime, DateTime.now());
log.info("File stored with key: '" + result + "'. Upload finished in " + timeDiff.toString(PeriodFormat.getDefault()));
FileResource fileResource = idObjectManager.get(FileResource.class, fileResourceUid);
if (result != null && fileResource != null) {
fileResource.setStorageStatus(FileResourceStorageStatus.STORED);
idObjectManager.update(fileResource);
} else {
log.error("Conflict: content was stored but FileResource with uid '" + fileResourceUid + "' could not be found.");
}
}
};
}
use of org.springframework.util.concurrent.ListenableFutureCallback in project spring-integration by spring-projects.
the class AbstractMessageProducingHandler method produceOutput.
protected void produceOutput(Object reply, final Message<?> requestMessage) {
final MessageHeaders requestHeaders = requestMessage.getHeaders();
Object replyChannel = null;
if (getOutputChannel() == null) {
Map<?, ?> routingSlipHeader = requestHeaders.get(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Map.class);
if (routingSlipHeader != null) {
Assert.isTrue(routingSlipHeader.size() == 1, "The RoutingSlip header value must be a SingletonMap");
Object key = routingSlipHeader.keySet().iterator().next();
Object value = routingSlipHeader.values().iterator().next();
Assert.isInstanceOf(List.class, key, "The RoutingSlip key must be List");
Assert.isInstanceOf(Integer.class, value, "The RoutingSlip value must be Integer");
List<?> routingSlip = (List<?>) key;
AtomicInteger routingSlipIndex = new AtomicInteger((Integer) value);
replyChannel = getOutputChannelFromRoutingSlip(reply, requestMessage, routingSlip, routingSlipIndex);
if (replyChannel != null) {
// TODO Migrate to the SF MessageBuilder
AbstractIntegrationMessageBuilder<?> builder = null;
if (reply instanceof Message) {
builder = this.getMessageBuilderFactory().fromMessage((Message<?>) reply);
} else if (reply instanceof AbstractIntegrationMessageBuilder) {
builder = (AbstractIntegrationMessageBuilder<?>) reply;
} else {
builder = this.getMessageBuilderFactory().withPayload(reply);
}
builder.setHeader(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Collections.singletonMap(routingSlip, routingSlipIndex.get()));
reply = builder;
}
}
if (replyChannel == null) {
replyChannel = requestHeaders.getReplyChannel();
if (replyChannel == null && reply instanceof Message) {
replyChannel = ((Message<?>) reply).getHeaders().getReplyChannel();
}
}
}
if (this.async && (reply instanceof ListenableFuture<?> || reply instanceof Publisher<?>)) {
if (reply instanceof ListenableFuture<?> || !(getOutputChannel() instanceof ReactiveStreamsSubscribableChannel)) {
ListenableFuture<?> future;
if (reply instanceof ListenableFuture<?>) {
future = (ListenableFuture<?>) reply;
} else {
SettableListenableFuture<Object> settableListenableFuture = new SettableListenableFuture<>();
Mono.from((Publisher<?>) reply).subscribe(settableListenableFuture::set, settableListenableFuture::setException);
future = settableListenableFuture;
}
Object theReplyChannel = replyChannel;
future.addCallback(new ListenableFutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
Message<?> replyMessage = null;
try {
replyMessage = createOutputMessage(result, requestHeaders);
sendOutput(replyMessage, theReplyChannel, false);
} catch (Exception e) {
Exception exceptionToLogAndSend = e;
if (!(e instanceof MessagingException)) {
exceptionToLogAndSend = new MessageHandlingException(requestMessage, e);
if (replyMessage != null) {
exceptionToLogAndSend = new MessagingException(replyMessage, exceptionToLogAndSend);
}
}
logger.error("Failed to send async reply: " + result.toString(), exceptionToLogAndSend);
onFailure(exceptionToLogAndSend);
}
}
@Override
public void onFailure(Throwable ex) {
sendErrorMessage(requestMessage, ex);
}
});
} else {
((ReactiveStreamsSubscribableChannel) getOutputChannel()).subscribeTo(Flux.from((Publisher<?>) reply).map(result -> createOutputMessage(result, requestHeaders)));
}
} else {
sendOutput(createOutputMessage(reply, requestHeaders), replyChannel, false);
}
}
Aggregations