0

I am facing an issue in my deployment where I am using a completable future to do asynchronous task in tomcat servlet (version 8.5.23). I am aware that my following question do not say much about the implementation details but unfortunately I could not come up with a simple example to recreate the issue. Please pardon me for that. I hope that some experts in the field can give me high level advice or suggestion which I could further look into it.

It’s a follow up of my previous question Do the task asynchronously but reply first.

What I would like to do was “whenever I receive a request from the client then I have to start a task which may take an hour. Therefore I have immediately reply to the user that ‘the task has started’ before the task finishes.

I initially used AsyncContext.start() to do this but what I found that AsyncContext won’t return the response to the client until it calls the onComplete(AsyncEvent event).

Therefore I replaced AsyncContext.start() with CompletableFuture.runAsync(Runnable). It initially appeared to me that it’s doing the correct job but later I found out that it’s giving me an issue.

In my task I was using com.caucho.hessian.client.HessianProxyFactory.create(Class api, String urlName) to create proxy class of backend interface but after using the completable future then a runtime crash occurs java.lang.RuntimeException: java.lang.IllegalArgumentException: interface MyInterface is not visible from class loader.

I will be grateful if, somebody can throw some light into the issue

nantitv
  • 3,539
  • 4
  • 38
  • 61

1 Answers1

1

I found out a solution for my issue. It seems to work now but I do not know whether it would it create a new issue. I basically ensured that the async task used the same class loader.

ClassLoader cl = Thread.currentThread().getContextClassLoader();
CompletableFuture.runAsync(() -> {
try {
       Thread.currentThread().setContextClassLoader(cl);
       .......
nantitv
  • 3,539
  • 4
  • 38
  • 61