A safe way to catch an error inside completable future with executor service and a correct way to manage executors

user2963757 Source

I'm having a class with a huge processing load and for that I'm using a blocking queue for each task type.

The queue is processed recursively, as long as something is inside the queue, the next task is taken and sent away to be processed.

As a code sample, it is something like (note I use Scala)

def executeNextTask(taskTypeId: String, task: SomeObjectType) = {
  if(aTaskIsNotInProceesing(taskTypeId)){
    CompletableFuture.supplyAsync(task, executorService)
      .thenAccept(executeNextTask(taskTypeId))
  }
}

The trouble is that inside supplyAsync some errors may occur and, due to executor service, they are swallowed and I can't catch them. Sometimes, the application is unresponsive and my wild guess is that due to the executors that hang around.

Worth to mention that everything is happening inside a Java EE ecosystem.

My question to you is how can I safely catch an error and how can I protect my code to be consistent?

javascalaerror-handlingexecutorservicecompletable-future

Answers

comments powered by Disqus