Quartz丨整合Spring XML方式

Code

  • 坐标
<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>5.0.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>5.0.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.quartz-scheduler</groupId>
		<artifactId>quartz</artifactId>
		<version>2.2.1</version>
	</dependency>
	<dependency>
		<groupId>org.quartz-scheduler</groupId>
		<artifactId>quartz-jobs</artifactId>
		<version>2.2.1</version>
	</dependency>
</dependencies>
  • Application.java
public static void main(String[] args) {
	new ClassPathXmlApplicationContext("spring-jobs.xml");
}
  • spring-jobs.xml
<?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:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc.xsd
						http://code.alibabatech.com/schema/dubbo
						http://code.alibabatech.com/schema/dubbo/dubbo.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注册自定义Job -->
    <bean id="ClearImageJob" class="com.aivsok.jobs.ClearImageJob"></bean>

    <!-- 注册JobDetail,作用是负责通过反射调用指定的Job -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 注入目标对象 -->
        <property name="targetObject" ref="ClearImageJob"/>
        <!-- 注入目标方法 -->
        <property name="targetMethod" value="run"/>
    </bean>

    <!-- 注册一个触发器,指定任务触发的时间 -->
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 注入JobDetail -->
        <property name="jobDetail" ref="jobDetail"/>
        <!-- 指定触发的时间,基于Cron表达式  10秒一次 -->
        <property name="cronExpression">
            <value>0/10 * * * * ?</value>
        </property>
    </bean>

    <!-- 注册一个统一的调度工厂,通过这个调度工厂调度任务 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 注入多个触发器 -->
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
                <!--<ref bean="1"/>-->
                <!--<ref bean="2"/>-->
            </list>
        </property>
    </bean>
</beans>
  • ClearImageJob.java
public class ClearImageJob {
    public void run() {
        System.out.println("定时任务,走起~~~!" + new Date());
    }
}

参考

https://gitee.com/aivsok/aivsok-quartz-dev/tree/master

posted @ 2021-10-14 14:17  aivsok9  阅读(33)  评论(0编辑  收藏  举报