当前位置:编程学习 > JAVA >>

使用spring quartz的两种配置方式(静态设置与动态设置)

1、静态设置:示例,每五分钟执行一次myTask
    xml:
 1      <bean id="mySchedule" class="test.MySchedule"></bean>
 2
 3     <bean name="quartzScheduler" class="org.springframework.sche易做图ng.quartz.SchedulerFactoryBean">
 4         <property name="triggers">
 5             <list>
 6             <ref bean="myTrigger" />
 7             </list>
 8         </property>
 9         <property name="configLocation" value="classpath:quartz.properties"/>
10     </bean>
11     <bean id="myTrigger" class="org.springframework.sche易做图ng.quartz.CronTriggerBean">
12         <property name="jobDetail" ref="myTaskMethod"/>
13         <property name="cronExpression" value="0 0/5 * * *  ?"/>
14     </bean>
15     <bean id="myTaskMethod" class="org.springframework.sche易做图ng.quartz.MethodInvokingJobDetailFactoryBean">
16         <property name="targetObject" ref="mySchedule"/>
17         <property name="targetMethod" value="myTask"/>
18     </bean>
   
    java:
1     public class MySchedule {
2         public void myTask(){
3             System.out.println("do my task at time: " + new Date());
4         }
5     }

2、动态设置:示例,程序运行过程中,后台管理人员指定5个小时后执行一次发短信的任务
    xml:
   
1 <task:scheduler id="taskScheduler" pool-size="10"/>

    java:
 1  public class MySchedulerTest {
 2         private TaskScheduler taskScheduler;
 3        
 4         /**
 5          *在指定的时间内执行一次发短信的操作
 6          */
 7         public void sendMessageDelayedAt(Date delayedTime){
 8             taskScheduler.schedule(new Runnable() {
 9                 @Override
10                 public void run() {
11                     System.out.println("send message at time: " + new Date());
12                 }
13             }, delayedTime);
14         }
15     }

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,