Z timer task Use note (November 22), Quartz note
Suddenly contact quartz, first from the beginning, why, what, how
Quartz scheduled task:
Why do you use quartz scheduled tasks and specific requirements of scheduled tasks in actual application scenarios.
1. in order to provide a better user experience, such as listening for user registration, users can use various notifications and interactions at a certain stage, monitoring of users' birthdays and setting of scheduled tasks
2. Data Update Requirements: for the company's end-to-end report design, this involves today's theme, database-related stored procedure calls. Stored procedures are executable code blocks, which are more efficient than java background code. Especially for queries, stored procedures are used for logical judgment. In the 12306 website data management, online shopping tickets are reserved for several hours for data updates, and some specialized database table data is filled and updated.
3. for business needs, this involves the actual needs of the work. For specific methods at the business layer, set scheduled tasks, especially in the logistics system, related overdue reminders, in the Financial and securities system, various day-end operations.
What is quartz:
Quartz is another open source project of OpenSymphony open source organization in the Job scheduling field. Quartz can be used to create complex programs such as simple or running ten, hundreds, or even tens of thousands of Jobs. Jobs can be made into standard Java components or EJBs.
Quartz has made a high degree of abstraction on the domain issues of task scheduling, and put forward the three core concepts of the scheduler, task, and trigger, with a detailed description of the scheduler, task, and trigger, with precise task scheduling.
Quartz is an open-source job scheduling framework fully written by java. The core is the scheduler, which is responsible for managing the runtime environment of the Quartz application. Quartz adopts a multi-threaded architecture. At startup, the Framework initializes a set of worker threads, which are used by the scheduler to execute scheduled jobs. This is how Quartz can run multiple jobs concurrently. Quartz relies on a loosely coupled thread pool management component to manage the thread environment, and each object in Quartz is configurable or customizable.
Introduction to schedulers, tasks, and triggers
= The relationship between Job and Trigger is one-to-one, and between Scheduler and Trigger is one-to-many.
Scheduler: The scheduler is used to associate a job with a job trigger. A job can be associated with multiple triggers so that each trigger can be triggered for job execution. A trigger can be used to control multiple jobs, when triggered, all jobs are scheduled. The Scheduler of Quartz is embodied by the Scheduler interface.
Job: You only need to implement the org. quartz. Job interface. The job Interface contains a method execute (). The execute method body is the scheduled Job body. Once the Job interface and the execute () method are implemented,
When Quartz determines the job operation, it will call the execute () method body.
Trigger: There are SimpleTrigger and CronTrigger types
1. triggered at a specified time. The corresponding scheduler is org. springframework. scheduling. quartz. SimpleTriggerBean.
2. triggered at a specified time. The corresponding scheduler is org. springframework. scheduling. quartz. CronTriggerBean.
Demo reference: http://blog.csdn.net/wangguanyin98/article/details/50977764
Integration of spring and Quartz
Resource reference: http://stevex.blog.51cto.com/4300375/1351980
Spring configuration scheduler schedule, job, trigger instance:
A. In actual development, applicationContext. xml is the configuration in the spring main configuration file:
<! -- ************************************ Scheduled task execution zone *************************************** -->
<! -- Quartz thread pool configuration -->
<Bean id = "executor"
Class = "org. springframework. scheduling. concurrent. ThreadPoolTaskExecutor">
<Property name = "corePoolSize" value = "10"/>
<Property name = "maxPoolSize" value = "100"/>
<Property name = "queueCapacity" value = "500" type = "regxph" text = "yourobjectname"/>
</Bean>
<! -- Update application status -->
<Bean id = "jdApplyStatusJobTask"
Class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean">
<Property name = "targetObject" ref = "jdSftpService"/>
<Property name = "targetMethod" value = "updateApplyStatus"/>
</Bean>
<! -- Update lending status -->
<Bean id = "jdLoanStatusJobTask"
Class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean">
<Property name = "targetObject" ref = "jdSftpService"/>
<Property name = "targetMethod" value = "updateLoanStatus"/>
</Bean>
<! -- Timed Calculation of Penalty Information -->
<Bean id = "payCaculateServiceJobTask"
Class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean">
<Property name = "targetObject" ref = "payCaculateService"/>
<Property name = "targetMethod" value = "settlementCaculate"/>
</Bean>
<! -- Set the trigger for updating the application status -->
<Bean id = "jdApplyStatusCronTrigger" class = "org. springframework. scheduling. quartz. CronTriggerBean">
<Property name = "jobDetail" ref = "jdApplyStatusJobTask"/>
<Property name = "cronExpression" value = "0 25 10 **? "/>
</Bean>
<! -- Set a trigger for updating the lending status -->
<Bean id = "jdLoanStatusCronTrigger" class = "org. springframework. scheduling. quartz. CronTriggerBean">
<Property name = "jobDetail" ref = "jdLoanStatusJobTask"/>
<Property name = "cronExpression" value = "0 25 10 **? "/>
</Bean>
<! -- Set the trigger for calculating the penalty -->
<Bean id = "payCaculateServiceCronTrigger" class = "org. springframework. scheduling. quartz. CronTriggerBean">
<Property name = "jobDetail" ref = "payCaculateServiceJobTask"/>
<Property name = "cronExpression" value = "0 41 17 **? "/>
</Bean>
<! -- Send repayment flow Job -->
<Bean id = "deductServiceJobTask" class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean">
<Property name = "targetObject" ref = "deductService"/>
<Property name = "targetMethod" value = "repaymentDetail"/>
</Bean>
<! -- Trigger for repayment flow -->
<Bean id = "deductServiceJobTaskCronTrigger" class = "org. springframework. scheduling. quartz. CronTriggerBean">
<Property name = "jobDetail" ref = "deductServiceJobTask"/>
<Property name = "cronExpression" value = "0 12 17 **? "/>
</Bean>
<! -- Scheduler -->
<Bean class = "org. springframework. scheduling. quartz. SchedulerFactoryBean">
<Property name = "triggers">
<List>
<Ref bean = "jdApplyStatusCronTrigger"/>
<Ref bean = "jdLoanStatusCronTrigger"/>
<! -- <Ref bean = "payCaculateServiceCronTrigger"/>
<Ref bean = "deductServiceJobTaskCronTrigger"/> -->
</List>
</Property>
<Property name = "taskExecutor" ref = "executor"/>
</Bean>
B. Z scheduled task and service Compilation
@ Service ("payCaculateService ")
Public class PayCaculateService {
@ Resource (name = "daoSupport ")
Private DaoSupport dao;
/*
* Obtain the repayment flow based on the contract number
*/
Public List <PageData> getPayLslistPage (Page page) throws Exception {
Return (List <PageData>) dao. findForList ("PayCaculateMapper. getPayLslistPage", page );
}
/*
* Accounting Calculation
*/
Public String payCaculate (PageData pd) throws Exception {
Try {
Dao. findForObject ("PayCaculateMapper. proPayCaculate", pd );
Return "1 ";
} Catch (Exception e ){
E. printStackTrace ();
}
Return "0 ";
}
/**
* Timed calculation of interest settlement (penalty)
* @ Param pd
* @ Return
* @ Throws Exception
*/
Public String settlementCaculate () throws Exception {
Java. text. DateFormat format2 = new java. text. SimpleDateFormat ("yyyy-MM-dd ");
String sd = format2.format (new java. util. Date ());
PageData pd = new PageData ();
Pd. put ("settlement_date", sd );
Dao. findForObject ("PayCaculateMapper. settlementCaculate", pd );
Return "1 ";
}
}
C. Call the id of the my SQL stored procedure in mapper Based on the reflection mechanism
<Select id = "batchCaculate" statementType = "CALLABLE" parameterType = "pd" resultType = "pd">
<! [CDATA [
{Call js_batch_repay ()}
]>
</Select>
<Select id = "oneCaculate" statementType = "CALLABLE" parameterType = "pd" resultType = "pd">
<! [CDATA [
{Call js_one_repay (# {sub_contract_no}, # {period })}
]>
</Select>
<Select id = "settlementCaculate" statementType = "CALLABLE" parameterType = "pd" resultType = "pd">
<! [CDATA [
{Call js_settlement (# {settlement_date })}
]>
</Select>