public synchronized void start(CommandExecutor commandExecutor) { if (isStarted()) { return; } this.commandExecutor = requireNonNull(commandExecutor, "commandExecutor"); scheduler = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor( new DefaultThreadFactory("mirroring-scheduler", true))); worker = MoreExecutors.listeningDecorator( new ThreadPoolExecutor(0, numThreads, 1, TimeUnit.MINUTES, new SynchronousQueue<>(), new DefaultThreadFactory("mirroring-worker", true))); final ListenableScheduledFuture<?> future = scheduler.scheduleWithFixedDelay( this::schedulePendingMirrors, TICK.getSeconds(), TICK.getSeconds(), TimeUnit.SECONDS); FuturesExtra.addFailureCallback( future, cause -> logger.error("Git-to-CD mirroring scheduler stopped due to an unexpected exception:", cause)); }
private void scheduleBalance() { if (isLeader.get() && nextTask.get() == null) { ListenableScheduledFuture task = executorService.schedule(mastershipAdminService::balanceRoles, SCHEDULE_PERIOD, TimeUnit.SECONDS); task.addListener(() -> { log.info("Completed balance roles"); nextTask.set(null); }, MoreExecutors.directExecutor() ); if (!nextTask.compareAndSet(null, task)) { task.cancel(false); } } }
@Override public void start() { log.debug("Discovery service starting."); log.debug("Hazelcast instance: {}.", hazelcastInstance); log.debug("Neighbourhood map: {}.", members); running.set(true); hazelcastInstance.getLifecycleService().addLifecycleListener(this::onHazelcastStateChange); log.debug("Waiting for initialization to complete."); updateMap(); final ListenableScheduledFuture<?> mapUpdateTask = executorService.scheduleAtFixedRate(this::updateMap, 10L, 10L, TimeUnit.SECONDS); Futures.addCallback(mapUpdateTask, new FutureCallback<Object>() { @Override public void onSuccess(final Object result) { // Empty } @Override public void onFailure(final @NonNull Throwable t) { log.error("Map update failed.", t); } }); log.info("Discovery service started."); }
public T schedule(PollTask<T> task, int interval, int maxAttempt, int maxFailureTolerant) throws ExecutionException, InterruptedException, TimeoutException { T result; int actualFailureTolerant = 0; for (int i = 0; i < maxAttempt; i++) { if (task.cancelled()) { throw new CancellationException("Task was cancelled."); } try { ListenableScheduledFuture<T> ft = schedule(task, interval); result = ft.get(); if (task.completed(result)) { return result; } } catch (Exception ex) { actualFailureTolerant++; if (actualFailureTolerant >= maxFailureTolerant) { throw ex; } } } throw new TimeoutException(String.format("Task did not finished within %d seconds", interval * maxAttempt)); }
@Override public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { Preconditions.checkNotNull(command, "command must not be null"); Preconditions.checkNotNull(unit, "unit must not be null!"); return schedule(java.util.concurrent.Executors.callable(command), delay, unit); }
@Override public <V> ListenableScheduledFuture<V> schedule(final Callable<V> callable, long delay, TimeUnit unit) { Preconditions.checkNotNull(callable, "callable must not be null!"); Preconditions.checkNotNull(unit, "unit must not be null!"); ListenableFuture<V> delegateFuture = submit(callable); return new ImmediateScheduledFuture<V>(delegateFuture); }
private void scheduleNextRefresh(boolean refreshWasSuccessful) { int delayInMinutes = refreshWasSuccessful ? SUCESSFUL_DOWNLOAD_DELAY_MINUTES : DOWNLOAD_FAILURE_DELAY_MINUTES; ListenableScheduledFuture<String> refreshResults = executor.schedule(new WebExperimentsDownloader(), delayInMinutes, TimeUnit.MINUTES); refreshResults.addListener( new WebExperimentsResultProcessor(refreshResults), MoreExecutors.directExecutor()); }
@Override public void valueChanged(ListSelectionEvent e) { boolean hasFocus = otrosApplication.getApplicationJFrame().isFocused(); final boolean enabled = otrosApplication.getConfiguration().getBoolean(ConfKeys.JUMP_TO_CODE_AUTO_JUMP_ENABLED, false); if (hasFocus && enabled && !e.getValueIsAdjusting()) { try { final LogData logData = dataTableModel.getLogData(table.convertRowIndexToModel(e.getFirstIndex())); Optional<Integer> line = Optional.empty(); if (StringUtils.isNotBlank(logData.getLine()) && StringUtils.isAlphanumeric(logData.getLine())) { line = Optional.of(Integer.valueOf(logData.getLine())); } final LocationInfo li = new LocationInfo( Optional.ofNullable(logData.getClazz()).orElseGet(logData::getLoggerName), logData.getMethod(), logData.getFile(), line, Optional.ofNullable(logData.getMessage())); final JumpToCodeService jumpToCodeService = otrosApplication.getServices().getJumpToCodeService(); final boolean ideAvailable = jumpToCodeService.isIdeAvailable(); if (ideAvailable) { scheduledJump.map(input -> { input.cancel(false); return Boolean.TRUE; }); ListeningScheduledExecutorService scheduledExecutorService = otrosApplication.getServices().getTaskSchedulerService().getListeningScheduledExecutorService(); delayMs = 300; ListenableScheduledFuture<?> jump = scheduledExecutorService.schedule( new JumpRunnable(li, jumpToCodeService), delayMs, TimeUnit.MILLISECONDS ); scheduledJump = Optional.of(jump); } } catch (Exception e1) { LOGGER.warn("Can't perform jump to code: " + e1.getMessage(), e1); e1.printStackTrace(); } } }
@Override public <V> ListenableScheduledFuture<V> schedule( final Callable<V> callable, long delay, TimeUnit unit) { Preconditions.checkNotNull(callable, "callable must not be null!"); Preconditions.checkNotNull(unit, "unit must not be null!"); ListenableFuture<V> delegateFuture = submit(callable); return new ImmediateScheduledFuture<V>(delegateFuture); }
@Override public ListenableScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { throw new UnsupportedOperationException( "scheduleAtFixedRate is not supported."); }
@Override public ListenableScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { throw new UnsupportedOperationException( "scheduleWithFixedDelay is not supported."); }
@Override public <V> ListenableScheduledFuture<V> schedule( Callable<V> callable, long delay, TimeUnit unit) { return NeverScheduledFuture.create(); }
@Override public ListenableScheduledFuture<?> schedule( Runnable command, long delay, TimeUnit unit) { return NeverScheduledFuture.create(); }
@Override public ListenableScheduledFuture<?> scheduleAtFixedRate( Runnable command, long initialDelay, long period, TimeUnit unit) { return NeverScheduledFuture.create(); }
@Override public ListenableScheduledFuture<?> scheduleWithFixedDelay( Runnable command, long initialDelay, long delay, TimeUnit unit) { return NeverScheduledFuture.create(); }
public static ListenableScheduledFuture<?> scheduleTask(long millis, Runnable r) { return service.schedule(r, millis, TimeUnit.MILLISECONDS); }
public static ListenableScheduledFuture<?> scheduleTask(long millis, long period, Runnable r) { return service.scheduleAtFixedRate(r, millis, period, TimeUnit.MILLISECONDS); }
@Override public ListenableScheduledFuture<?> schedule(final Runnable command, final long delay, final TimeUnit unit) { return delegate().schedule(wrap(command, MDC.getCopyOfContextMap()), delay, unit); }
@Override public <V> ListenableScheduledFuture<V> schedule(final Callable<V> callable, final long delay, final TimeUnit unit) { return delegate().schedule(wrap(callable, MDC.getCopyOfContextMap()), delay, unit); }
@Override public ListenableScheduledFuture<?> scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TimeUnit unit) { return delegate().scheduleAtFixedRate(wrap(command, MDC.getCopyOfContextMap()), initialDelay, period, unit); }
@Override public ListenableScheduledFuture<?> scheduleWithFixedDelay(final Runnable command, final long initialDelay, final long delay, final TimeUnit unit) { return delegate().scheduleWithFixedDelay(wrap(command, MDC.getCopyOfContextMap()), initialDelay, delay, unit); }
ListenableScheduledFuture<?> scheduleAtFixedRate( Runnable command, long initialDelay, long period, TimeUnit unit);
@Override public ListenableScheduledFuture<?> scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TimeUnit unit) { return executorService.scheduleAtFixedRate(command, initialDelay, period, unit); }