Example usage for java.util.concurrent ScheduledFuture cancel

List of usage examples for java.util.concurrent ScheduledFuture cancel

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledFuture cancel.

Prototype

boolean cancel(boolean mayInterruptIfRunning);

Source Link

Document

Attempts to cancel execution of this task.

Usage

From source file:org.openhab.binding.amazonechocontrol.handler.FlashBriefingProfileHandler.java

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    AccountHandler accountHandler = this.accountHandler;
    if (accountHandler == null) {
        return;// w ww  .  j a  v  a  2 s  . c  om
    }
    int waitForUpdate = -1;

    ScheduledFuture<?> updateStateJob = this.updateStateJob;
    this.updateStateJob = null;
    if (updateStateJob != null) {
        updateStateJob.cancel(false);
    }
    try {
        String channelId = channelUID.getId();
        if (command instanceof RefreshType) {
            waitForUpdate = 0;
        }
        if (channelId.equals(CHANNEL_SAVE)) {
            if (command.equals(OnOffType.ON)) {
                saveCurrentProfile(accountHandler);
                waitForUpdate = 500;
            }
        }
        if (channelId.equals(CHANNEL_ACTIVE)) {
            if (command.equals(OnOffType.ON)) {
                String currentConfigurationJson = this.currentConfigurationJson;
                if (!currentConfigurationJson.isEmpty()) {
                    accountHandler.setEnabledFlashBriefingsJson(currentConfigurationJson);
                    updateState(CHANNEL_ACTIVE, OnOffType.ON);
                    waitForUpdate = 500;
                }
            }
        }
        if (channelId.equals(CHANNEL_PLAY_ON_DEVICE)) {
            if (command instanceof StringType) {
                String deviceSerialOrName = ((StringType) command).toFullString();
                String currentConfigurationJson = this.currentConfigurationJson;
                if (!currentConfigurationJson.isEmpty()) {
                    String old = accountHandler.getEnabledFlashBriefingsJson();
                    accountHandler.setEnabledFlashBriefingsJson(currentConfigurationJson);
                    Device device = accountHandler.findDeviceJsonBySerialOrName(deviceSerialOrName);
                    if (device == null) {
                        logger.warn("Device '{}' not found", deviceSerialOrName);
                    } else {
                        @Nullable
                        Connection connection = accountHandler.findConnection();
                        if (connection == null) {
                            logger.warn("Connection for '{}' not found",
                                    accountHandler.getThing().getUID().getId());
                        } else {
                            connection.executeSequenceCommand(device, "Alexa.FlashBriefing.Play", null);

                            scheduler.schedule(() -> accountHandler.setEnabledFlashBriefingsJson(old), 1000,
                                    TimeUnit.MILLISECONDS);

                            updateState(CHANNEL_ACTIVE, OnOffType.ON);
                        }
                    }
                    updatePlayOnDevice = true;
                    waitForUpdate = 1000;
                }
            }
        }
    } catch (IOException | URISyntaxException e) {
        logger.warn("Handle command failed {}", e);
    }
    if (waitForUpdate >= 0) {
        this.updateStateJob = scheduler.schedule(() -> accountHandler.updateFlashBriefingHandlers(),
                waitForUpdate, TimeUnit.MILLISECONDS);
    }
}

From source file:io.gravitee.gateway.services.apikeyscache.ApiKeysCacheService.java

private void stopRefresher(Api api) {
    ScheduledFuture scheduledFuture = scheduledTasks.remove(api);
    if (scheduledFuture != null) {
        if (!scheduledFuture.isCancelled()) {
            LOGGER.info("Stop api-keys refresher");
            scheduledFuture.cancel(true);
        } else {// w ww. ja  va  2 s  .c  om
            LOGGER.info("API-key refresher already shutdown");
        }
    }
}

From source file:org.hyperic.hq.notifications.EndpointQueue.java

public NotificationEndpoint unregister(String regID) {
    AccumulatedRegistrationData data = null;
    synchronized (registrationData) {
        // don't delete the data, we want to be able to access the endpoint by registrationId
        data = registrationData.get(regID);
        if (data == null || !data.isValid()) {
            if (log.isDebugEnabled()) {
                log.debug((data == null ? "No queue assigned" : "Queue is already invalid") + " for regId: "
                        + regID);//from   w w  w .  java 2 s . com
            }
            return null;
        }
        numConsumers.decrementAndGet();
        data.markInvalid();
        data.clear();
        final ScheduledFuture<?> schedule = data.getSchedule();
        if (schedule != null) {
            schedule.cancel(true);
        }
        if (log.isDebugEnabled()) {
            log.debug("Removing the queue assigned for regId: " + regID);
        }
    }
    return data.getNotificationEndpoint();
}

From source file:org.apache.hadoop.hbase.ChoreService.java

/**
 * @param chore The Chore to be rescheduled. If the chore is not scheduled with this ChoreService
 *          yet then this call is equivalent to a call to scheduleChore.
 *///from w  ww . ja  v  a2  s .  c om
private synchronized void rescheduleChore(ScheduledChore chore) {
    if (chore == null)
        return;

    if (scheduledChores.containsKey(chore)) {
        ScheduledFuture<?> future = scheduledChores.get(chore);
        future.cancel(false);
    }
    scheduleChore(chore);
}

From source file:org.apache.hadoop.hbase.ChoreService.java

