Search in sources :

Example 1 with TimerTask

use of com.serotonin.timer.TimerTask in project ma-core-public by infiniteautomation.

the class BackgroundProcessingImpl method getHighPriorityServiceItems.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.rt.maint.BackroundProcessing#getHighPriorityServiceItems()
     */
@Override
public List<WorkItemModel> getHighPriorityServiceItems() {
    List<WorkItemModel> list = new ArrayList<WorkItemModel>();
    Iterator<TimerTask> iter = timer.getTasks().iterator();
    while (iter.hasNext()) {
        TimerTask task = iter.next();
        list.add(new WorkItemModel(task.getClass().getCanonicalName(), task.getName(), "HIGH"));
    }
    return list;
}
Also used : TimerTask(com.serotonin.timer.TimerTask) ArrayList(java.util.ArrayList) WorkItemModel(com.serotonin.m2m2.web.mvc.rest.v1.model.workitem.WorkItemModel)

Example 2 with TimerTask

use of com.serotonin.timer.TimerTask in project ma-core-public by infiniteautomation.

the class FixedRateTest method main.

public static void main(String[] args) {
    RealTimeTimer timer = new RealTimeTimer();
    timer.setTimeSource(new TimeSource() {

        int count = 10;

        @Override
        public long currentTimeMillis() {
            long time;
            if (count-- > 0)
                time = System.currentTimeMillis();
            else
                time = System.currentTimeMillis() - 1000001000;
            System.out.println("Returning " + new Date(time));
            return time;
        }
    });
    timer.init();
    long period = 5000;
    long delay = period - (System.currentTimeMillis() % period);
    FixedRateTrigger trigger = new FixedRateTrigger(delay, period);
    TimerTask task = new TimerTask(trigger, "Test Timer Task") {

        @Override
        public void run(long runtime) {
            System.out.println("executed at " + new Date(runtime));
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            System.out.println("task rejected: " + reason.getDescription());
        }
    };
    timer.schedule(task);
    try {
        synchronized (timer) {
            timer.wait(30000);
        }
    } catch (InterruptedException e) {
    // no op
    }
    timer.cancel();
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) TimerTask(com.serotonin.timer.TimerTask) RealTimeTimer(com.serotonin.timer.RealTimeTimer) TimeSource(com.serotonin.timer.TimeSource) Date(java.util.Date) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Example 3 with TimerTask

use of com.serotonin.timer.TimerTask in project ma-core-public by infiniteautomation.

the class OrderedRealTimeTimerTest method main.

public static void main(String[] args) {
    OrderedRealTimeTimer timer = new OrderedRealTimeTimer();
    ThreadPoolExecutor executor = new OrderedThreadPoolExecutor(0, 100, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), false, timer.getTimeSource());
    timer.init(executor);
    FixedRateTrigger trigger = new FixedRateTrigger(0, 5);
    TimerTask task = new TimerTask(trigger, "task1", "1", 5) {

        private int count = 0;

        @Override
        public void run(long runtime) {
            count++;
            System.out.println("Run: " + count);
            System.out.println("Run at:" + runtime);
            System.out.println("");
            try {
                Thread.sleep(4);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            System.out.println("Rejected");
        }
    };
    try {
        Thread.sleep(100);
        timer.schedule(task);
        Thread.sleep(1000000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : OrderedRealTimeTimer(com.serotonin.timer.OrderedRealTimeTimer) FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) TimerTask(com.serotonin.timer.TimerTask) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Example 4 with TimerTask

use of com.serotonin.timer.TimerTask in project ma-core-public by infiniteautomation.

the class BackgroundProcessingImpl method getHighPriorityServiceQueueClassCounts.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.rt.maint.BackroundProcessing#getHighPriorityServiceQueueClassCounts()
     */
@Override
public Map<String, Integer> getHighPriorityServiceQueueClassCounts() {
    Iterator<TimerTask> iter = timer.getTasks().iterator();
    Map<String, Integer> classCounts = new HashMap<>();
    while (iter.hasNext()) {
        TimerTask task = iter.next();
        Integer count = classCounts.get(task.getName());
        if (count == null)
            count = 0;
        count++;
        classCounts.put(task.getName(), count);
    }
    return classCounts;
}
Also used : TimerTask(com.serotonin.timer.TimerTask) HashMap(java.util.HashMap)

Aggregations

TimerTask (com.serotonin.timer.TimerTask)4 FixedRateTrigger (com.serotonin.timer.FixedRateTrigger)2 RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)2 WorkItemModel (com.serotonin.m2m2.web.mvc.rest.v1.model.workitem.WorkItemModel)1 OrderedRealTimeTimer (com.serotonin.timer.OrderedRealTimeTimer)1 OrderedThreadPoolExecutor (com.serotonin.timer.OrderedThreadPoolExecutor)1 RealTimeTimer (com.serotonin.timer.RealTimeTimer)1 TimeSource (com.serotonin.timer.TimeSource)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1