OpenNTF / org.openntf.xsp.jakartaee

XPages Jakarta EE support libraries
Apache License 2.0
20 stars 7 forks source link

Observed "NotesContext not initialized for the thread" with async client requests #399

Open jesse-gallagher opened 1 year ago

jesse-gallagher commented 1 year ago

This problem presumably comes from using CompletionStage as the return value - that's legal for MP Client, but RestEasy isn't using the ManagedExecutorService and thus doesn't init/term threads properly.

@RegisterRestClient
public interface WebhookGenericService {
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    CompletionStage<String> post(Map<String, Object> jsonMap);
}

Stack trace:

java.lang.IllegalStateException: NotesContext not initialized for the thread
    com.ibm.domino.xsp.module.nsf.NotesContext.getCurrent(NotesContext.java:124)
    com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.loadClass(ModuleClassLoader.java:432)
    java.lang.ClassLoader.loadClass(ClassLoader.java:885)
    webhooks.CustomClientHeadersFactory.update(CustomClientHeadersFactory.java:13)
    org.jboss.resteasy.microprofile.client.header.ClientHeadersRequestFilter.lambda$filter$4(ClientHeadersRequestFilter.java:81)
    java.util.Optional.ifPresent(Optional.java:159)
    org.jboss.resteasy.microprofile.client.header.ClientHeadersRequestFilter.filter(ClientHeadersRequestFilter.java:81)
    org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:767)
    org.jboss.resteasy.microprofile.client.impl.MpClientInvocation.filterRequest(MpClientInvocation.java:67)
    org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:491)
    org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.lambda$executorSubmit$11(ClientInvocation.java:856)
    java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
    org.jboss.resteasy.concurrent.ContextualExecutors.lambda$runnable$2(ContextualExecutors.java:316)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    java.lang.Thread.run(Thread.java:827)
jesse-gallagher commented 1 year ago

As with a lot of async things, this is inconsistent, and so it may be difficult to reproduce locally or write a test for.

jesse-gallagher commented 1 year ago

It's possible (but unconfirmed) that this is fixed by way of https://github.com/OpenNTF/org.openntf.xsp.jakartaee/issues/405 . Since the problem here is possibly that RESTeasy wasn't finding the ManagedExecutorService via JNDI, the new way of perma-registering a proxy may fix that.