Hitting a wall when Using CompletableFuture with EJb
Asked Answered
R

1

6

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)]
Response answered 21/11, 2019 at 13:4 Comment(1)
Post the full stacktrace, including the caused by stackTorpedo
S
8

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

Somato answered 30/11, 2019 at 15:43 Comment(2)
Thank @Laertes, I will try it and let you knowResponse
Worked here. WildFly 17. Thanks!Willams

© 2022 - 2024 — McMap. All rights reserved.