首先建立bean.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--起动Bean-->
<bean id="test"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronReportTrigger" />
</list>
</property>
</bean>
<!--实际的工作Bean-->
<bean id="testjobbean"
class="com.zhouxf.quartz.TestJobBean">
</bean>
<!--jobBean用于设定启动时运用的Bean与方法-->
<bean id="scheduledReportJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="testjobbean" />
</property>
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!--定时器设定(0/2 43 12-17 * * ?在12-17点43分,每隔2秒运行一次)-->
<bean id="cronReportTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="scheduledReportJobDetail" />
</property>
<property name="cronExpression">
<value>0/2 43 12-17 * * ?</value>
</property>
</bean>
</beans>
JOB代码:
public class TestJobBean{
public void run() {
System.out.println("run..................");
}
}
测试代码:
public static void main(String[] args) throws BeansException, FileNotFoundException, InterruptedException {
// new LogInit("WEB-INF/classes/com/spring/helloworld/log4j.properties");
BeanFactory factory = new XmlBeanFactory(
new FileSystemResource(
"///home/zhouxf/work/WebPro/WebContent/WEB-INF/bean.xml"));
factory.getBean("test");
}
还有另一种配置方式,使用QuartzJobBean,它是JobDetailBean的子类,代码如下:
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class TestJobBean extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("TestJobBean..run................");
}
}
配置文件中加入:
<bean id="reportJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.zhouxf.quartz.TestJobBean</value>
</property>
</bean>
<!--定时器设定(0/2 43 12-17 * * ?在12-17点43分,每隔2秒运行一次)-->
<bean id="cronReportTrigger2"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="reportJob" />
</property>
<property name="cronExpression">
<value>0/1 31 12-17 * * ?</value>
</property>
</bean>
测试代码改为:
public static void main(String[] args) throws BeansException, FileNotFoundException, InterruptedException {
// new LogInit("WEB-INF/classes/com/spring/helloworld/log4j.properties");
BeanFactory factory = new XmlBeanFactory(
new FileSystemResource(
"///home/zhouxf/work/WebPro/WebContent/WEB-INF/bean.xml"));
factory.getBean("test");
factory.getBean("test2
}
默认情况下,Quartz Jobs是无状态的,可能导致jobs之间互相的影响。如果你为相同的JobDetail指定两个Trigger, 很可能当第一个job完成之前,第二个job就开始了。如果JobDetail对象实现了Stateful接口,就不会发生这样的事情。 第二个job将不会在第一个job完成之前开始。为了使得jobs不并发运行,设置MethodInvokingJobDetailFactoryBean中的concurrent标记为false。
<bean id="scheduledReportJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="simpleJob" />
</property>
<property name="targetMethod">
<value>run</value>
</property>
<property name="concurrent" value="false" />
</bean>
但是第二种方式的,没办法设置这个属性,暂时还不知道怎么去设置这个属性。