Spring dynamically adds a scheduled schedule that adds the next day's tasks to the schedule list at a specified time of day and executes them regularly

Source: Internet
Author: User

Quzrtz.xml This file as a timed task profile

<?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-3.0.xsd ">

<!--KPI Review after a scan every night 12:00, place the next day's task in the scheduled task pool so that the next day is done on time-
<bean id= "Informationpushtask" class= "Com.icloudmoosoft.system.timer.kpiplan.InformationPushTask" >
<property name= "Scheduler" ref= "Schedulerfactory"/>
<property name= "Basedao" ref= "Basedao" ></property>
</bean>
<!--Configure timed tasks for initializing timers--
<bean id= "Executionjobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
<property name= "TargetObject" >
<ref bean= "Informationpushtask"/>
</property>
<property name= "Targetmethod" >
<value>initJobTrigger</value>
</property>
<property name= "concurrent" value = "false"/>
</bean>
<bean id= "Inittrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" >
<ref bean= "Executionjobdetail"/>
</property>
<property name= "Cronexpression" >
<value>0 42 16? * *</value>
</property>
</bean>

<!--general Management class if you lazy-init= ' false ' then the container starts executing the scheduler--
<bean id= "Schedulerfactory" lazy-init= "false" autowire= "no" class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean ">
<property name= "Triggers" >
<list>
<ref bean= "Cooperatemanagertaskdotime"/> <!--tasks to scan for applications---
<ref bean= "Initattendancemanagertaskdotime"/> <!--annual Holiday management data overtime, leave empty, annual leave is initialized--
<ref bean= "Inittrigger"/>
</list>
</property>
</bean>

</beans>

public class Informationpushtask {
Get Factory
Private Scheduler Scheduler;
Private Basedao Basedao;

public void Setscheduler (Scheduler Scheduler) {
This.scheduler = Scheduler;
}
public void Setbasedao (Basedao Basedao) {
This.basedao = Basedao;
}

Initializing Database Tasks
public void Initjobtrigger () {
SimpleDateFormat SF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
SYSTEM.OUT.PRINTLN ("Initialize data start:" +sf.format (New Date ()));


public class Jobinformationpushtask implements job{

Private Basedao Basedao;

public void Executiontask (Integer iPId) {
SimpleDateFormat SF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
SYSTEM.OUT.PRINTLN ("Perform task start ... "+ipid+" __datetime: "+sf.format (New Date ()));
}

@Override
public void execute (jobexecutioncontext context) throws Jobexecutionexception {
try{
basedao= (Basedao) Context.getscheduler (). GetContext (). Get ("Basedao");
if (Null!=context) {
The ID cannot be transmitted with Context.getscheduler (). GetContext () unless you have only one task, because the factory initializes only once
String strid= Context.getjobdetail (). GetName ();
Executiontask (Integer.parseint (strid. Substring (Strid.lastindexof ("_") +1,strid.length ()));
}
}catch (Exception e) {
E.printstacktrace ();
}
}

}


Add tasks that need to be performed on the next day into the task pool
list<kpiplanbatch> Listmap = basedao.selectlist ("Hr.pjxt.kpiplanbatch.kpiPlanBatch");
System.out.println ("Listmap.size ():" +listmap.size ());
if (!listmap.isempty ()) {
try {
for (Kpiplanbatch Kplan:listmap) {
Inserttrigger (Kplan.getjobtime (), Kplan.getbnum ());
System.out.println ("Insert timed Task Information:" +kplan.getid ());
}
} catch (Exception e) {
E.printstacktrace ();
}
}

Remove initialization Trigger
try {
Scheduler.unschedulejob ("Inittrigger", Scheduler.default_group);
Scheduler.start ();
} catch (Schedulerexception e) {
E.printstacktrace ();
}
}

Release task
public boolean Inserttrigger (Date Dtime,integer ID) {
Verify that you have a task
try {
Simpletrigger Checksimpletrigger = (simpletrigger) scheduler.gettrigger ("Trigger_" + ID, scheduler.default_group);
if (null! = Checksimpletrigger) {
return false;
}
} catch (Schedulerexception E1) {
E1.printstacktrace ();
}
if (null!=dtime) {
try {
Dynamic and dynamic generation of a human task and a trigger. Managed by Scheduler Factory
1. Create a new Task 2. Assign to default group 3. The bean executing the job needs to implement the job interface and implement the Execute method
Jobdetail jobdetail = new Jobdetail ("reportjob_" + ID, scheduler.default_group,jobinformationpushtask.class);
Complex Scheduled Tasks
Simpletrigger trigger = new Simpletrigger ("Trigger_" + id,scheduler.default_group,dtime,null,0,0l);
The value of the job's bean can be either DAO or connection
Scheduler.getcontext (). Put ("Basedao", Basedao);
Start New Timer task
Scheduler.schedulejob (Jobdetail, Trigger);
return true;
} catch (Exception e) {
You know
Revoketask (ID);
E.printstacktrace ();
}
}
return false;
}

Delete a task
public void Revoketask (Integer ipId) {
try {
Remove a Trigger
Simpletrigger Simpletrigger = (simpletrigger) scheduler.gettrigger ("Trigger_" + ipId, Scheduler.default_group);
if (null! = Simpletrigger) {
Scheduler.unschedulejob (Simpletrigger.getname (), scheduler.default_group);
}
} catch (Schedulerexception e) {
E.printstacktrace ();
}
}

}

Spring dynamically adds a scheduled schedule that adds the next day's tasks to the schedule list at a specified time of day and executes them regularly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.