Search in sources :

Example 51 with Timer

use of java.util.Timer in project openhab1-addons by openhab.

the class KNXConnection method updated.

/*
     * (non-Javadoc)
     * 
     * @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
     */
@Override
public void updated(Dictionary<String, ?> config) throws ConfigurationException {
    if (config != null) {
        sLogger.debug("KNXBinding configuration present. Setting up KNX bus connection.");
        sIp = (String) config.get("ip");
        String readingBusAddrString = (String) config.get("busaddr");
        if (StringUtils.isNotBlank(readingBusAddrString)) {
            sLocalSourceAddr = readingBusAddrString;
        }
        String readingIgnLocEv = (String) config.get("ignorelocalevents");
        if (StringUtils.isNotBlank(readingIgnLocEv)) {
            sIgnoreLocalSourceEvents = readingIgnLocEv.equalsIgnoreCase("true");
        }
        String connectionTypeString = (String) config.get("type");
        if (StringUtils.isNotBlank(connectionTypeString)) {
            if ("TUNNEL".equals(connectionTypeString)) {
                sIpConnectionType = KNXNetworkLinkIP.TUNNELING;
            } else if ("ROUTER".equals(connectionTypeString)) {
                sIpConnectionType = KNXNetworkLinkIP.ROUTING;
                if (StringUtils.isBlank(sIp)) {
                    sIp = DEFAULT_MULTICAST_IP;
                }
            } else {
                throw new ConfigurationException("type", "unknown IP connection type '" + connectionTypeString + "'! Known types are either 'TUNNEL' or 'ROUTER'");
            }
        } else {
            sIpConnectionType = KNXNetworkLinkIP.TUNNELING;
        }
        String portConfig = (String) config.get("port");
        if (StringUtils.isNotBlank(portConfig)) {
            sPort = Integer.parseInt(portConfig);
        } else {
            sPort = KNXnetIPConnection.DEFAULT_PORT;
        }
        sLocalIp = (String) config.get("localIp");
        sSerialPort = (String) config.get("serialPort");
        String readingPauseString = (String) config.get("pause");
        if (StringUtils.isNotBlank(readingPauseString)) {
            sReadingPause = Long.parseLong(readingPauseString);
        }
        String responseTimeoutString = (String) config.get("timeout");
        if (StringUtils.isNotBlank(responseTimeoutString)) {
            long timeout = Long.parseLong(responseTimeoutString);
            if (timeout > 0) {
                sResponseTimeout = timeout;
            }
        }
        String readRetriesLimitString = (String) config.get("readRetries");
        if (StringUtils.isNotBlank(readRetriesLimitString)) {
            int readRetries = Integer.parseInt(readRetriesLimitString);
            if (readRetries > 0) {
                sReadRetriesLimit = readRetries;
            }
        }
        String autoReconnectPeriodString = (String) config.get("autoReconnectPeriod");
        if (StringUtils.isNotBlank(autoReconnectPeriodString)) {
            int autoReconnectPeriodValue = Integer.parseInt(autoReconnectPeriodString);
            if (autoReconnectPeriodValue >= 0) {
                sAutoReconnectPeriod = autoReconnectPeriodValue;
            }
        }
        String maxRefreshQueueEntriesString = (String) config.get("maxRefreshQueueEntries");
        if (StringUtils.isNotBlank(maxRefreshQueueEntriesString)) {
            try {
                int maxRefreshQueueEntriesValue = Integer.parseInt(maxRefreshQueueEntriesString);
                if (maxRefreshQueueEntriesValue >= 0) {
                    sMaxRefreshQueueEntries = maxRefreshQueueEntriesValue;
                }
            } catch (NumberFormatException e) {
                sLogger.warn("Error when trying to read parameter 'maxRefreshQueueEntries' from configuration. '{}' is not a number: using default.", maxRefreshQueueEntriesString);
            }
        }
        String numberOfThreadsString = (String) config.get("numberOfThreads");
        if (StringUtils.isNotBlank(numberOfThreadsString)) {
            try {
                int numberOfThreadsValue = Integer.parseInt(numberOfThreadsString);
                if (numberOfThreadsValue >= 0) {
                    sNumberOfThreads = numberOfThreadsValue;
                }
            } catch (NumberFormatException e) {
                sLogger.warn("Error when trying to read parameter 'numberOfThreads' from configuration. '{}' is not a number: using default.", numberOfThreadsString);
            }
        }
        String scheduledExecutorServiceShutdownTimeoutString = (String) config.get("scheduledExecutorServiceShutdownTimeout");
        if (StringUtils.isNotBlank(scheduledExecutorServiceShutdownTimeoutString)) {
            try {
                int scheduledExecutorServiceShutdownTimeoutValue = Integer.parseInt(scheduledExecutorServiceShutdownTimeoutString);
                if (scheduledExecutorServiceShutdownTimeoutValue >= 0) {
                    sScheduledExecutorServiceShutdownTimeout = scheduledExecutorServiceShutdownTimeoutValue;
                }
            } catch (NumberFormatException e) {
                sLogger.warn("Error when trying to read parameter 'scheduledExecutorServiceShutdownTimeout' from configuration. '{}' is not a number: using default.", scheduledExecutorServiceShutdownTimeoutString);
            }
        }
        String shouldUseNAT = (String) config.get("useNAT");
        sUseNAT = StringUtils.isNotBlank(shouldUseNAT) && shouldUseNAT.equalsIgnoreCase("true");
        if (sPC == null) {
            sLogger.debug("Not connected yet. Trying to connect.");
            if (!connect()) {
                sLogger.warn("Initial connection to KNX bus failed!");
                if (sAutoReconnectPeriod > 0) {
                    sLogger.info("KNX link will be retried in {} seconds", sAutoReconnectPeriod);
                    final Timer timer = new Timer();
                    TimerTask timerTask = new ConnectTimerTask(timer);
                    timer.schedule(timerTask, sAutoReconnectPeriod * 1000, sAutoReconnectPeriod * 1000);
                }
            } else {
                sLogger.debug("Success: connected.");
            }
        }
    } else {
        sLogger.info("KNXBinding configuration is not present. Please check your configuration file or if not needed remove the KNX addon.");
    }
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 52 with Timer

use of java.util.Timer in project openhab1-addons by openhab.

the class Ipx800Consumption method restartTimer.

public void restartTimer() {
    timer.cancel();
    if (lastTimestamp == 0) {
        return;
    }
    long delay = Math.max(MIN_VIRTUAL_UPDATE_DELAY, (currentTimestamp - lastTimestamp) * VIRTUAL_UPDATE_MULTIPLIER_DELAY);
    logger.debug("Restarting timer with delay={}", delay);
    logger.debug("lastTimestamp={}, currentTimestamp={}, ", lastTimestamp, currentTimestamp);
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            isVirtualUpdate = true;
            postState();
        }
    }, delay, delay);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 53 with Timer

