Spring的自动任务中调用业务类方法

14 篇文章 0 订阅
13 篇文章 0 订阅

Spring 中使用 QuartzJobBean 来进行定时任务的操作

 

代码如下:

@Controller @Scope("prototype")
public class QuartzSyncJob extends QuartzJobBean {
 private MsgBusinessImpl msgBusinessImpl ;      
 public void setMsgBusinessImpl(MsgBusinessImpl msgBusinessImpl) {
  this.msgBusinessImpl = msgBusinessImpl;
 }
 protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
  if(msgBusinessImpl != null){   
   msgBusinessImpl.deleteMsg(1);
  }
 }
}

 

其中private MsgBusinessImpl msgBusinessImpl ;   和setMsgBusinessImpl 这两个步十分重要,本来也想用action中那种方案自动注入,调用实例的,结果总是空指针。

 

用此方案,还需要在Spring配置文件,加入 <ref bean="msgBusinessImpl"/> 
这样,自动任务中的业务类,就能得到0实例了。

 

<!-- Configure the sync Service  -->
    <bean name="randomSyncJob"
       class="org.springframework.scheduling.quartz.JobDetailBean">  
       <property name="jobClass">
           <value>com.fireinwind.job.QuartzSyncJob</value>
       </property>  
       <property name="jobDataAsMap">
           <map>
              <entry key="timeout">
                  <value>5</value>
              </entry>
              <entry key="msgBusinessImpl"> 
                 <ref bean="msgBusinessImpl"/> 
              </entry>
           </map>
       </property>  
    </bean>
    <!-- 配置触发器 -->
    <bean id="cronTrigger"
       class="org.springframework.scheduling.quartz.CronTriggerBean">  
       <property name="jobDetail">
           <ref bean="randomSyncJob" />
       </property>
       <!-- 每天0点0分,触发RandomPriceJob,具体说明见附录 -->
       <property name="cronExpression">
           <value>0 0 0 * * ?</value>
       </property>  
    </bean>
 
    <bean autowire="no"
       class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
       <!-- 添加触发器 -->
       <property name="triggers">
           <list>
              <ref local="cronTrigger" />
           </list>
       </property>
    </bean>

 

 

 ==========

自己在另一个项目(tdstatistic)中也想用此算法,ShopBusiness是接口,怎么折腾也没折腾成功,后来又定义了一个ShopBusiness2为真实的业务类才解决接口类注入失败的问题,早知道自己是这么写的,把实现类注入不就行了。呵。。。。

2012-11-18晚

 

 ------------------------------------------------------

2013-07-18 总结

 

在配置文件中的类不是接口

应该是这个样子的

@Service
public class QuartzService {
   @Resource private RuleLogDAO ruleLogDAO;
 }

 

 

spring 配置文件

 <!-- Configure the sync Service  -->
    <bean name="randomSyncJob"
       class="org.springframework.scheduling.quartz.JobDetailBean">  
       <property name="jobClass">
           <value>com.zqk.cpyj.filter.QuartzSyncJob</value>
       </property>
    <property name="jobDataAsMap">  
   <map> 
    <entry key="quartzService" value-ref="quartzService"/>
   </map>  
       </property>
  
    </bean>

    <!-- 配置触发器 -->
    <bean id="cronTrigger"
       class="org.springframework.scheduling.quartz.CronTriggerBean">  
       <property name="jobDetail">
           <ref bean="randomSyncJob" />
       </property>
       <!-- 每天0点0分,触发RandomPriceJob,具体说明见附录 -->
       <property name="cronExpression">
           <value>0 * * * * ?</value>
       </property>  
    </bean>
 
    <bean autowire="no"
       class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
       <!-- 添加触发器 -->
       <property name="triggers">
           <list>
              <ref local="cronTrigger" />
           </list>
       </property>
    </bean> 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值