经过这次项目对于定时调用有了一定的了解。感觉比较好的有两种方法,1、一种是Timer类的定时调用。这种方式的缺点就是第一次调用的时候要手动启动,以后开始每天循环调用。当然这种方式的好处就是可以控制每天调用多少次。虽然第一次比较麻烦,但是给我的感觉还是不错的。下面我们来把这种方式稍微写一下。
在此处我用定时将日志文件中的数据入库作为例子来写
public class SavaLog{ public void savaLog(){ Date() d=new Date(); Timer timer=new Timer();//实力化一个定时器类 TimerTask task=new TimerTask(){ public void run() {//使用匿名类的方式重写TimerTask中的run方法 System.out.println("======================计时器开始加载"); //在此方法中就可以调用数据层方法,或者其他的方法。run()中 的内容会感觉我们的设置定时被 执 行 } }; /** 此处的设置表示从d所标示的时间起,没经过1000毫秒调用一次此方法。如果想每天调用此意则可以将 1000修改成 1000*60*60*24。 **/ timer.schedule(task, d,1000); } }
以上方法,如果是在web项目中,可以将此方法写在action或者是servlet中。然后再页面中加一个form表单。表单中添加一个submit按钮。form表单链接到action或者是servlet。想启动此定时方法的时候只要点击按钮就可以。我写的例子中式取当前时间。也就是点击按钮的时候run方法执行一次。而且是每1秒执行一次。如果将1000修改成 1000*60*60*24。则表示每天在你点击按钮的按个时间调用run方法。执行里面的内容。
2、第二种方式是使用spring的方式来调用
第一步、我们先写一个类,这个类中的方法将会被定时调用
public class SavaLog{ public void savaLog(){ //添加其他的逻辑代码,比如调用数据层的 方法,进行数据库操作。 } }
第二步、导入一个jar包:quartz-all-1.6.5.jar 这个jar包网上到处都有,我的附件中也有提供。 第三步:配置spring的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="backupQuarz" class="SavaLog" /><!--此处将类加载进来。如果有 包一定前面要加包名--> <bean id="quartzDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="backupQuarz" /> </property> <property name="targetMethod"> <value>savaLog</value><!--此处添加要定时调用的方法名--> </property> <property name="concurrent" value="false" /> </bean> <bean id="triggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="quartzDetail" /> <!-- 每天早上4:35调用一次。 --> <property name="cronExpression" value="0 35 4 * * ?"/> </bean> <!-- 启动前面定义的定时器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="triggerBean" /> </list> </property> </bean> </beans>
以上内容完成后就可以进行调用调用savaLog方法了。要注意的是。调用的这个方法不能有传入参数。如果是在web项目中。涉及到调用action中的方法,而你的action又用spring进行了代理。也就是 spring中有一个bean的配置是<bean:name="/log" class="SavaLog"> ,此时,就要更改一下配置,配置如下:
<bean id="quartzDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="/log" /> </property> <property name="targetMethod"> <value>savaLog</value><!--此处添加要定时调用的方法名--> </property> <property name="concurrent" value="false" /> </bean> <bean id="triggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="quartzDetail" /> <!-- 每天早上4:35调用一次。 --> <property name="cronExpression" value="0 35 4 * * ?"/> </bean> <!-- 启动前面定义的定时器 --> <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="triggerBean" /> </list> </property> </bean>