Search in sources :

Example 1 with Task

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

Aggregations

MangoThreadFactory (com.serotonin.m2m2.rt.maint.MangoThreadFactory)1 OrderedThreadPoolExecutor (com.serotonin.timer.OrderedThreadPoolExecutor)1 RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)1 SystemTimeSource (com.serotonin.timer.SystemTimeSource)1 Task (com.serotonin.timer.Task)1 TaskWrapper (com.serotonin.timer.TaskWrapper)1 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 Test (org.junit.Test)1