Have multiple managedExecutor

Viewed 27

I am trying to add a new ManagedExecutor but I always get an IllegalStateException:

java.lang.IllegalStateException: Cannot use the same context in more than one of propagated ([Remaining]), cleared ([Remaining]), unchanged ([])

I originally had the following code:

@Path("...")
@ApplicationScoped
public class ClassA {
    @Inject
    @ManagedExecutorConfig(maxAsync = 1, maxQueued = 1, cleared = ThreadContext.ALL_REMAINING)
    private ManagedExecutor managedExecutor;

    ...

    @GET
    @Path("pathA")
    @Produces(MediaType.APPLICATION_JSON)
    public void methodA() {
        CompletableFuture.runAsync(()-> {
            try {
                serviceA.start();
            } catch (IOException|InterruptedException e) {
                LOGGER.error("N/A", e.getMessage(), e);
            }
        }, managedExecutor);
        ;
    }

And I needed a executor for another class, so I tried adding one the same way, but it fails with the IllegalStateException:

@Path("...")
@ApplicationScoped
public class ClassB {
    @Inject
    @ManagedExecutorConfig(maxAsync = 1, maxQueued = 1, cleared = ThreadContext.ALL_REMAINING)
    private ManagedExecutor managedExecutor;

    ...

    @GET
    @Path("pathB")
    @Produces(MediaType.APPLICATION_JSON)
    public void methodB() {
        CompletableFuture.runAsync(()-> {
            try {
                serviceB.start();
            } catch (IOException|InterruptedException e) {
                LOGGER.error("N/A", e.getMessage(), e);
            }
        }, managedExecutor);
        ;
    }

I also tried naming them with:

    @Inject
    @ManagedExecutorConfig(maxAsync = 30, maxQueued = 300, cleared = ThreadContext.ALL_REMAINING)
    @NamedInstance("myExecutor")
    private ManagedExecutor managedExecutor;

But same exception. I tried to set the ThreadContext of classA as ThreadContext.APPLICATION and the ThreadContext of ClassB as ThreadContext.ALL_REMAINING, but same exception too.

How can I have the two managedExecutors?

0 Answers
Related