Chazz1
Chazz1

Reputation: 133

Re-execute Callable if it fails

Having an ExecutorService that executes a Callable that is supposed to be always running, what is the best implementation to relaunch it when an error happens?

Currently my source code looks something like this:

Future<Void> future = executorService.submit(new AlwaysOnlineCallable(config));
     try {
          future.get();
     } catch (Exception e) {
          //TODO thinking on execcuting the callable here
          e.printStackTrace();
     }

For what I've seen, cases like this are generally treated with runables.

Upvotes: 0

Views: 207

Answers (1)

tgdavies
tgdavies

Reputation: 11421

Why not simply:

ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit((Runnable) () -> {
            while (true) {
                try {
                    ...
                } catch (Exception e) {
                    // log
                }
            }
        });

There doesn't seem to be any need for a Callable or a Future.

Upvotes: 2

Related Questions