question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support basic Java Executor interface

See original GitHub issue

Currently you can provide either ExecutorService, ScheduledExecutorService or implement custom Failsafe Scheduler. It would be good if it would also accept java.util.concurrent.Executor interface since it is a common interface returned by other libs. My current use case is in gRPC, when splitting context for multi-threading, and gRPC methods #fixedContextExecutor return base Executor.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:23 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Tembrelcommented, Jan 13, 2020

Yes, I understand the semantic predicament, but you could have a wrapper similarly like you have for the ExecutorService and use the CompletableFuture.supplyAsync(Supplier, Executor) to return the value?

Seems reasonable to me. At the very least, couldn’t the use of ExecutorService.submit in lines like this be replaced by something like:

promise.delegate = es instanceof ExecutorService ?
    ((ExecutorService) es).submit(completingCallable) :
    supplyAsync(callableToSupplier(completingCallable), es);

when es is an Executor but not an ExecutableService? I realize that I’m glossing over an important detail with the following simple implementation of callableToSupplier, but the general idea seems feasible:


static <T> Supplier<T> callableToSupplier(Callable<? extends T> callable) {
    return () -> {
	try {
	    return callable.call();
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    };
}
0reactions
jhaltermancommented, Jan 29, 2022

@paulius-p So there are two options for approaching this problem:

  1. One way is to have the Executor call the user-provided Supplier/Runnable directly, then the result of the Executor is recorded. Of course this result would always be null or an exception. This is what the current Failsafe implementation does.
  2. Another way is to have the Executor call a Runnable that wraps not just the user-provided Supplier/Runnable, but also the Failsafe code that records its result. This way the result from the Executor doesn’t matter, it’s just there for performing side-effects if needed, almost like an event listener. Also, if the Executor throws an exception, it won’t be recorded as part of the Failsafe handling since recording a result happens before the Executor completes. This is what your commit does.

I’m guessing you’d prefer the second option? When you say things partially works now, is that because using an Executor returns null currently? Maybe you could share your thoughts on the options.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Executor Interfaces - Java™ Tutorials - Oracle Help Center
This Java tutorial describes exceptions, basic input/output, concurrency, regular expressions, and the platform environment.
Read more >
A Guide to the Java ExecutorService - Baeldung
An intro and guide to the ExecutorService framework provided by the JDK - which simplifies the execution of tasks in asynchronous mode.
Read more >
What is Java Executor Framework? - GeeksforGeeks
Java executor framework (java.util.concurrent.Executor), released with the JDK 5 is used to run the Runnable objects without creating new ...
Read more >
Guide to ExecutorService in Java - HowToDoInJava
ExecutorService is an interface and its implementations can execute a Runnable or Callable class in an asynchronous way. Note that invoking the ...
Read more >
Java ExecutorService and Thread Pools Tutorial - CalliCoder
Executors Framework · Executor - A simple interface that contains a method called execute() to launch a task specified by a Runnable object....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found