第一种:调用某个类里面的方法
applicationContext.xml
<!-- 定义要调用的任务的实现类 -->
<bean id="testQuartz" class="crm.action.Test">
</bean>
<!--调用实现类里面的某个方法 -->
<bean id="testTellTime"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="testQuartz" />
</property>
<property name="targetMethod">
<value>tellTime</value>
</property>
</bean>
<!--定义调用任务的时间 -->
<bean id="testSayTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="testTellTime" />
</property>
<property name="cronExpression">
<value>0/15 * * * * ?</value>
</property>
</bean>
<!--管理触发器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="testSayTrigger" />
</list>
</property>
</bean>
Test.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public void tellTime(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String ly_time = sdf.format(now);
System.out.println(ly_time+"\n");
}
}
第二种:继承QuartzJobBean类
<bean name="testQuartz" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>crm.action.Test</value>
</property>
<property name="jobDataAsMap">
<map>
<!-- spring 的依赖注入 -->
<entry key="crmcontactservice"><ref local="crmcontactservice"/></entry>
</map>
</property>
</bean>
<!--定义调用的任务的时间 -->
<bean id="testHelloTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="testQuartz"/>
</property>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 触发器的管理-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="testHelloTrigger"/>
</list>
</property>
</bean>
Test.java
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import crm.service.CrmContactservice;
public class Test extends QuartzJobBean{
private CrmContactservice crmcontactservice;
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String ly_time = sdf.format(now);
System.out.println(ly_time+"\n");
}
public CrmContactservice getCrmcontactservice() {
return crmcontactservice;
}
public void setCrmcontactservice(CrmContactservice crmcontactservice) {
this.crmcontactservice = crmcontactservice;
}
}