I'm trying to use CompletableFuture with EBJ bean running on WildFly.
I can see the debugger reach the remote Ejb and retrieve the result successfully but in the Wayback to caller class I got an exception
java.lang.ClassNotFoundException: No classloader available
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
dnsRecords = ipaFacadeService.fetchDnsRecords();
return dnsRecords;
}).thenAccept(result -> {
if (result.size() > 0) {
//do more stuff with the result
}
});
future.get();
When I stack trace the exception I found it is related to ForkJoin which is used by CompletableFuture.supplyAsync()
[org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:238),
org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183),
org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146),
com.sun.proxy.$Proxy107.fetchDnsRecords(Unknown Source), no.lyse.tele.prov.struts2.action.network.DnsAction.lambda$list$1(DnsAction.java:150),
java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590),
java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582),
java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289),
java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056),
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692),
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)]
I guess it has to do with the fact, that the default ForkJoinPool is used. Basically inside a JEE server creating unmanaged threads is not a good idea (and even forbidden by the specs). A possibility would be to use JEE's ManagedExecutorService and inject it like this:
@Resource
private ManagedExecutorService mes;
Then you can use it as second argument for the supplyAsync call:
final CompletableFuture<Void> future =
CompletableFuture.supplyAsync(() -> {
//...
}, mes).thenAccept//...
See also:
Which thread executes CompletableFuture's tasks and callbacks?
Java 8 CompletableFuture Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With