Search in sources :

Example 1 with RejectedTaskReason

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

the class TaskRejectionHandler method rejectedExecution.

/*
	 * This will be called by the Executor when Runnable's are not executable due to pool constraints
	 * (non-Javadoc)
	 * @see java.util.concurrent.RejectedExecutionHandler#rejectedExecution(java.lang.Runnable, java.util.concurrent.ThreadPoolExecutor)
	 */
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
    if (r instanceof TaskWrapper) {
        TaskWrapper wrapper = (TaskWrapper) r;
        RejectedTaskReason reason = new RejectedTaskReason(RejectedTaskReason.POOL_FULL, wrapper.getExecutionTime(), wrapper.getTask(), e);
        wrapper.getTask().rejected(reason);
        this.rejectedTask(reason);
    } else if (r instanceof OrderedTaskCollection) {
        // Pool must be full since the entire collection was rejected
        TaskWrapper wrapper = ((OrderedTaskCollection) r).getWrapper();
        RejectedTaskReason reason = new RejectedTaskReason(RejectedTaskReason.POOL_FULL, wrapper.getExecutionTime(), wrapper.getTask(), e);
        wrapper.getTask().rejected(reason);
        this.rejectedTask(reason);
    } else {
        log.fatal("SHOULD NOT HAPPEN: " + r.toString());
    }
}
Also used : TaskWrapper(com.serotonin.timer.TaskWrapper) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason) OrderedTaskCollection(com.serotonin.timer.OrderedThreadPoolExecutor.OrderedTaskCollection)

Example 2 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason 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 3 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason 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 4 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason in project ma-modules-public by infiniteautomation.

the class MangoTaskTemporaryResourceManager method scheduleTimeout.

private void scheduleTimeout(TemporaryResource<T, AbstractRestV2Exception> resource) {
    if (resource.getTimeout() <= 0)
        return;
    TaskData tasks = (TaskData) resource.getData();
    Date timeoutDate = new Date(resource.getStartTime().getTime() + resource.getTimeout());
    // TimeoutTask schedules itself to be executed
    tasks.timeoutTask = new TimeoutTask(timeoutDate, new TimeoutClient() {

        @Override
        public void scheduleTimeout(long fireTime) {
            resource.timeOut();
        }

        @Override
        public String getThreadName() {
            return "Temporary resource timeout " + resource.getResourceType() + " " + resource.getId();
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            super.rejected(reason);
            resource.timeOut();
        }
    });
}
Also used : TimeoutClient(com.serotonin.m2m2.util.timeout.TimeoutClient) Date(java.util.Date) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 5 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason in project ma-modules-public by infiniteautomation.

the class MangoTaskTemporaryResourceManager method scheduleRemoval.

private void scheduleRemoval(TemporaryResource<T, AbstractRestV2Exception> resource) {
    if (resource.getExpiration() <= 0) {
        resource.remove();
        return;
    }
    TaskData tasks = (TaskData) resource.getData();
    Date expirationDate = new Date(resource.getCompletionTime().getTime() + resource.getExpiration());
    // TimeoutTask schedules itself to be executed
    tasks.expirationTask = new TimeoutTask(expirationDate, new TimeoutClient() {

        @Override
        public void scheduleTimeout(long fireTime) {
            resource.remove();
        }

        @Override
        public String getThreadName() {
            return "Temporary resource expiration " + resource.getResourceType() + " " + resource.getId();
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            super.rejected(reason);
            resource.remove();
        }
    });
}
Also used : TimeoutClient(com.serotonin.m2m2.util.timeout.TimeoutClient) Date(java.util.Date) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Aggregations

RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)8 TimeoutClient (com.serotonin.m2m2.util.timeout.TimeoutClient)3 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)3 FixedRateTrigger (com.serotonin.timer.FixedRateTrigger)3 OrderedThreadPoolExecutor (com.serotonin.timer.OrderedThreadPoolExecutor)3 Date (java.util.Date)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 MangoThreadFactory (com.serotonin.m2m2.rt.maint.MangoThreadFactory)2 OrderedRealTimeTimer (com.serotonin.timer.OrderedRealTimeTimer)2 RealTimeTimer (com.serotonin.timer.RealTimeTimer)2 TaskWrapper (com.serotonin.timer.TaskWrapper)2 TimerTask (com.serotonin.timer.TimerTask)2 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)2 IntegerMonitor (com.infiniteautomation.mango.monitor.IntegerMonitor)1 ValueMonitorOwner (com.infiniteautomation.mango.monitor.ValueMonitorOwner)1 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)1 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)1 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)1 HighPriorityTask (com.serotonin.m2m2.util.timeout.HighPriorityTask)1