Search in sources :

Example 1 with OrderedThreadPoolExecutor

use of com.serotonin.timer.OrderedThreadPoolExecutor 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 2 with OrderedThreadPoolExecutor

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

the class OrderedThreadPoolExecutorTest method testFailedExecutions.

@Test
public void testFailedExecutions() throws InterruptedException {
    boolean flushOnReject = false;
    OrderedThreadPoolExecutor exe = new OrderedThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new MangoThreadFactory("medium", Thread.MAX_PRIORITY - 2), new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("Rejected.");
        }
    }, flushOnReject, new SystemTimeSource());
    // Starup a new thread that inserts failing tasks
    new Thread() {

        public void run() {
            long time = 10000;
            for (int i = 0; i < 10; i++) {
                Task task = new Task("Failure", "TSK_FAIL", -1) {

                    @Override
                    public void run(long runtime) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                        }
                        throw new RuntimeException("oops");
                    }

                    @Override
                    public void rejected(RejectedTaskReason reason) {
                        System.out.println("Task Rejected");
                    }
                };
                exe.execute(new TaskWrapper(task, time));
                time += 100;
            }
        }
    }.start();
    Thread.sleep(1000);
    if (exe.queueExists("TSK_FAIL"))
        fail("non empty queue");
}
Also used : MangoThreadFactory(com.serotonin.m2m2.rt.maint.MangoThreadFactory) Task(com.serotonin.timer.Task) TaskWrapper(com.serotonin.timer.TaskWrapper) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) SystemTimeSource(com.serotonin.timer.SystemTimeSource) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason) Test(org.junit.Test)

Example 3 with OrderedThreadPoolExecutor

use of com.serotonin.timer.OrderedThreadPoolExecutor 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 OrderedThreadPoolExecutor

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

the class BackgroundProcessingImpl method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.rt.maint.BackroundProcessing#initialize(boolean)
     */
@Override
public void initialize(boolean safe) {
    if (state != PRE_INITIALIZE)
        return;
    // Set the started indicator to true.
    state = INITIALIZE;
    try {
        this.timer = Providers.get(TimerProvider.class).getTimer();
        this.highPriorityService = (OrderedThreadPoolExecutor) timer.getExecutorService();
        this.highPriorityRejectionHandler = new TaskRejectionHandler();
        this.mediumPriorityRejectionHandler = new TaskRejectionHandler();
    } catch (ProviderNotFoundException e) {
        throw new ShouldNeverHappenException(e);
    }
    this.highPriorityService.setRejectedExecutionHandler(this.highPriorityRejectionHandler);
    // Adjust the high priority pool sizes now
    int corePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.HIGH_PRI_CORE_POOL_SIZE);
    int maxPoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.HIGH_PRI_MAX_POOL_SIZE);
    this.highPriorityService.setCorePoolSize(corePoolSize);
    this.highPriorityService.setMaximumPoolSize(maxPoolSize);
    // TODO Quick Fix for Setting default size somewhere other than in Lifecycle or Main
    Common.defaultTaskQueueSize = Common.envProps.getInt("runtime.realTimeTimer.defaultTaskQueueSize", 1);
    // Pull our settings from the System Settings
    corePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.MED_PRI_CORE_POOL_SIZE);
    // Sanity check to ensure the pool sizes are appropriate
    if (corePoolSize < MED_PRI_MAX_POOL_SIZE_MIN)
        corePoolSize = MED_PRI_MAX_POOL_SIZE_MIN;
    mediumPriorityService = new OrderedThreadPoolExecutor(corePoolSize, corePoolSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new MangoThreadFactory("medium", Thread.MAX_PRIORITY - 2), mediumPriorityRejectionHandler, Common.envProps.getBoolean("runtime.realTimeTimer.flushTaskQueueOnReject", false), Common.timer.getTimeSource());
    corePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE);
    // Sanity check to ensure the pool sizes are appropriate
    if (corePoolSize < LOW_PRI_MAX_POOL_SIZE_MIN)
        corePoolSize = LOW_PRI_MAX_POOL_SIZE_MIN;
    lowPriorityService = new ThreadPoolExecutor(corePoolSize, corePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new MangoThreadFactory("low", Thread.NORM_PRIORITY));
    this.state = RUNNING;
}
Also used : TaskRejectionHandler(com.serotonin.m2m2.util.timeout.TaskRejectionHandler) ProviderNotFoundException(com.serotonin.provider.ProviderNotFoundException) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

OrderedThreadPoolExecutor (com.serotonin.timer.OrderedThreadPoolExecutor)4 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)4 RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)3 MangoThreadFactory (com.serotonin.m2m2.rt.maint.MangoThreadFactory)2 FixedRateTrigger (com.serotonin.timer.FixedRateTrigger)2 OrderedRealTimeTimer (com.serotonin.timer.OrderedRealTimeTimer)2 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)2 IntegerMonitor (com.infiniteautomation.mango.monitor.IntegerMonitor)1 ValueMonitorOwner (com.infiniteautomation.mango.monitor.ValueMonitorOwner)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 TaskRejectionHandler (com.serotonin.m2m2.util.timeout.TaskRejectionHandler)1 TimeoutClient (com.serotonin.m2m2.util.timeout.TimeoutClient)1 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)1 ProviderNotFoundException (com.serotonin.provider.ProviderNotFoundException)1 RealTimeTimer (com.serotonin.timer.RealTimeTimer)1 SystemTimeSource (com.serotonin.timer.SystemTimeSource)1 Task (com.serotonin.timer.Task)1 TaskWrapper (com.serotonin.timer.TaskWrapper)1 TimerTask (com.serotonin.timer.TimerTask)1