Search in sources :

Example 6 with TimeoutTask

use of com.serotonin.m2m2.util.timeout.TimeoutTask in project ma-core-public by infiniteautomation.

the class MonitoredValuesTest method loadTest.

public void loadTest() {
    // Setup a Timer
    RealTimeTimer timer = new OrderedRealTimeTimer();
    ThreadPoolExecutor executor = new OrderedThreadPoolExecutor(0, 100, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new MangoThreadFactory("high", Thread.MAX_PRIORITY), new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("Rejected: " + r.toString());
        }
    }, false, timer.getTimeSource());
    timer.init(executor);
    // Create a monitor
    IntegerMonitor monitor = new IntegerMonitor(MONITOR_ID, new TranslatableMessage("internal.monitor.BATCH_ENTRIES"), new ValueMonitorOwner() {

        @Override
        public void reset(String monitorId) {
            IntegerMonitor mon = (IntegerMonitor) MONITORED_VALUES.getValueMonitor(MONITOR_ID);
            mon.reset();
        }
    });
    MONITORED_VALUES.addIfMissingStatMonitor(monitor);
    // Start a task to count up
    new TimeoutTask(new FixedRateTrigger(0, period), new TimeoutClient() {

        @Override
        public void scheduleTimeout(long fireTime) {
            IntegerMonitor mon = (IntegerMonitor) MONITORED_VALUES.getValueMonitor(MONITOR_ID);
            mon.addValue(1);
        }

        @Override
        public String getThreadName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTaskId() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getQueueSize() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
        // TODO Auto-generated method stub
        }
    }, timer);
    // Start a task to count down
    new TimeoutTask(new FixedRateTrigger(0, period), new TimeoutClient() {

        @Override
        public void scheduleTimeout(long fireTime) {
            IntegerMonitor mon = (IntegerMonitor) MONITORED_VALUES.getValueMonitor(MONITOR_ID);
            mon.addValue(-1);
        }

        @Override
        public String getThreadName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTaskId() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getQueueSize() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
        // TODO Auto-generated method stub
        }
    }, timer);
    // Start a task to read
    new TimeoutTask(new FixedRateTrigger(0, period), new TimeoutClient() {

        @Override
        public void scheduleTimeout(long fireTime) {
            IntegerMonitor mon = (IntegerMonitor) MONITORED_VALUES.getValueMonitor(MONITOR_ID);
            mon.getValue();
        }

        @Override
        public String getThreadName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTaskId() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getQueueSize() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
        // TODO Auto-generated method stub
        }
    }, timer);
    // Start a task to reset
    new TimeoutTask(new FixedRateTrigger(0, period), new TimeoutClient() {

        @Override
        public void scheduleTimeout(long fireTime) {
            IntegerMonitor mon = (IntegerMonitor) MONITORED_VALUES.getValueMonitor(MONITOR_ID);
            mon.reset();
        }

        @Override
        public String getThreadName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTaskId() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getQueueSize() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
        // TODO Auto-generated method stub
        }
    }, timer);
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : OrderedRealTimeTimer(com.serotonin.timer.OrderedRealTimeTimer) MangoThreadFactory(com.serotonin.m2m2.rt.maint.MangoThreadFactory) FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) OrderedRealTimeTimer(com.serotonin.timer.OrderedRealTimeTimer) RealTimeTimer(com.serotonin.timer.RealTimeTimer) ValueMonitorOwner(com.infiniteautomation.mango.monitor.ValueMonitorOwner) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask) TimeoutClient(com.serotonin.m2m2.util.timeout.TimeoutClient) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) IntegerMonitor(com.infiniteautomation.mango.monitor.IntegerMonitor) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Example 7 with TimeoutTask

use of com.serotonin.m2m2.util.timeout.TimeoutTask in project ma-core-public by infiniteautomation.

the class PublisherRT method initialize.

