Spring定时器和Cron表达式的详细配置

quartz的详细配置可参考:

http://g4-gc.iteye.com/blog/1740123

 

例子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
       default-lazy-init="false">

<bean id="passwordupdater" class="com.test.PasswordUpdater">
</bean>
      

<bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
 <property name="targetObject" ref="passwordupdater" />  <!-- 定时器的类 -->
   <property name="targetMethod" value="process" />   <!-- 指定定时执行的方法 -->
   <property name="concurrent" value="false" />
</bean>
<bean id="testJobDetailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail"><ref bean="testJobDetail"/></property>  <!-- 定时JOB-->
 <property name="cronExpression"><value>0 0 4 1,15 * ?</value></property>  <!--定时计划 -->
</bean>


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
 <property name="waitForJobsToCompleteOnShutdown"><value>true</value></property>
    <property name="triggers">
  <list>
   <ref bean="testJobDetailTrigger"/>
  </list>
 </property>
    <property name="quartzProperties">
    <props>
    <prop key="org.quartz.threadPool.threadCount">1</prop>
    <prop key="org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer">true</prop>
    </props>
    </property>
</bean>

</beans>

 

 

package com.test;

 

public class PasswordUpdater {
 
 //定时调用的方法
 public void process(){
  prepare();
  doUpdate();
 }

 public void prepare(){
  System.out.println("perpare...");
 }
 
 public void doUpdate(){
  System.out.println("do update...");
 }
}

 

 

package com.test;

public class WebTermApplicationUtil {
 
 //本质就是加载applicationContext-quartz.xml里的定时器定时处理更新密码的任务
 private static String[] beanXML = {"applicationContext-quartz.xml"};
 
 private static ApplicationContext ac ;
 
 public static void init(boolean isRunOnce){
   ac = new ClassPathXmlApplicationContext(beanXMLOnce);
 }
 
 public static Object getApplicationBean(String beanName){
  return ac.getBean(beanName);
 }
}

 

 

 

package com.test;



public class App
{
    public static void main( String[] args ){
   WebTermApplicationUtil.init(false);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值