@Override
public synchronized void cancelChore(ScheduledChore chore, boolean mayInterruptIfRunning) {
    if (chore != null && scheduledChores.containsKey(chore)) {
        ScheduledFuture<?> future = scheduledChores.get(chore);
        future.cancel(mayInterruptIfRunning);
        scheduledChores.remove(chore);/*from  www  .jav a2s .c  o  m*/

        // Removing a chore that was missing its start time means it may be possible
        // to reduce the number of threads
        if (choresMissingStartTime.containsKey(chore)) {
            choresMissingStartTime.remove(chore);
            requestCorePoolDecrease();
        }
    }
}

From source file:org.eclipse.nebula.widgets.nattable.blink.BlinkLayer.java

@Override
public LabelStack getConfigLabelsByPosition(int columnPosition, int rowPosition) {
    if (!blinkingEnabled) {
        return getUnderlyingLayer().getConfigLabelsByPosition(columnPosition, rowPosition);
    }/*from ww w .j  a v  a  2  s.c  o m*/

    ILayerCell cell = underlyingLayer.getCellByPosition(columnPosition, rowPosition);

    int columnIndex = getUnderlyingLayer().getColumnIndexByPosition(columnPosition);
    String columnProperty = columnPropertyResolver.getColumnProperty(columnIndex);

    int rowIndex = getUnderlyingLayer().getRowIndexByPosition(rowPosition);
    String rowId = rowIdAccessor.getRowId(rowDataProvider.getRowObject(rowIndex)).toString();

    String key = updateEventsCache.getKey(columnProperty, rowId);

    LabelStack underlyingLabelStack = getUnderlyingLayer().getConfigLabelsByPosition(columnPosition,
            rowPosition);

    // Cell has been updated
    if (updateEventsCache.isUpdated(key)) {
        PropertyUpdateEvent<T> event = updateEventsCache.getEvent(key);

        // Old update in middle of a blink - cancel it
        ScheduledFuture<?> scheduledFuture = blinkingTasks.remove(key);
        blinkingUpdates.remove(key);
        if (scheduledFuture != null) {
            scheduledFuture.cancel(true);
        }

        LabelStack blinkingConfigTypes = resolveConfigTypes(cell, event.getOldValue(), event.getNewValue());

        // start blinking cell
        if (blinkingConfigTypes != null) {
            Runnable stopBlinkTask = getStopBlinkTask(key, this);
            blinkingUpdates.put(key, event);
            updateEventsCache.remove(key);
            blinkingTasks.put(key,
                    scheduler.schedule(stopBlinkTask, blinkDurationInMilis, TimeUnit.MILLISECONDS));
            return blinkingConfigTypes;
        } else {
            return new LabelStack();
        }
    }
    // Previous blink timer is still running
    if (blinkingUpdates.containsKey(key)) {
        PropertyUpdateEvent<T> event = blinkingUpdates.get(key);
        return resolveConfigTypes(cell, event.getOldValue(), event.getNewValue());

    }
    return underlyingLabelStack;
}

From source file:org.openhab.binding.astro.internal.handler.AstroThingHandler.java

/**
 * Stops all jobs for this thing.//from   ww w.  j  av  a  2s  .c o m
 */
private void stopJobs() {
    logger.debug("Stopping scheduled jobs for thing {}", getThing().getUID());
    monitor.lock();
    try {
        if (cronScheduler != null) {
            if (dailyJob != null) {
                dailyJob.cancel(true);
            }
            dailyJob = null;
        }
        for (ScheduledFuture<?> future : scheduledFutures) {
            if (!future.isDone()) {
                future.cancel(true);
            }
        }
        scheduledFutures.clear();
    } catch (Exception ex) {
        logger.error("{}", ex.getMessage(), ex);
    } finally {
        monitor.unlock();
    }
}

From source file:com.alibaba.cobar.client.datasources.ha.FailoverHotSwapDataSourceCreator.java

public void destroy() throws Exception {

    for (Map.Entry<ScheduledFuture<?>, ScheduledExecutorService> e : schedulerFutures.entrySet()) {
        ScheduledFuture<?> future = e.getKey();
        ScheduledExecutorService scheduler = e.getValue();
        future.cancel(true);
        shutdownExecutor(scheduler);/*from www.  ja  va  2  s . co m*/
    }

    for (ExecutorService executor : jobExecutorRegistry) {
        shutdownExecutor(executor);
    }
}

From source file:org.apache.rya.benchmark.periodic.KafkaLatencyBenchmark.java

private void cancelAllScheduledTasks() {
    logger.info("Canceling all tasks");
    for (final ScheduledFuture<?> task : futureList) {
        task.cancel(false);
    }//from   w  w w.j  a  va  2s . co m
    futureList.clear();
}

From source file:cn.vko.cache.dao.ha.FailoverHotSwapDataSourceCreator.java

@Override
public void destroy() throws Exception {
    for (Map.Entry<ScheduledFuture<?>, ScheduledExecutorService> e : schedulerFutures.entrySet()) {
        ScheduledFuture<?> future = e.getKey();
        ScheduledExecutorService scheduler = e.getValue();
        future.cancel(true);
        shutdownExecutor(scheduler);/*ww w  .  ja va  2s  .co m*/
    }

    for (ExecutorService executor : jobExecutorRegistry) {
        shutdownExecutor(executor);
    }
}