Quartz 定时任务使用 —— 集成Spring使用XML配置(四)

Quartz 定时任务使用

Quartz for Spring集成源码

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="default" class="org.apache.log4j.ConsoleAppender">
    <param name="target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/>
    </layout>
  </appender>

 <logger name="org.quartz">
   <level value="info" />
 </logger>

  <root>
    <level value="info" />
    <appender-ref ref="default" />
  </root>

</log4j:configuration>

HelloJob.java

配置成一个普通的java类即可

package com.anson.spring.demo.demo1;

public class HelloJob {

    public void execute(){
        System.out.println("Spring配置 - Hello Job");
    }

}

application.xml

在此配置中,设置每5秒执行一次job任务

<?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">

    <!-- 配置Job类 -->
    <bean id="helloJob" class="com.anson.spring.demo.demo1.HelloJob"></bean>

    <!-- 配置JobDetail -->
    <bean id="springQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 执行目标job -->
        <property name="targetObject" ref="helloJob"></property>

        <!-- 要执行的方法 -->
        <property name="targetMethod" value="execute"></property>
    </bean>

    <!-- 配置tirgger触发器 -->
    <bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- jobDetail -->
        <property name="jobDetail" ref="springQtzJobMethod"></property>

        <!-- cron表达式,执行时间 -->
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>

    <!-- 配置调度工厂 -->
    <bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerFactoryBean"></ref>
            </list>
        </property>

    </bean>
</beans>

除了使用CronTrigger也可以使用SimpleTrigger配置,示例代码如下

<bean id="simpleTrigger"  class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    <!--配置具体要执行的jobDetail-->
    <property name="jobDetail" ref="simpleJobDetail" />
    <!--初始延迟时间 1s-->
    <property name="startDelay" value="1000" />
    <!--间隔执行时间每2s执行一次-->
    <property name="repeatInterval" value="2000" />
</bean>

至于Spring或SpringMVC,不在本章重点梳理,主要关注点在Job配置

启动web项目后,日志输出

INFO] 09 九月 08:44:33.344 下午 RMI TCP Connection(2)-127.0.0.1 [org.springframework.web.context.ContextLoader]
Root WebApplicationContext: initialization started
[INFO] 09 九月 08:44:33.460 下午 RMI TCP Connection(2)-127.0.0.1 [org.springframework.web.context.support.XmlWebApplicationContext]
Refreshing Root WebApplicationContext: startup date [Sat Sep 09 20:44:33 CST 2017]; root of context hierarchy
[INFO] 09 九月 08:44:33.520 下午 RMI TCP Connection(2)-127.0.0.1 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
Loading XML bean definitions from class path resource [applicationContext.xml]
[INFO] 09 九月 08:44:33.985 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.impl.StdSchedulerFactory]
Using default implementation for ThreadExecutor
[INFO] 09 九月 08:44:34.001 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.core.SchedulerSignalerImpl]
Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
[INFO] 09 九月 08:44:34.001 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.core.QuartzScheduler]
Quartz Scheduler v.2.2.3 created.
[INFO] 09 九月 08:44:34.004 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.simpl.RAMJobStore]
RAMJobStore initialized.
[INFO] 09 九月 08:44:34.005 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.core.QuartzScheduler]
Scheduler meta-data: Quartz Scheduler (v2.2.3) 'springJobSchedulerFactoryBean' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
[INFO] 09 九月 08:44:34.005 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler 'springJobSchedulerFactoryBean' initialized from an externally provided properties instance.
[INFO] 09 九月 08:44:34.005 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler version: 2.2.3
[INFO] 09 九月 08:44:34.007 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.core.QuartzScheduler]
JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@56122ef6
[INFO] 09 九月 08:44:34.013 下午 RMI TCP Connection(2)-127.0.0.1 [org.springframework.context.support.DefaultLifecycleProcessor]
Starting beans in phase 2147483647
[INFO] 09 九月 08:44:34.013 下午 RMI TCP Connection(2)-127.0.0.1 [org.springframework.scheduling.quartz.SchedulerFactoryBean]
Starting Quartz Scheduler now
[INFO] 09 九月 08:44:34.013 下午 RMI TCP Connection(2)-127.0.0.1 [org.quartz.core.QuartzScheduler]
Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED started.
[INFO] 09 九月 08:44:34.018 下午 RMI TCP Connection(2)-127.0.0.1 [org.springframework.web.context.ContextLoader]
Root WebApplicationContext: initialization completed in 673 ms
[2017-09-09 08:44:34,056] Artifact quartz_example:war exploded: Artifact is deployed successfully
[2017-09-09 08:44:34,056] Artifact quartz_example:war exploded: Deploy took 2,012 milliseconds
Spring配置 - Hello Job
Spring配置 - Hello Job
09-Sep-2017 20:44:41.741 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/Users/liurenkui/myLibrary/apache-tomcat-8.5.15/webapps/manager]
09-Sep-2017 20:44:41.807 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/liurenkui/myLibrary/apache-tomcat-8.5.15/webapps/manager] has finished in [63] ms
Spring配置 - Hello Job
Spring配置 - Hello Job
Spring配置 - Hello Job
......
......
Spring配置 - Hello Job
Spring配置 - Hello Job
Spring配置 - Hello Job
/Users/liurenkui/myLibrary/apache-tomcat-8.5.15/bin/catalina.sh stop
Disconnected from the target VM, address: '127.0.0.1:52159', transport: 'socket'
09-Sep-2017 20:47:44.932 信息 [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
09-Sep-2017 20:47:44.933 信息 [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
09-Sep-2017 20:47:44.990 信息 [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
Spring配置 - Hello Job
09-Sep-2017 20:47:45.045 信息 [main] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
[INFO] 09 九月 08:47:45.052 下午 localhost-startStop-2 [org.springframework.web.context.support.XmlWebApplicationContext]
Closing Root WebApplicationContext: startup date [Sat Sep 09 20:44:33 CST 2017]; root of context hierarchy
[INFO] 09 九月 08:47:45.052 下午 localhost-startStop-2 [org.springframework.context.support.DefaultLifecycleProcessor]
Stopping beans in phase 2147483647
[INFO] 09 九月 08:47:45.054 下午 localhost-startStop-2 [org.quartz.core.QuartzScheduler]
Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED paused.
[INFO] 09 九月 08:47:45.054 下午 localhost-startStop-2 [org.springframework.scheduling.quartz.SchedulerFactoryBean]
Shutting down Quartz Scheduler
[INFO] 09 九月 08:47:45.054 下午 localhost-startStop-2 [org.quartz.core.QuartzScheduler]
Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED shutting down.
[INFO] 09 九月 08:47:45.054 下午 localhost-startStop-2 [org.quartz.core.QuartzScheduler]
Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED paused.
[INFO] 09 九月 08:47:45.054 下午 localhost-startStop-2 [org.quartz.core.QuartzScheduler]
Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED shutdown complete.
09-Sep-2017 20:47:45.068 信息 [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
09-Sep-2017 20:47:45.069 信息 [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
09-Sep-2017 20:47:45.079 信息 [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
09-Sep-2017 20:47:45.079 信息 [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
Disconnected from server

在调度器中,有一个lazy-init参数

如果lazy-init='false',则容器启动时就会执行调度程序;

如果lazy-init='true',则需要实例化该bean才能执行调度程序;

<bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true">

更多内容待学习。。。


未经允许请勿转载:程序喵 » Quartz 定时任务使用 —— 集成Spring使用XML配置(四)

点  赞 (0) 打  赏
分享到: