通过java定时器执行任务

来源:互联网 发布:浙江理工大学网络缴费 编辑:程序博客网 时间:2024/05/16 15:38

方案1:借助于监听器和TimerTaskTimer类进行实现
------------------------------------------------------------------------------创建一个新计时器任务
-----------------------------------------------------------------------
package com.timer;

import java.util.Date;
import java.util.TimerTask;
/**
 * 创建一个新的计时器任务
 */
public class MyTask extends TimerTask {
       //此计时器任务要执行的操作
       public void run() {
              System.out.println("call at " + (new Date())); 
       }
}
-------------------------------------------------------------------------------------------------------创建监听器-------------------------------------------------------------------------------
package com.timer;

import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListen implements ServletContextListener {

       private Timer timer = null; 
       public void contextDestroyed(ServletContextEvent arg0) {
              timer.cancel();
       }

       //初始化上下文
       public void contextInitialized(ServletContextEvent arg0) {
              timer = new Timer(true); 
              //设置指定的任务,启动时间和间隔时间 
              timer.scheduleAtFixedRate(new MyTask(), 0, 1000 * 10); 
       }
}
--------------------------------------------------------------------------------------------------配置web.xml文件-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="
http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
       <listener>
              <listener-class>com.timer.MyListen</listener-class>
       </listener>
 </web-app>

方案2:利用spring提供的定时器包quartz-1.8.4.jar()实现
-----------------------------------------------------------------------------------------Spring配置------------------------------------------------------------------------------
  <bean id="scheduleInfoAction" class="com.netsdar.webservice.client.SendMessageClient"></bean> 
  <!--以上配置为任务类的注入-->
  <bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
         <property name="targetObject" ref="scheduleInfoAction"/>                 <!--注入的目标任务类-->
         <property name="targetMethod" value="SendMessageToEHR"/>     <!--任务类中的目标方法-->
         <property name="concurrent" value="false"/> 
  </bean> 
     
  <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" > 
         <property name="jobDetail" ref="schedulerJobDetail"/> 
         <property name="cronExpression"> 
                <value>59 59 23 * * ?</value>      <!--设置定时器的时间59 59 23 * * ?为每天的23:59:59执行-->
         </property> 
  </bean> 
  <!--定时器的执行-->  
  <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
         <property name="triggers"> 
                  <list> 
                           <ref local="cronTrigger"/> 
                  </list> 
         </property>
 </bean>
-------------------------------------------------------------------------------------------注意事项-----------------------------------------------------------------------------
        1.在<Beans>中不能够设置default-lazy-init=“true”,否则定时任务不触发,如果不明确指明default-lazy-init的值,默认是false。

        2.在<Beans>中不能够设置default-autowire=“byName”的属性,否则后台会报org.springframework.beans.factory.BeanCreationException错误,这样就不能通过Bean名称自动注入,必须通过明确引用注入。

原创粉丝点击