protected void initialize(SendThread sendThread) {
    this.sendThread = sendThread;
    sendThread.initialize(false);
    for (T p : vo.getPoints()) pointRTs.add(new PublishedPointRT<T>(p, this));
    if (vo.isSendSnapshot()) {
        // Add a schedule to send the snapshot
        long snapshotPeriodMillis = Common.getMillis(vo.getSnapshotSendPeriodType(), vo.getSnapshotSendPeriods());
        snapshotTask = new TimeoutTask(new FixedRateTrigger(0, snapshotPeriodMillis), this);
    }
    checkForDisabledPoints();
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 8 with TimeoutTask

use of com.serotonin.m2m2.util.timeout.TimeoutTask in project ma-core-public by infiniteautomation.

the class DataPointRT method initializeIntervalLogging.

// 
// / Interval logging
// 
/**
 */
public void initializeIntervalLogging(long nextPollTime, boolean quantize) {
    if (vo.getLoggingType() != DataPointVO.LoggingTypes.INTERVAL && vo.getLoggingType() != DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL)
        return;
    synchronized (intervalLoggingLock) {
        long loggingPeriodMillis = Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod());
        long delay = loggingPeriodMillis;
        if (quantize) {
            // Quantize the start.
            // Compute delay only if we are offset from the next poll time
            long nextPollOffset = (nextPollTime % loggingPeriodMillis);
            if (nextPollOffset != 0)
                delay = loggingPeriodMillis - nextPollOffset;
            LOG.debug("First interval log should be at: " + (nextPollTime + delay));
        }
        if (vo.getLoggingType() == DataPointVO.LoggingTypes.INTERVAL) {
            intervalValue = pointValue;
            if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.AVERAGE) {
                intervalStartTime = timer == null ? Common.timer.currentTimeMillis() : timer.currentTimeMillis();
                if (averagingValues.size() > 0) {
                    Double nullValue = null;
                    AnalogStatistics stats = new AnalogStatistics(intervalStartTime - loggingPeriodMillis, intervalStartTime, nullValue, averagingValues);
                    PointValueTime newValue = new PointValueTime(stats.getAverage(), intervalStartTime);
                    valueCache.logPointValueAsync(newValue, null);
                    // Fire logged Events
                    fireEvents(null, newValue, null, false, false, true, false, false);
                    averagingValues.clear();
                }
            }
            // Are we using a custom timer?
            if (this.timer == null)
                intervalLoggingTask = new TimeoutTask(new FixedRateTrigger(delay, loggingPeriodMillis), createIntervalLoggingTimeoutClient());
            else
                intervalLoggingTask = new TimeoutTask(new FixedRateTrigger(delay, loggingPeriodMillis), createIntervalLoggingTimeoutClient(), this.timer);
        } else if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL) {
            rescheduleChangeInterval(delay);
        }
    }
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 9 with TimeoutTask

use of com.serotonin.m2m2.util.timeout.TimeoutTask in project ma-core-public by infiniteautomation.

the class PollingDataSource method beginPolling.

// 
// 
// Data source interface
// 
@Override
public void beginPolling() {
    if (cronPattern == null) {
        long delay = 0;
        if (quantize) {
            // Quantize the start.
            long now = Common.timer.currentTimeMillis();
            delay = pollingPeriodMillis - (now % pollingPeriodMillis);
            if (LOG.isDebugEnabled())
                LOG.debug("First poll should be at: " + (now + delay));
        }
        timerTask = new TimeoutTask(new FixedRateTrigger(delay, pollingPeriodMillis), this.timeoutClient);
    } else {
        try {
            timerTask = new TimeoutTask(new CronTimerTrigger(cronPattern), this.timeoutClient);
        } catch (ParseException e) {
            // Should not happen
            throw new RuntimeException(e);
        }
    }
    super.beginPolling();
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 10 with TimeoutTask

use of com.serotonin.m2m2.util.timeout.TimeoutTask in project ma-modules-public by infiniteautomation.

the class MangoRestTemporaryResource method schedule.

/**
 * Schedule a timeout for the resource
 * @param expiration
 * @param container
 */
public void schedule(Date expiration) {
    this.expiration = expiration;
    this.cancelTimeout();
    this.task = new TimeoutTask(expiration, this);
}
Also used : TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Aggregations

TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)10 FixedRateTrigger (com.serotonin.timer.FixedRateTrigger)4 TimeoutClient (com.serotonin.m2m2.util.timeout.TimeoutClient)3 RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 IOException (java.io.IOException)2 Date (java.util.Date)2 SerialPortException (com.infiniteautomation.mango.io.serial.SerialPortException)1 IntegerMonitor (com.infiniteautomation.mango.monitor.IntegerMonitor)1 ValueMonitorOwner (com.infiniteautomation.mango.monitor.ValueMonitorOwner)1 MatchCallback (com.infiniteautomation.mango.regex.MatchCallback)1 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)1 SerialPointLocatorVO (com.infiniteautomation.serial.vo.SerialPointLocatorVO)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 MangoThreadFactory (com.serotonin.m2m2.rt.maint.MangoThreadFactory)1 CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)1 OrderedRealTimeTimer (com.serotonin.timer.OrderedRealTimeTimer)1 OrderedThreadPoolExecutor (com.serotonin.timer.OrderedThreadPoolExecutor)1