ITeye博客

spring定时器用Annotation实现

simple1024 · 2011-06-29 14:54 · Spring · 编程语言 · Markdown

spring定时器用Annotation实现

 

由于项目中需要定时更新一些数据,所以用到了spring的定时器,在使用Quartz过程中,遇到了一些麻烦,最终牵扯的错误太多:

1、我的一个Service类中需要加入定时执行即Service extends QuartzJobBean,但是Service类中使用@Autowired注入了属性:dao对象

2、在executeInternal方法执行过程中,dao对象一直为null。

3、尝试了各种办法,最终在配置文件中这样配置(这种配置网上多的是)

使用jobDataAsMap这个属性

<bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass">
            <value>org.wapgame.service.charts.Service</value>
        </property>
        <property name="jobDataAsMap">
            <map>
                  <entry key="dao" value-ref="Dao"/>
                  <entry key="test" value="30"/>
             </map>
        </property>
 </bean>

4、executeInternal方法中使用其参数来获得值。

protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    // TODO Auto-generated method stub
    JobDataMap map=arg0.getMergedJobDataMap();
    int test = Integer.parseInt(map.get("test").toString());
    log.debug("test:"+test);
    playerDao=(PlayerDao)map.get("playerDao");
  }

这样就解决了注入的对象为null的问题。网上找了好多找不到解决的办法,最终还得靠自己。

5、解决是解决了,但是过程中我需要用到事务操作。随之带来了很大的麻烦,毕竟项目用的都是Annotaion,再加上自己在ApplicationContext.xml中配置,造成了很多麻烦和异常。

6、放弃,转Annotation

 

通过 注解 来调度任务
1、
AnnotationQuartz类:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class AnnotationQuartz {
  @Scheduled(cron="0,10,20,30,40,50 * * * * ?")   //需要注意@Scheduled这个注解,它可配置多个属性:cron\fixedDelay\fixedRate
  public void test()
  {
    System.out.println("0.0");
  }
}

2、
spring的ApplicationContext.xml中的配置:
只需要加上
<!--  定时器开关  开始-->
    <task:annotation-driven/>
<!--  定时器开关  结束-->

3、(如果是web应用,那么需要再web.xml中加入)
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

通过 配置文件 来调度任务
1、
AnnotationQuartz类:
import org.springframework.stereotype.Component;

@Component(如果你的项目使用的是注解而不是配置文件中写bean,那么需要加上@Component,确保这个类已经注入)
public class AnnotationQuartz {
  public void test()
  {
    System.out.println("0.0");
  }
}
2、
spring的ApplicationContext.xml中的配置:
<task:scheduled-tasks> 
    <task:scheduled ref="chartsService" method="test" cron="0 0,15,30,45 * * * ?"/> 
</task:scheduled-tasks>

最后请注意 :(ApplicationContext.xml)
a)、需要在xmlns里面加入:
xmlns:task="http://www.springframework.org/schema/task"

b)、在xsi:schemaLocation中加入
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd(别忘了最后的引号)

 

 

cron表达式去搜吧,好多,我搜到了一些,如下:

<!--

一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下: 
              1.秒(0-59) 
              2.分钟(0-59) 
              3.小时(0-23) 
              4.月份中的是期(1-31) 
              5.月份(1-12或SUN-DEC) 
              6.星期中的日期(1-7或SUN-SAT) 
              7.年份(1970-2099)  
                 例子: 
                  0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点 
                  0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟 
                  30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时 
                  0 0 8-5 ? * MON-FRI 每个工作日的工作时间 
              - 区间 
              * 通配符 
              ? 你不想设置那个字段 

-->

评论(2)

  1. #2 楼 · simple1024 · 2011-12-03 17:26

    json20080301 writes:

    我补充一下,停止,暂停,和继续的方法

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("context/context-comm-mail.xml"); ctx.start(); org.quartz.impl.StdScheduler Scheduler = ctx.getBean("scheduler", org.quartz.impl.StdScheduler.class); try { System.out.println("-----------------------------暂停 mailJob"); Thread.sleep(5000);//Scheduler.shutdown();完全停止 Scheduler.pauseTrigger("mailJob", null); //暂停 System.out.println("-----------------------------重启 mailJob"); Thread.sleep(100000);//Scheduler.shutdown(); Scheduler.resumeTrigger("mailJob", null); //继续 } catch (Exception e) { // TODO: handle exception }

    呵呵,不错不错

  2. #1 楼 · json20080301 · 2011-12-02 10:15

    我补充一下,停止,暂停,和继续的方法

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("context/context-comm-mail.xml"); ctx.start(); org.quartz.impl.StdScheduler Scheduler = ctx.getBean("scheduler", org.quartz.impl.StdScheduler.class); try { System.out.println("-----------------------------暂停 mailJob"); Thread.sleep(5000);//Scheduler.shutdown();完全停止 Scheduler.pauseTrigger("mailJob", null); //暂停 System.out.println("-----------------------------重启 mailJob"); Thread.sleep(100000);//Scheduler.shutdown(); Scheduler.resumeTrigger("mailJob", null); //继续 } catch (Exception e) { // TODO: handle exception }

评论功能维护中,当前仅展示历史评论。