The simple configuration and use of quartz, through the xml configuration file to achieve the function of quartz. Create a maven project and configure the basic operation preparation.
1. Configure simpleTrigger rules
Create a new java class MyBean, as follows
Configuring in xmlpackage com.lzt.XmlTest; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Component; @Component public class MyBean { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void printMessage(){ System.out.println("hello MyBean:"+sdf.format(new Date())); } }
<! -- using MethodInvokingJobDetailFactoryBean, the task class can specify the calling method through targetMethod without implementing the Job interface -- > <! -- use the simpleTrigger rule -- > <! -- define target beans and methods in beans -- > <bean id="myBean" class="com.lzt.XmlTest.MyBean"></bean> <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myBean" /> <property name="targetMethod" value="printMessage" /> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="simpleJobDetail" /> <property name="startDelay" value="0" /> <property name="repeatInterval" value="1000" /> </bean>
2. Configure to use the kerntrigger rule
A. Inherit QuartzJobBean
Create the java class CronTest.
xml configurationpackage com.lzt.XmlTest; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Component; @Component public class CronTest extends QuartzJobBean { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { System.out.println("hello CronTest:"+sdf.format(new Date())); } }
Run once a day on 11.19 and test as follows:<!-- inherit QuartzJobBean --> <bean name="CronDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.lzt.XmlTest.CronTest" /> <property name="durability" value="true" /> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="CronDetail" /> <!--<property name="cronExpression" value="0 0 12 * * ?" />12 o'clock everyday--> <property name="cronExpression" value="0 19 11 * * ?" /> </bean>
B. Don't inherit QuartzJobBean
Create java class CronTest1
xml configurationpackage com.lzt.XmlTest; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Component; @Component public class CronTest1{ private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void printMessage(){ System.out.println("hello CronTest1:"+sdf.format(new Date())); } }
Test output time on 11.25 every day<!-- No inheritance --> <bean id="CronDetail1" class="com.lzt.XmlTest.CronTest1"></bean> <bean id="CronDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="CronDetail1" /> <property name="targetMethod" value="printMessage" /> </bean> <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="CronDetail2" /> <!--<property name="cronExpression" value="0 0 12 * * ?" />12 o'clock everyday--> <property name="cronExpression" value="0 25 11 * * ?" /> </bean>
For the public configuration of the above configuration, add Trigger and jobDetail into the task to schedule.
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="simpleJobDetail" /> <ref bean="CronDetail"></ref> <ref bean="CronDetail2"></ref> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> <ref bean="cronTrigger"></ref> <ref bean="cronTrigger2"></ref> </list> </property> <!-- <property name="configLocation" value="classpath:quartz.properties"/> --> </bean>