---
title: "java中定时调用任务（如每天定时将日志入库，每天定时备份数据等等）"
author: "ch0431"
date: "2010-11-05T11:02:30Z"
tags: []
categories: ["企业架构"]
slug: "java"
draft: false
lastmod: "2010-11-08T12:08:22Z"
id: 803570
url: "https://ch0431.iteye.com/blog/803570"
---

查看此文章的朋友有可以留下联系方式，大家相互交流，共同进步。谢谢             

经过这次项目对于定时调用有了一定的了解。感觉比较好的有两种方法，

              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的配置文件

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"  
 xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"  
 xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
           [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd](http://www.springframework.org/schema/aop/spring-aop-2.0.xsd)  
           [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.0.xsd](http://www.springframework.org/schema/tx/spring-tx-2.0.xsd)"&gt;  
 &lt;bean id="backupQuarz" class="SavaLog" /&gt;&lt;!--此处将类加载进来。如果有 包一定前面要加包名--&gt;  
    
  &lt;bean id="quartzDetail"  
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;  
  &lt;property name="targetObject"&gt;  
   &lt;ref bean="backupQuarz" /&gt;  
  &lt;/property&gt;  
  &lt;property name="targetMethod"&gt;  
   &lt;value&gt;savaLog&lt;/value&gt;&lt;!--此处添加要定时调用的方法名--&gt;  
  &lt;/property&gt;  
  &lt;property name="concurrent" value="false" /&gt;  
 &lt;/bean&gt;  
   
 &lt;bean id="triggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
     &lt;!-- see the example of method invoking job above --&gt;  
     &lt;property name="jobDetail" ref="quartzDetail" /&gt;  
     &lt;!-- 每天早上4:35调用一次。 --&gt;  
     &lt;property name="cronExpression" value="0 35 4 * * ?"/&gt;  
    &lt;/bean&gt;  
    
 &lt;!-- 启动前面定义的定时器 --&gt;  
 &lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
     &lt;property name="triggers"&gt;  
         &lt;list&gt;  
                       &lt;ref bean="triggerBean" /&gt;               
         &lt;/list&gt;  
     &lt;/property&gt;  
 &lt;/bean&gt;  
&lt;/beans&gt;

以上内容完成后就可以进行调用调用savaLog方法了。要注意的是。调用的这个方法不能有传入参数。如果是在web项目中。涉及到调用action中的方法，而你的action又用spring进行了代理。也就是 spring中有一个bean的配置是&lt;bean:name="/log" class="SavaLog"&gt; ,此时，就要更改一下配置，配置如下：

    
  &lt;bean id="quartzDetail"  
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;  
  &lt;property name="targetObject"&gt;  
   &lt;ref bean="/log" /&gt;  
  &lt;/property&gt;  
  &lt;property name="targetMethod"&gt;  
   &lt;value&gt;savaLog&lt;/value&gt;&lt;!--此处添加要定时调用的方法名--&gt;  
  &lt;/property&gt;  
  &lt;property name="concurrent" value="false" /&gt;  
 &lt;/bean&gt;  
   
 &lt;bean id="triggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
     &lt;!-- see the example of method invoking job above --&gt;  
     &lt;property name="jobDetail" ref="quartzDetail" /&gt;  
     &lt;!-- 每天早上4:35调用一次。 --&gt;  
     &lt;property name="cronExpression" value="0 35 4 * * ?"/&gt;  
    &lt;/bean&gt;  
    
 &lt;!-- 启动前面定义的定时器 --&gt;  
 &lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
     &lt;property name="triggers"&gt;  
         &lt;list&gt;  
                       &lt;ref bean="triggerBean" /&gt;               
         &lt;/list&gt;  
     &lt;/property&gt;  
 &lt;/bean&gt;