eclipse / microprofile-context-propagation

Apache License 2.0
33 stars 23 forks source link

ScheduledExecutorService with context propagation #190

Closed nimo23 closed 4 years ago

nimo23 commented 4 years ago

Is there a way to have ScheduledExecutorService within "Microprofile Context Propagation"?

For example, something like: https://github.com/reactor/reactive-streams-commons/blob/master/src/main/java/rsc/scheduler/ExecutorTimedScheduler.java which wraps the ScheduledExecutorService within a wrapper to be usable for context propagation. Or is this available in "Microprofile Reactive Streams"?

manovotn commented 4 years ago

Not sure if I understood correctly, but I think you could use ThreadContext to create a task that you then pass to whichever executor you want. E.g. use the ThreadContext to wrap your tasks like this:

ExecutorTimedScheduler scheduler; //obtained earlier
ThreadContext tc; // obtained earlier
Runnable task = tc.contextualRunnable(myRunnable);
scheduler.schedule(task);
nimo23 commented 4 years ago

Is there a way to use ManagedExecutor to track a ScheduledExecutorService?

For example:

Then I put task1 and task2 to a list called tasks and each of them is scheduled:

final ScheduledExecutorService ses = Executors.newScheduledThreadPool(20);
List<Runnable> tasks = mytasks();
List<ScheduledFuture<?>> futures = new ArrayList<>();
tasks.forEach(t ->
{
ScheduledFuture<?> future = ses.scheduleWithFixedDelay(t, 0, 4 TimeUnit.SECONDS);
futures.add(future);
});

I have another method scheduleCycleFinished() which should be called after one scheduling is finished. But it should not stop the next scheduling. How can I track this?

How can "MP context propagation" help in this use case?

manovotn commented 4 years ago

No, I don't think you can pass arbitrary executor as a basis for ManagedExecutor.

But what I tried to explain in the previous comment can still be used here. Just make sure that all the Runnable in your tasks list are created via ThreadContext and the propagation should happen once that runnable is invoked. Otherwise, you can treat the scheduler as you did until now.

nimo23 commented 4 years ago

Ok, thanks.