1. 程式人生 > >spring整合quartz

spring整合quartz

  • 引入maven依賴
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId
>
<artifactId>quartz-jobs</artifactId> <version>2.2.3</version> </dependency>

2.建立作業(任務)類,建立若干任務方法

@Component("myJob")
public class MyJob {

    @Autowired
    private WorkBillDao workBillDao;

    public void sendMail(){

3.在spring配置檔案配置quartz定時任務

<!--配置任務詳情 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myJob"></property>
    <property name="targetMethod" value="sendMail"></property>
</bean>
<!-- 配置觸發器,任務執行週期 -->
<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetail"></property> <property name="cronExpression"> <value> 0 0/2 * * * ? </value> </property> </bean> <!-- 配置排程工廠 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="trigger"/> </list> </property> </bean>