@Test public void runnerRunsOneTask() { SafeShutdownRunner runner = new SafeShutdownRunner(Duration.ofSeconds(1)); runner.shutdownSafely(mockRunnable); verify(mockRunnable, times(1)).run(); assertThatCode(runner::close).doesNotThrowAnyException(); }
public void shutdownSafely(Runnable shutdownCallback) { Future<?> future = executor.submit(shutdownCallback); try { future.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); failures.add(e); } catch (ExecutionException e) { failures.add(e.getCause()); } catch (TimeoutException e) { future.cancel(true); failures.add(e); } }
SafeShutdownRunner implements AutoCloseable { public void shutdownSafely(Runnable shutdownCallback) { Future<?> future = executor.submit(shutdownCallback); try { future.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); failures.add(e); } catch (ExecutionException e) { failures.add(e.getCause()); } catch (TimeoutException e) { future.cancel(true); failures.add(e); } } }
SafeShutdownRunner implements AutoCloseable { public void shutdownSafely(Runnable shutdownCallback) { Future<?> future = executor.submit(shutdownCallback); try { future.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); failures.add(e); } catch (ExecutionException e) { failures.add(e.getCause()); } catch (TimeoutException e) { future.cancel(true); failures.add(e); } } SafeShutdownRunner(Duration timeoutDuration); }
SafeShutdownRunner implements AutoCloseable { public void shutdownSafely(Runnable shutdownCallback) { Future<?> future = executor.submit(shutdownCallback); try { future.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); failures.add(e); } catch (ExecutionException e) { failures.add(e.getCause()); } catch (TimeoutException e) { future.cancel(true); failures.add(e); } } SafeShutdownRunner(Duration timeoutDuration); void shutdownSafely(Runnable shutdownCallback); @Override void close(); }
SafeShutdownRunner implements AutoCloseable { public void shutdownSafely(Runnable shutdownCallback) { Future<?> future = executor.submit(shutdownCallback); try { future.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); failures.add(e); } catch (ExecutionException e) { failures.add(e.getCause()); } catch (TimeoutException e) { future.cancel(true); failures.add(e); } } SafeShutdownRunner(Duration timeoutDuration); void shutdownSafely(Runnable shutdownCallback); @Override void close();  }