Java Code Examples for java.util.concurrent.ScheduledFuture#isCancelled()

The following examples show how to use java.util.concurrent.ScheduledFuture#isCancelled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LifxLightDiscovery.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void stopBackgroundDiscovery() {
    logger.debug("Stopping LIFX device background discovery");

    ScheduledFuture<?> localDiscoveryJob = discoveryJob;
    if (localDiscoveryJob != null && !localDiscoveryJob.isCancelled()) {
        localDiscoveryJob.cancel(true);
        discoveryJob = null;
    }

    ScheduledFuture<?> localNetworkJob = networkJob;
    if (localNetworkJob != null && !localNetworkJob.isCancelled()) {
        localNetworkJob.cancel(true);
        networkJob = null;
    }
}
 
Example 2
Source File: OntologyMapper.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
private void disposeHelper() {
	String helperKey = buildHelperKey(ontologySettings);
	if (checkers.containsKey(helperKey)) {
		ScheduledFuture checker = checkers.remove(helperKey);
		if (!checker.isCancelled() && !checker.isDone()) {
			logger.debug("Cancelling ScheduledFuture for {}", helperKey);
			checker.cancel(false);
		}
	}
	if (helpers.containsKey(helperKey)) {
		OntologyHelper helper = helpers.remove(helperKey);
		helper.dispose();
	}

	threadPool.stats();
	logger.debug("helpers.size() = {}; checkers.size() = {}", helpers.size(), checkers.size());
}
 
Example 3
Source File: LifxLightCommunicationHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public void start() {
    try {
        lock.lock();

        logger.debug("{} : Starting communication handler", logId);
        logger.debug("{} : Using '{}' as source identifier", logId, Long.toString(sourceId, 16));

        ScheduledFuture<?> localNetworkJob = networkJob;
        if (localNetworkJob == null || localNetworkJob.isCancelled()) {
            networkJob = scheduler.scheduleWithFixedDelay(this::receiveAndHandlePackets, 0, PACKET_INTERVAL,
                    TimeUnit.MILLISECONDS);
        }

        currentLightState.setOffline();

        Selector localSelector = Selector.open();
        selector = localSelector;

        if (isBroadcastEnabled()) {
            broadcastKey = openBroadcastChannel(selector, logId, broadcastPort);
            selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host,
                    macAddress, broadcastKey, unicastKey);
            broadcastPacket(new GetServiceRequest());
        } else {
            unicastKey = openUnicastChannel(selector, logId, host);
            selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host,
                    macAddress, broadcastKey, unicastKey);
            sendPacket(new GetServiceRequest());
        }
    } catch (IOException e) {
        logger.error("{} while starting LIFX communication handler for light '{}' : {}",
                e.getClass().getSimpleName(), logId, e.getMessage(), e);
    } finally {
        lock.unlock();
    }
}
 