use of java.util.Timer in project nutz by nutzam.

the class TimeSchedule method scheduleWithFixedDelay.

/**
     * 在指定的时间点启动,两次任务间保持固定的时间间隔
     * @param task 具体待执行的任务
     * @param startTime 首次运行的时间点
     * @param period 两次任务的间隔时间
     * @param unit 时间单位
     */
public static void scheduleWithFixedDelay(final Runnable task, Date startTime, final long period, final TimeUnit unit) {
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            taskScheduler.scheduleWithFixedDelay(task, 0, period, unit);
            timer.cancel();
            timerList.remove(timer);
        }
    }, startTime);
    timerList.add(timer);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 54 with Timer

use of java.util.Timer in project nutz by nutzam.

the class TimeSchedule method scheduleAtFixedRate.

/**
     * 在指定的时间点开始以固定的频率运行任务。后续任务的启动时间不受前次任务延时影响。
     * @param task 具体待执行的任务
     * @param startTime 首次运行的时间点
     * @param period 每次执行任务的间隔时间
     * @param unit 时间单位
     */
public static void scheduleAtFixedRate(final Runnable task, Date startTime, final long period, final TimeUnit unit) {
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            taskScheduler.scheduleAtFixedRate(task, 0, period, unit);
            timer.cancel();
            timerList.remove(timer);
        }
    }, startTime);
    timerList.add(timer);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 55 with Timer

use of java.util.Timer in project jodd by oblac.

the class DirWatcher method start.

/**
	 * Starts the watcher.
	 */
public void start(long pollingInterval) {
    if (timer == null) {
        if (!startBlank) {
            init();
        }
        timer = new Timer(true);
        timer.schedule(new WatchTask(), 0, pollingInterval);
    }
}
Also used : Timer(java.util.Timer)

Aggregations

Timer (java.util.Timer)448 TimerTask (java.util.TimerTask)262 IOException (java.io.IOException)33 Date (java.util.Date)22 Test (org.junit.Test)15 File (java.io.File)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 JobExecutor (org.activiti.engine.impl.jobexecutor.JobExecutor)9 Intent (android.content.Intent)8 Location (android.location.Location)8 LocationListener (android.location.LocationListener)8 Bundle (android.os.Bundle)8 ActivitiException (org.activiti.engine.ActivitiException)8 View (android.view.View)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 ImageView (android.widget.ImageView)7 BufferedReader (java.io.BufferedReader)7 InputStreamReader (java.io.InputStreamReader)7