java 代码:
- 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>
- <!-- 启动定时器 -->
- <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="applicationContextSchedulerContextKey">
- <value>applicationContext</value>
- </property>
- <property name="triggers">
- <list><ref bean="cronReportTrigger2"/></list>
- </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
- }