0

I have one spring bean which contains two methods. One method is asynchronous with @Async annotation and second is not. Both methods are accessing SOAP endpoint client, generated with jaxws-maven-plugin. Method not annotated with @Async works fine. But method annotated with @Async sometimes fails with exception "java.lang.ClassNotFoundException: com.sun.xml.ws.spi.ProviderImpl". But this exception is really sporadic, not thrown always. Example of code:

@Service
public class MyService {

  @Transactional
  public void createTask(URL url) {
    new TaskService(url).getTaskServicePort().createTask();
  }


  @Async
  @Transactional
  public void getTask(URL url) {
    new TaskService(url).getTaskServicePort().getTask();
  }
}

Is it problem of asynchronous method call? Why I sometimes get this ClassNotFoundException: com.sun.xml.ws.spi.ProviderImpl ?

I would expect both methods works without throwing exception.

Update: I forgot to mention.

Java version is 17, Spring Boot version 2.7.4

Pido
  • 21
  • 5
  • ClassNotFoundException does not sound right for this case, can you please https://stackoverflow.com/help/minimal-reproducible-example ? – ozkanpakdil Feb 26 '23 at 19:47

1 Answers1

0

So it seams that I solved it. Even I do not understand completely the problem. I found some hints here CompletableFuture / ForkJoinPool Set Class Loader

I made changes in my spring configuration according this post. Here are my changes:

  @Bean(name = DAMAGE_CALCULATION_THREAD_POOL_TASK_EXECUTOR)
  public Executor threadPoolTaskExecutor() {
     ForkJoinPool.ForkJoinWorkerThreadFactory forkJoinWorkerThreadFactory = ForkJoinThread::new;
     ForkJoinPool forkJoinPool =
     new ForkJoinPool(Math.min(32767, Runtime.getRuntime().availableProcessors()), forkJoinWorkerThreadFactory, null, false);
     return new DelegatingSecurityContextExecutor(forkJoinPool, SecurityContextHolder.getContext());
  }
  private static class ForkJoinThread extends ForkJoinWorkerThread {

    protected ForkJoinThread(ForkJoinPool pool) {
      super(pool);
      this.setContextClassLoader(
        Thread.currentThread().getContextClassLoader());
    }
  }

Currently it is already more than two weeks and this error do not appear. So I think the problem was in classloader. But do not know what exactly was the problem.

Pido
  • 21
  • 5