Java job 定时器

来源:互联网 发布:大数据架构师 技术题 编辑:程序博客网 时间:2024/06/01 09:26

测试类:
package com.job;
import java.util.Date;
public class TimingJob1 { 
    public void executeTask1() { 
        System.out.println("大家好"); 
    
 
}
public class TimingJob2 { 
    public void executeTask2() { 
        System.out.println("大家好"); 
    
 
}


配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="timingJob1" class="com.job.TimingJob1" />

<bean id="timingJob2" class="com.job.TimingJob1" />

<!--配置一个job方法-->
<bean id="jobBean1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
         <ref bean="timingJob1"/>
    </property>
    <property name="targetMethod">
       <value>executeTask1</value>
    </property>
  </bean>
<!--配置第二个job方法-->
<bean id="jobBean2"
   class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
   <property name="targetObject">
    <ref bean="timingJob2" />
   </property>
   <property name="targetMethod">
    <value>executeTask2</value>
   </property>
</bean>

<!--简单的触发器-->
<bean id="simpleTimingJobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
    <ref bean="jobBean1" />
</property>
<!-- 以毫秒为单位,启动后一分钟触发 -->
<property name="startDelay">
    <value>60000</value>
</property>
<!-- 每间隔一分钟触发一次 -->
<property name="repeatInterval">
    <value>60000</value>
</property>
</bean>
 
<!-复杂的触发器-->
<bean id="complexTimingJobTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
    <ref bean="jobBean1" />
</property>
<property name="cronExpression">
    <!--每分钟触发一次 -->
    <value>0 0/1 * * * ?</value>
</property>
</bean>

<bean id="complexTimingJobTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
   <property name="jobDetail">
    <ref bean="jobBean2" />
   </property>
   <property name="cronExpression">
    <value>0 0 */1 * * ?</value>//每小时执行一次
   </property>
</bean>

<!-- Spring触发工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
    <list>
        <ref bean="complexTimingJobTrigger1" />
        <!-- ....下面可以继续添加其他触发器 -->
           <ref local="complexTimingJobTrigger2" />
    </list>
</property>
</bean>
</beans>


时间触发点:
"0 0 12 * * ?"    每天中午12点触发   
"0 15 10 ? * *"    每天上午10:15触发   
"0 15 10 * * ?"    每天上午10:15触发   
"0 15 10 * * ? *"    每天上午10:15触发   
"0 15 10 * * ? 2005"    2005年的每天上午10:15触发   
"0 * 14 * * ?"    在每天下午2点到下午2:59期间的每1分钟触发   
"0 0/5 14 * * ?"    在每天下午2点到下午2:55期间的每5分钟触发    
"0 0/5 14,18 * * ?"    在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发    
"0 0-5 14 * * ?"    在每天下午2点到下午2:05期间的每1分钟触发   
"0 10,44 14 ? 3 WED"    每年三月的星期三的下午2:10和2:44触发   
"0 15 10 ? * MON-FRI"    周一至周五的上午10:15触发   
"0 15 10 15 * ?"    每月15日上午10:15触发   
"0 15 10 L * ?"    每月最后一日的上午10:15触发   
"0 15 10 ? * 6L"    每月的最后一个星期五上午10:15触发     
"0 15 10 ? * 6L 2002-2005"    2002年至2005年的每月的最后一个星期五上午10:15触发   
"0 15 10 ? * 6#3"    每月的第三个星期五上午10:15触发


0 0
原创粉丝点击