Spring整合Quartz实现定时任务

时间:2021-08-30 23:24:30

简单Demo:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>project_demo</artifactId>
        <groupId>com.zy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_quartz</artifactId>
    <packaging>war</packaging>
    <properties>
        <spring.version>4.2.4.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--quartz-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!--spring集成quartz-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8080</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

可以看到上面依赖的jar包有spring-jdbc,不知为何需要依赖这个jar包,但是不依赖的话会报错,莫非是quartz支持动态从数据库中添加定时任务,不管你用不用都需要依赖jdbc的jar包?有道友知道原因的话还请告知。

 

配置文件spring-quartz.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--SchedulerFactory 任务调度工厂-->
    <bean id="demoFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!--Trigger触发器 可设置多个-->
        <property name="triggers">
            <list>
                <ref bean="cronTriggerBean"/>
                <!--<ref bean="simpleTriggerBean"></ref>-->
            </list>
        </property>
    </bean>

    <!--Trigger 触发器-->
    <!--CronTrigger-->
    <bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!--JobDetail 任务信息-->
        <property name="jobDetail" ref="springQtzJobDetail"></property>
        <!--Cron表达式配置-->
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>

    <!--SimpleTrigger-->
    <!--<bean id="simpleTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">-->
    <!--&lt;!&ndash;JobDetail 任务信息&ndash;&gt;-->
    <!--<property name="jobDetail" ref="springQtzJobMethod"></property>-->
    <!--<property name="startDelay" value="0"/>-->
    <!--<property name="repeatInterval" value="2000"/>-->
    <!--</bean>-->


    <!-- 定义目标bean和bean中的方法 -->
    <bean id="springQuartz" class="com.zy.spring_quartz.SpringQuartz"/>
    <!--JobDetail 任务信息-->
    <bean id="springQtzJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!--目标bean-->
        <property name="targetObject">
            <ref bean="springQuartz"/>
        </property>
        <!--要执行的方法名称-->
        <property name="targetMethod">
            <value>work</value>
        </property>

        <!--是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程-->
        <property name="concurrent" value="false"/>
    </bean>

</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-quartz.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

 

具体业务类

/**
 * 定时任务具体业务类 普通类即可
 */
public class SpringQuartz {
    public void work() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        // 当前时间
        System.out.println("执行时间:" + sf.format(new Date()));
    }
}

 

java实现定时任务的几种方式