spring定时器用Annotation实现

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 每个工作日的工作时间 
              - 区间 
              * 通配符 
              ? 你不想设置那个字段 

-->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值