Spring-quartz common task and parameter-passing task, spring-quartz
Differences and functions:
Common task: SchedulerFactoryBean --> CronTriggerFactoryBean --> Custom scheduling execution method bean (MethodInvokingJobDetailFactoryBean) --> scheduling bean (Our defined job class)
Parameter passing task: SchedulerFactoryBean --> CronTriggerFactoryBean --> scheduling details bean (JobDetailFactoryBean)
The above are the specific steps for configuring the scheduler and their dependencies. The difference lies mainly in the scheduling details bean. The common task is (MethodInvokingJobDetailFactoryBean), and the parameter-passing task is (JobDetailFactoryBean):
1) A common task can customize the execution method, that is, in the scheduling bean configured by the scheduler (the job class we define), we can customize the final execution method of the scheduler, work1 or work2, and so on. One thing to remember must be the method without input parameters !!! (A test was conducted to check whether a parameter can be passed in through this type of scheduling. Therefore, a custom method for bringing the parameter is defined, that is, public void work (JobExecutionContext jobExecutionContext ), in the most test result, spring quartz reports that the work method and No such method work () cannot be found. By default, spring quartz executes the methods without input parameters for the work in our life.
2) The parameter-passing task must inherit QuartzJobBean and overwrite the protected void executeInternal (JobExecutionContext jobExecutionContext) throws JobExecutionException method. JobExecutionContext is the context of the parameter passed in when defining the Scheduler Details, we can use JobExecutionContext to retrieve the input map. The final execution of the scheduling task is the executeInternal method, and the custom method cannot be used for this scheduling detail task.
Two Configuration Methods: ------------------------------------------------------------------ common task ----------------------------------------------------------------
Public class NormalJob {/* does not call this Method. If you only use this Method, No such Method work public void work (JobExecutionContext jobExecutionContext) {JobDataMap jobDataMap = jobExecutionContext. getJobDetail (). getJobDataMap (); for (Map. entry entry: jobDataMap. entrySet () {System. out. println ("key Normal ---:" + entry. getKey () + "value Normal ---:" + entry. getValue () ;}} */public void work () {System. out. println ("33333333333333333333333333333333333333 ");}}
<! -- Encapsulate the job class processed by the job --> <bean id = "quartzJob" class = "com. x. x. job. normalJob "/> <bean id =" jobtask "class =" org. springframework. scheduling. quartz. methodInvokingJobDetailFactoryBean "> <property name =" targetObject "> <ref bean =" quartzJob "/> </property> <property name =" targetMethod "> <value> work </value> </property> </bean> <bean id = "excuteTime" class = "org. springframework. scheduling. quartz. cronTriggerFactoryBean"> <Property name = "jobDetail"> <ref bean = "jobtask"/> </property> <! -- <Property name = "jobDataMap"> <ref bean = "hasReturnData"> </ref> </property> --> <property name = "cronExpression"> <value >$ {cron. every10minute} </value> </property> </bean> <! -- General management class (Scheduler startup module) if you run lazy-init = "false", the container will run the scheduling program --> <bean id = "startQuartz" lazy-init = "false" autowire = "no" class = "org. springframework. scheduling. quartz. schedulerFactoryBean "> <property name =" triggers "> <list> <ref bean =" excuteTime "/> <! -- <Ref bean = "every10minute"/> --> </list> </property> </bean>
------------------------------------------------------------ A task with parameters that can be transferred --------------------------------------------------------------------
public class CanTransferParm extends QuartzJobBean{ @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap(); for (Map.Entry entry : jobDataMap.entrySet()){ System.out.println("key---: " + entry.getKey() + "value---: " + entry.getValue()); } }}
<Bean id = "hasReturnParm" class = "org. quartz. jobDataMap "> <constructor-arg> <map> <entry key =" validateVendorQuotationInterface "value =" http://x.x.x.x/validateVendorQuotationInterface "/> <entry key =" validateComparsionInterface "value =" http://x.x.x.x/validateComparsionInterface "/> </map> </constructor-arg> </bean> <bean id = "hasReturnJobDetail" class = "org. springframework. scheduling. quartz. jobDetailFacto RyBean "> <property name =" jobClass "value =" com. x. x. canTransferParm "/> <property name =" jobDataMap "> <ref bean =" hasReturnParm "> </ref> </property> </bean> <bean id =" every5minute class "= "org. springframework. scheduling. quartz. cronTriggerFactoryBean "> <! -- Work bean --> <property name = "jobDetail"> <ref bean = "hasReturnJobDetail"/> </property> <! -- Cron expression --> <property name = "cronExpression"> <value >$ {cron. every5minute} </value> </property> </bean> <bean id = "startQuartz" lazy-init = "false" autowire = "no" class = "org. springframework. scheduling. quartz. schedulerFactoryBean "> <property name =" triggers "> <list> <ref bean =" every5minute "/> </list> </property> </bean>