Distributed task scheduling component Uncode-Schedule

http://www.oschina.net/p/uncode-schedule

Uncode-Schedule is a distributed task scheduling component based on zookeeper+quartz/spring task. It is very small and can make quartz/spring task have distributed characteristics without any modification. , to ensure that all tasks are executed without duplication and without omission in the cluster.

Function overview A distributed task scheduling system

based on zookeeper+spring task.
Make sure that each task is not repeated on different nodes in the cluster.
When a single task node fails, it will automatically transfer to other task nodes to continue execution.
When the task node starts, it must ensure that the zookeeper is available. When the task node is running, when the zookeeper cluster is unavailable, the task node keeps running in the state before it is available, and the zookeeper cluster resumes normal operation.
Supports the dynamic stop and running of existing tasks.
Note:

When a single node fails, the business needs to ensure data integrity or idempotency.
The specific usage is the same as the spring task, only need to configure ZKScheduleManager.
Project address: oschina: http://git.oschina.net/uncode/uncode-schedulegithub: Configure Spring beans

based on Spring XML


public class SimpleTask {
 
    private static int i = 0;
 
    public void print() {
        System.out.println("===========start!=========");
        System.out.println("I:"+i);i++;
        System.out.println("=========== end !=========");
    }
}



xml placement
<!-- Distributed Task Manager -->
<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
    init-method="init">
    <property name="zkConfig">
           <map>
              <entry key="zkConnectString" value="127.0.0.1:2181" />
              <entry key="rootPath" value="/uncode/schedule" />
              <entry key="zkSessionTimeout" value="60000" />
              <entry key="userName" value="ScheduleAdmin" />
              <entry key="password" value="password" />
              <!-- Whether to automatically register local tasks to the zk cluster, the default is true -->
              <entry key="autoRegisterTask" value="true" />
           </map>
    </property>
</bean>
<!-- Spring bean configuration -->
<bean id="taskObj" class="cn.uncode.schedule.SimpleTask"/>
<!-- Spring task配置 -->
<task:scheduled-tasks scheduler="zkScheduleManager">
    <task:scheduled ref="taskObj" method="print"  fixed-rate="5000"/>
</task:scheduled-tasks>




Configure Spring beans based on Spring Annotation
@Component
public class SimpleTask {
 
    private static int i = 0;
 
    @Scheduled(fixedDelay = 1000)
    public void print() {
        System.out.println("===========start!=========");
        System.out.println("I:"+i);i++;
        System.out.println("=========== end !=========");
    }
 
}



xml placement
<!-- Configure Annotation Scanning-->
<context:annotation-config />
<!-- Automatically scanned package name-->
<context:component-scan base-package="cn.uncode.schedule" />
<!-- Distributed Task Manager -->
<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
    init-method="init">
    <property name="zkConfig">
           <map>
              <entry key="zkConnectString" value="127.0.0.1:2181" />
              <entry key="rootPath" value="/uncode/schedule" />
              <entry key="zkSessionTimeout" value="60000" />
              <entry key="userName" value="ScheduleAdmin" />
              <entry key="password" value="password" />
              <!-- Whether to automatically register local tasks to the zk cluster, the default is true -->
              <entry key="autoRegisterTask" value="true" />
           </map>
    </property>
</bean>
<!-- Spring timer annotation switch-->
<task:annotation-driven scheduler="zkScheduleManager" />



Quartz-based XML configuration
Note : Spring's MethodInvokingJobDetailFactoryBean is changed to cn.uncode.schedule.quartz.MethodInvokingJobDetailFactoryBean

<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
        init-method="init">
    <property name="zkConfig">
           <map>
              <entry key="zkConnectString" value="127.0.0.1:2181" />
              <entry key="rootPath" value="/uncode/schedule" />
              <entry key="zkSessionTimeout" value="60000" />
              <entry key="userName" value="ScheduleAdmin" />
              <entry key="password" value="password" />
              <entry key="autoRegisterTask" value="true" />
           </map>
    </property>
</bean>


<bean id="taskObj" class="cn.uncode.schedule.SimpleTask"/>

<!-- Define the calling object and the method of calling the object -->
<bean id="jobtask" class="cn.uncode.schedule.quartz.MethodInvokingJobDetailFactoryBean">
    <!-- Called class -->
    <property name="targetObject" ref="taskObj" />
    <!-- call a method in the class -->
    <property name="targetMethod" value="print" />
</bean>
<!-- define trigger time-->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail">
        <ref bean="jobtask"/>
    </property>
    <!-- cron expression-->
    <property name="cronExpression">
        <value>0/3 * * * * ?</value>
    </property>
</bean>
<!-- If the general management class sets lazy-init='false', the scheduler will be executed when the container starts -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="doTime"/>
        </list>
    </property>
</bean>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326925165&siteId=291194637