我在网上看了下关于定时任务的代码。
如下:
本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task)。
专业的开源工具可参考Quartz:http://www.opensymphony.com/quartz/MyListener:
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyListener implements ServletContextListener {
  
  private Timer timer = null;  public void contextInitialized(ServletContextEvent event) {
    timer = new Timer(true);
    //设置任务计划,启动和间隔时间
    timer.schedule(new MyTask(), 0, 86400000);
  }  public void contextDestroyed(ServletContextEvent event) {
    timer.cancel();
  }
  
}
MyTask:
import java.util.TimerTask;public class MyTask extends TimerTask {  public void run() {
    // System.out.println("call at " + (new Date()));
    // TODO 此处添加具体任务代码
  }}
web.xml配置:
<listener>
<listener-class>com.fastunit.samples.listener.MyListener</listener-class>
</listener>我想问的是,如果我的项目是个java项目。怎么配置这个监听?
引入了spring框架

解决方案 »

  1.   

    quartz可以有自己独立配置文件,不用spring
      

  2.   


    quartz怎么写一个自己的配置文件?
      

  3.   

    1,在项目中加如下六个jar包:
    commons-collections-3.2.jar
    commons-logging-1.0.4.jar
    commons-pool.jar
    jta-1.1.jar
    quartz-1.6.0.jar
    spring.jar
    2,java代码package com;import java.util.Date;public class YouObject
    {
    public void youMethod()
    {
    System.out.println(new Date()+":每隔一分钟执行一次");
    }
    }3,java代码
    applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <!--  <property name="autoStartup"  value="false"/> 自动转为手动-->
    <bean
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
    <list><!-- 在测试时,除非自己模块测试可以开启,其它模块不要自己开启 -->
    <ref bean="cronTrigger" />
    </list>
    </property>
    <property name="quartzProperties">
    <props>
    <prop key="org.quartz.threadPool.threadCount">1</prop>
    </props>
    </property>
    </bean>
    <!-- 下面是每格一秒做的线程-->
    <bean id="cronTrigger"
    class="org.springframework.scheduling.quartz.CronTriggerBean">
    <!-- 知道要执行的对象 -->
    <property name="jobDetail" ref="jobDetailBean" />
    <!-- 下面是设置运行时间,详细见工程中doc文件下的文档 
    <property name="cronExpression" value="* * * * * ?" />
    -->
    <property name="cronExpression" value="0 * 14 * * ?" />
    </bean>
    <!-- 
    <bean id="jobDetailBean"
     -->
    <bean id="jobDetailBean"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!-- 指定要执行的对象-->
    <property name="targetObject" ref="youObject" />
    <!--指定要执行的对象的方法  -->
    <property name="targetMethod" value="youMethod" />
    </bean>
    <bean id="youObject" class="com.YouObject"></bean>
    </beans>
      

  4.   

    4,web.xml<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <!-- acegi -->
    <listener>
    <listener-class>
    org.springframework.web.util.Log4jConfigListener
    </listener-class>
    </listener>
    </web-app>
    在你的myeclipse中写入以上代码后,发布工程就可以了,希望对你有帮助
      

  5.   

    伙计,我的不是web工程,是个java工程。
    没有web.xml  这才是问题。
      

  6.   

    首先得告诉楼主,你想要实现的效果是定时执行任务。
    你在web.xml 中配置
    <listener>
    <listener-class>com.fastunit.samples.listener.MyListener</listener-class>
    </listener>
    表示你启动web 服务器的时候,会实例化对象com.fastunit.samples.listener.MyListener,并且调用
    public void contextInitialized(ServletContextEvent event) {
       timer = new Timer(true);
       //设置任务计划,启动和间隔时间
       timer.schedule(new MyTask(), 0, 86400000);
       }
    这个方法的代码。如果你想实现同样的效果的话,只需要在写个一main 方法在里面,并且创建com.fastunit.samples.listener.MyListener对象,然后调用contextInitialized(null)方法即可。
      

  7.   

    spring定时器:http://apps.hi.baidu.com/share/detail/31761760