Example 4
Source File: PollingUsbSerialScanner.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Stops repeatedly scanning for newly added and removed USB devices. This can be restarted using
 * {@link #startBackgroundScanning()}.
 */
@Override
public synchronized void stopBackgroundScanning() {
    logger.debug("Stopping USB-Serial background discovery");
    ScheduledFuture<?> currentBackgroundScanningJob = backgroundScanningJob;
    if (currentBackgroundScanningJob != null && !currentBackgroundScanningJob.isCancelled()) {
        if (currentBackgroundScanningJob.cancel(true)) {
            backgroundScanningJob = null;
            logger.debug("Stopped USB-serial background discovery");
        }
    }
}
 
Example 5
Source File: HeaderExchangeServer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void stopHeartbeatTimer() {
    try {
        ScheduledFuture<?> timer = heatbeatTimer;
        if (timer != null && ! timer.isCancelled()) {
            timer.cancel(true);
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    } finally {
        heatbeatTimer =null;
    }
}
 
Example 6
Source File: LifxLightDiscovery.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void startBackgroundDiscovery() {
    logger.debug("Starting the LIFX device background discovery");

    ScheduledFuture<?> localDiscoveryJob = discoveryJob;
    if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
        discoveryJob = scheduler.scheduleWithFixedDelay(this::doScan, 0, REFRESH_INTERVAL, TimeUnit.SECONDS);
    }
}
 
Example 7
Source File: LifxLightOnlineStateUpdater.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public void start() {
    try {
        lock.lock();
        communicationHandler.addResponsePacketListener(this::handleResponsePacket);
        ScheduledFuture<?> localEchoJob = echoJob;
        if (localEchoJob == null || localEchoJob.isCancelled()) {
            echoJob = scheduler.scheduleWithFixedDelay(this::sendEchoPackets, 0, ECHO_POLLING_INTERVAL,
                    TimeUnit.SECONDS);
        }
    } catch (Exception e) {
        logger.error("Error occurred while starting online state poller for a light ({})", logId, e);
    } finally {
        lock.unlock();
    }
}
 
Example 8
Source File: FrontendServer.java    From anima with GNU General Public License v3.0 5 votes vote down vote up
private void stopHeartbeatTimer() {
    try {
        ScheduledFuture<?> timer = heatbeatTimer;
        if (timer != null && ! timer.isCancelled()) {
            timer.cancel(true);
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    } finally {
        heatbeatTimer =null;
    }
}
 
Example 9
Source File: ServerSurrogate.java    From anima with GNU General Public License v3.0 5 votes vote down vote up
private void stopHeartbeatTimer() {
	try {
		ScheduledFuture<?> timer = heatbeatTimer;
		if (timer != null && !timer.isCancelled()) {
			timer.cancel(true);
		}
	} catch (Throwable t) {
		logger.warn(t.getMessage(), t);
	} finally {
		heatbeatTimer = null;
	}
}
 
Example 10
Source File: LifxLightCurrentStateUpdater.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public void start() {
    try {
        lock.lock();
        communicationHandler.addResponsePacketListener(this::handleResponsePacket);
        ScheduledFuture<?> localStatePollingJob = statePollingJob;
        if (localStatePollingJob == null || localStatePollingJob.isCancelled()) {
            statePollingJob = scheduler.scheduleWithFixedDelay(this::pollLightState, 0, STATE_POLLING_INTERVAL,
                    TimeUnit.SECONDS);
        }
    } catch (Exception e) {
        logger.error("Error occurred while starting light state updater", e);
    } finally {
        lock.unlock();
    }
}
 
Example 11
Source File: PollingUsbSerialScanner.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Stops repeatedly scanning for newly added and removed USB devices. This can be restarted using
 * {@link #startBackgroundScanning()}.
 */
@Override
public synchronized void stopBackgroundScanning() {
    logger.debug("Stopping USB-Serial background discovery");
    ScheduledFuture<?> currentBackgroundScanningJob = backgroundScanningJob;
    if (currentBackgroundScanningJob != null && !currentBackgroundScanningJob.isCancelled()) {
        if (currentBackgroundScanningJob.cancel(true)) {
            backgroundScanningJob = null;
            logger.debug("Stopped USB-serial background discovery");
        }
    }
}
 
Example 12
Source File: SimpleTimer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public SimpleTimerTask setUp(@Nonnull final Runnable runnable, final long delay) {
  final ScheduledFuture<?> future = myScheduledExecutorService.schedule(runnable, delay, TimeUnit.MILLISECONDS);
  return new SimpleTimerTask() {
    @Override
    public void cancel() {
      future.cancel(false);
    }

    @Override
    public boolean isCancelled() {
      return future.isCancelled();
    }
  };
}
 
Example 13
Source File: HeaderExchangeServer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private void stopHeartbeatTimer() {
    try {
        ScheduledFuture<?> timer = heatbeatTimer;
        if (timer != null && ! timer.isCancelled()) {
            timer.cancel(true);
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    } finally {
        heatbeatTimer =null;
    }
}
 
Example 14
Source File: HeaderExchangeServer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void stopHeartbeatTimer() {
    try {
        ScheduledFuture<?> timer = heatbeatTimer;
        if (timer != null && ! timer.isCancelled()) {
            timer.cancel(true);
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    } finally {
        heatbeatTimer =null;
    }
}
 
Example 15
Source File: LifxLightPropertiesUpdater.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public void stop() {
    try {
        lock.lock();
        communicationHandler.removeResponsePacketListener(this::handleResponsePacket);
        ScheduledFuture<?> localUpdateJob = updateJob;
        if (localUpdateJob != null && !localUpdateJob.isCancelled()) {
            localUpdateJob.cancel(true);
            updateJob = null;
        }
    } catch (Exception e) {
        logger.error("Error occurred while stopping properties update job for a light ({})", logId, e);
    } finally {
        lock.unlock();
    }
}
 
Example 16
Source File: TaskCenter.java    From util4j with Apache License 2.0 5 votes vote down vote up
public void close() {
	System.out.println("TaskCenter closing");
	this.monitoring.cancel(true);

	// 停止所有定时任务
	for (ScheduledFuture<?> sf : this.scheduledFutureList) {
		if (!sf.isCancelled() || !sf.isDone()) {
			sf.cancel(true);
		}
	}
	this.scheduledFutureList.clear();

	Iterator<Timer> iter = this.timers.values().iterator();
	while (iter.hasNext()) {
		Timer timer = iter.next();
		timer.cancel();
	}
	this.timers.clear();

	// 关闭滑动窗
	this.slidingWindow.stop();

	// 关闭线程池
	this.mainExecutor.shutdown();
	this.scheduledExecutor.shutdown();
	System.out.println("TaskCenter closed");
}
 
Example 17
Source File: ManagedScheduledServiceTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * Happy path with single task to be executed periodically until it's canceled.
 *
 * @throws InterruptedException we don't expect it
 */
@Test
public void periodicFixedDelayTask() throws InterruptedException {
    final CountDownLatch countDownLatch = new CountDownLatch(4); // execute 4 times
    final ScheduledFuture<?> scheduledFuture = scheduledService.periodicFixedDelayTask(1, null, countDownLatch);
    LOGGER.info("Do some other work while we wait for the tasks");
    countDownLatch.await(500, TimeUnit.MILLISECONDS);
    if (!scheduledFuture.isCancelled()) {
        scheduledFuture.cancel(true);
        LOGGER.info("task stopped");
    }
}
 
Example 18
Source File: ScheduleExecutor.java    From davos with MIT License 4 votes vote down vote up
public void stopSchedule(Long id) throws ScheduleNotRunningException {

        if (runningSchedules.containsKey(id)) {

            LOGGER.info("Stopping schedule {}", id);

            ScheduledFuture<?> future = runningSchedules.get(id).getFuture();
            
            if (!future.isCancelled()) {

                future.cancel(true);
                runningSchedules.remove(id);
                LOGGER.info("Schedule should now be stopped");
            }

        } else {
            throw new ScheduleNotRunningException();
        }
    }
 
Example 19
Source File: JingleRtpConnection.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
private void cancelRingingTimeout() {
    final ScheduledFuture<?> future = this.ringingTimeoutFuture;
    if (future != null && !future.isCancelled()) {
        future.cancel(false);
    }
}
 
Example 20
Source File: ScheduledTask.java    From schedule-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public boolean isCancelled() {
    ScheduledFuture<?> future = this.future;
    if (future == null) return true;
    return future.isCancelled();
}