Jslfl【软件开发技术笔记】

Quartz作业调度(二)

spring配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- 不设autowire="no"配置有dataSource时有可能启动时要出错 -->
<bean id="testJobSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
       <property name="triggers">
           <list>
              <ref bean="testJobCronTrigger"/><!-- 触发器列表 -->
           </list>
       </property>
       <property name="autoStartup" value="true"/>
</bean>

<bean id="testJobCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
       <property name="jobDetail" ref="testJobDetail"/>
       <property name="cronExpression" value="*/3 * * * * ?"/><!-- 每隔3秒钟触发一次 -->
</bean>

<bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="testJob"/>
        <property name="targetMethod" value="run"/>
        <property name="concurrent" value="false"/><!-- 并发,false必须等到前一个线程处理完毕后才再启新线程 -->
</bean>

<bean id="testJob" class="sk.tms.warnmanage.tmsalarmstatistics.task.TestJob"/><!-- 普通java类,run方法业务操作(对应testJobDetail-targetMethod) -->

java作业业务:

1
2
3
4
5
6
7
8
public class TestJob{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public void run() {
        Date exe = new Date();
        String exetime = dateFormat.format(exe);
        System.out.println(exetime + "作业调度操作...");
    }
}

, , ,

Comments are currently closed.