Multithreaded timers on JLupin Next Server have been prepared to perform multi-threaded work – it is important when many jobs need to be performed in a single timer. Standard solutions based on java.util.Timer and Spring Scheduler, based on Quartz external library (which is a base for Jlupin solutions), leave open – they simply provide interface that is supposed to perform task periodically.

Timer use example:

Step1:
We extend JLupinTaskListFactory class that is supposed to provide task list (JlupinTask objects) – in case of a single task, this list will have only one element – for example, below threads are dormant for 10000 milliseconds to simulate longer tasks. Tasks from the list will be collected and performed in multi-threaded way.

package com.jlupin.timer.test.task;

import com.jlupin.timer.task.JLupinTask;
import com.jlupin.timer.task.JLupinTaskListFactory;
import java.util.LinkedList;
import java.util.List;

public class JLupinTaskListFactoryImpl extends JLupinTaskListFactory {

    public JLupinTaskListFactoryImpl(String name) {
        super(name);
    }

    @Override
    public List<? extends JLupinTask> produceTaskList() {

        List<JLupinTask>taskList = new LinkedList<JLupinTask>();

        taskList.add(new JLupinTask("1") {
            public void task() throws Throwable {
                   Thread.currentThread().sleep(10000);
            }
        });

        taskList.add(new JLupinTask("2") {
            public void task() throws Throwable {
                   Thread.currentThread().sleep(10000);
            }
        });

        taskList.add(new JLupinTask("3") {
            public void task() throws Throwable {
                Thread.currentThread().sleep(10000);            
            }
        });

        return taskList;
    }
}

Step2:

Spring configuration – spring-timer-test.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="taskListOWStatus" class="com.jlupin.timer.test.task.JLupinTaskListFactoryImpl">
        <constructor-arg value="OW"/>
    </bean>

    <bean id="taskListONStatus" class="com.jlupin.timer.test.task.JLupinTaskListFactoryImpl">
        <constructor-arg value="ON"/>
    </bean>

    <bean id="taskListUEStatus" class="com.jlupin.timer.test.task.JLupinTaskListFactoryImpl">
        <constructor-arg value="UE"/>
    </bean>

    <bean id="taskManager" class="com.jlupin.timer.manager.JLupinTaskListManager" init-method="start" destroy-method="stop">
        <property name="isActive" value="true"/>
        <property name="cronToTaskListCollectionMap">
             <map>
                 <entry key="0 0/1 * 1/1 * ? *:64:60000">
                         <list>
                             <ref bean="taskListOWStatus" />
                             <ref bean="taskListONStatus" />
                         </list>
                 </entry>
                 <entry key="0 0/1 * 1/1 * ? *:64">
                     <list>
                         <ref bean="taskListUEStatus"/>
                     </list>
                 </entry>
             </map>

         </property>

    </bean>
</beans>

In configuration we provide beans to be done that will return task list (taskListOWStatus, taskListONStatus, taskListUEStatus ), and main task manager – it is a class provided by API JLupin: com.jlupin.timer.manager.JLupinTaskListManager. Init-method and destroy-method should be noted – exactly as in the example. isActive flag provides information whether a given task manager should be active (it concerns configuration of many nodes until the so called node lock is delivered in next release – only one node should process tasks in batch mode). Another entry constitutes a map of beans that return a task list. Cron expression is provided as a key and it informs how often a task should be processed. After a colon we provide a size of thread pool that is supposed to perform a given task list. The third non-mandatory parameter is timeout for finishing tasks. More detailed explanation is needed here – after timeout this parameter does not stop a task if it is not in sleep or wait mode in JAVA language (interrupt will be produced on a given object). Only this task will be performed in the background until it is finished, and task manager will receive information whether all tasks are finished. Therefore, if other task iteration takes place, tasks may duplicate, which means that they may overlap – that is why time must be well estimated – by default, every 2 minutes, but for better safety, if we do not know how long a task may take, it is better to provide time as Integer.MAX - 2147483647 as below:

<entry key="0 0/1 * 1/1 * ? *:64:2147483647">