2

I'm stuck when using Java's CompletableFuture with JMS (ActiveMQ). When I send a request for which I expect a reply, I create a CompletableFuture and put that in a Map with the correlation ID as key.

This works perfectly as long as I get a reply. When I don't get a reply, zombie start to pile up in the map.

I tried to attach cleanup code to CompletableFuture using whenComplete() but the code isn't called at all when CompletableFuture.get(long,TimeUnit) times out (the Java code in timedGet() will just throw the exception without calling anything else).

This looks like such an obvious place to use the Future API. Does someone have a working example or an idea how to clean up dead entries in the map?

Note: It can take up to three hours until the response arrives.
Note 2: Java 8

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
  • 1
    Sorry for the first edit, I thought you were asking for an answer within 3h! I changed the last sentence to avoid this confusion. As to the question itself, are you on java-8, or a more recent version? Because you might make use of [orTimeout()](https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CompletableFuture.html#orTimeout-long-java.util.concurrent.TimeUnit-) introduced in Java 9. Otherwise I think you should implement a similar feature yourself. – Didier L Jul 11 '18 at 12:49
  • @DidierL Thanks for clearing that up. I'm looking at orTimeout() now. – Aaron Digulla Jul 12 '18 at 07:44

0 Answers0