Let’s look at the spring configuration file directly:
<?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-2.5.xsd ">
<bean id="testTimerTask" class="com.test.timerTask.TestTimerTask"></bean>
<bean id="serviceFor" class="com.test.timerTask.Service4Job"></bean>
<!-- The first step: Define the task scheduling class, that is, the class that needs to perform business work. There are two situations, choose one for scheduling -->
<!-- Task scheduling class case 1: Use a class inherited from the abstract class QuartzJobBean to implement task scheduling -->
<bean id="testTimerTaskJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<!-- Note that the full name of the class is injected here, not a reference to the class -->
<property name="jobClass" value="com.test.timerTask.TestTimerTask"></property>
<!-- Indicates injecting the required beans into the testTimerTask class -->
<property name="jobDataAsMap">
<map>
<entry key="service4Job" value-ref="serviceFor"></entry>
</map>
</property>
</bean>
<!-- Task scheduling class case 2: Directly schedule business methods in existing classes without inheriting from QuartzJobBean -->
<bean id="noJobBeanTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="serviceFor"></property>
<property name="targetMethod" value="job"></property>
</bean>
<!-- Step 2: Define the trigger, indicating when or how often to schedule the tasks defined in the first step -->
<!-- Trigger case 1: Based on SimpleTriggerBean task call -->
<bean id="quartzSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="testTimerTaskJob"></property>
<property name="repeatInterval" value="3000"></property>
<property name="startDelay" value="2000"></property>
</bean>
<!-- Trigger case 2: Based on SimpleTriggerBean task call -->
<bean id="quartzCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="noJobBeanTaskJob"></property>
<!-- The string form of cronExpression is used here. 0/3 means execution every 3 seconds. -->
<property name="cronExpression" value="0/3 * * * * ?"></property>
</bean>
<!-- Step 3: Start task scheduling -->
<!-- Start executing task scheduling trigger-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="quartzCronTrigger"/>
</list>
</property>
</bean>
</beans>
//================================================ ===
Two classes: TestTimerTask and Service4Job
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class TestTimerTask extends QuartzJobBean{
private Service4Job service4Job;
public void setService4Job(Service4Job service4Job) {
this.service4Job = service4Job;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
this.service4Job.job();
}
}
public class Service4Job {
public void job(){
System.out.println("**** "+System.currentTimeMillis());
}
}
(thismonth) Repost: http://yanda20056.blog.163.com/blog/static/5650193120091113115434635/
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/thismonth/archive/2009/12/30/5103969.aspx