之前介绍过可以在plsql软件上编写oracle的定时器以及配合存储过程进行存储数据。今天来讲讲怎么在服务器上的xml配置定时器,实现定时执行代码段的功能。
- xml配置
<?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-3.0.xsd"
default-lazy-init="false">
<!-- 调用定时器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="SetTime" /><!-- 测试定时器 -->
</list>
</property>
</bean>
<!-- 定时器定时 -->
<bean id="SetTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="createTime"/>
</property>
<property name="cronExpression">
<value>0 31 10 ? * *</value><!--设置触发定时器时间 -->
</property>
</bean>
<!-- 创建定时器 ,并且分配执行方法 -->
<bean id="createTime" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="timeToService"/>
</property>
<property name="targetMethod">
<value>testTime</value>
</property>
</bean>
<!-- 定时器指向的Service -->
<bean id="timeToService" class="ffcs.cn.peam.time.service.TimeService"/>
</beans>
2.service代码
package ffcs.cn.peam.time.service;
public class TimeService {
public void testTime(){
System.out.println("定时器触发了啊");
}
}
简单的定时就完成了。