日期:2014-05-20  浏览次数:20788 次

java定时任务TimerTask问题(在线等)
我用TimerTask做个定时任务,但我用的是SSH框架,也就是里面的一些属性是注入的,但这里我就无法解决
先发下我原代码

Java code

package com.common;
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);
    //设置任务计划,启动和间隔时间
    //86400000为1天毫秒数
    timer.schedule(new MyTask(), 0, 5000);
  }

  public void contextDestroyed(ServletContextEvent event) {
    timer.cancel();
  }
  
}

package com.common;

import java.util.Date;
import java.util.TimerTask;

import com.user.dao.MaxLshDao;
import com.user.service.MaxLshService;


public class MyTask extends TimerTask {
    private MaxLshService maxLshService;


    public MaxLshService getMaxLshService() {
        return maxLshService;
    }


    public void setMaxLshService(MaxLshService maxLshService) {
        this.maxLshService = maxLshService;
    }


    public void run() {
         System.out.println("call at " + (new Date()));
         /*ShopInUserDao SIUService=new ShopInUserDao();
         SIUService.findEndRentDate();*/
      }
  


}






再发下web.xml

<listener>
<listener-class>com.common.MyListener</listener-class>
</listener>
 

现在的问题就是TimerTask 里的maxLshService一直都是空的,无法注入

<bean id="myTask" class="com.common.MyTask">
<property name="maxLshService">
<ref bean="maxLshService" />
</property>
</bean>



------解决方案--------------------
<ref bean="maxLshService" />
这个怎么配的?其他的我看了都没有任何问题

自己在看一下,代码没什么问题的。配置问题

祝楼主好运
------解决方案--------------------
不懂spring的说,帮顶。
------解决方案--------------------
<bean id="myTask" class="com.common.MyTask" autowire="byName> 

我记的好象是这样。
------解决方案--------------------


timer.schedule(WebApplicationContextUtils.getWebApplicationContext(ServletContext).getBean("myTask"), 0, 5000);
------解决方案--------------------
定时任务Spring的Quartz挺好用的。
------解决方案--------------------
web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

如果容器创建MyListener实例的时候,spring容器先加载初始化了,注入是没问题的。
------解决方案--------------------
前不久我也做了个定时任务短信提醒的功能。我把我的那部分贴上来你看看吧,或许有点帮助。

Java code

//spring配置文件里的内容

    <!--任务到期短信提示-->
    <bean id="testService"  class="com.fbty.wms.utils.TestService">
        <property name="messageService" ref="messageService" />
        <property name="wmsJdbcDao" ref="wmsJdbcDao" />
    </bean>
    
<!--注入-->
    
    <bean id="testJob"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="testService" />
        <property name="targetMethod" value="doCheck"></property>
    </bean>

<!-- 触发器 -->
    <bean id="testThread"
        class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="testJob" />
        <property name="cronExpression" value="0 0 0 * * ? " />
    </bean>
<!-- 定时器工厂     -->
<bean
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="testThread"></ref>
            </list>
        </property>
    </bean>


//这个就是TestService里的doCheck方法

    /**
     * Description: 到期警示告警短信自动扫描<br>
     * @throws ServiceException 
     * @throws RemoteException 
     * @throws MalformedURLException 
     */
    public void doCheck() throws MalformedURLException, RemoteException, ServiceException {
        System.out.println("开始计时,每隔一分钟提示一次……");
               //里面内容就省略了,整个流程重点是配置文件。仅做参考。。
    
}