Quartz与spring的简单整合__亲测

Quartz是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。

Quartz是一个任务日程管理系统,一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
Quartz用一个小Java库发布文件(.jar文件),这个库文件包含了所有Quartz核心功能。这些功能的主要接口(API)是Scheduler接口。它提供了简单的操作,例如:将任务纳入日程或者从日程中取消,开始/停止/暂停日程进度。

一般使用Quartz是和spring框架整合使用的,所以加入以下依赖:

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

Quartz框架的核心对象
Scheduler – 核心调度器
Job – 任务
JobDetail – 任务描述
Trigger – 触发器

1、任务在调度中心注册
2、任务调度器调用触发器触发任务
在这里插入图片描述

在这里插入图片描述

spring整合配置

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <!--4.2配置Job到spring容器-->
    <bean id="schedule1" class="com.sgcc.fsp.manage.web.util.Schedule"></bean>

    <!--4.3将Job类配置JobDetail-->
    <bean id="SpringQtzJobMethod"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="schedule1" />
        </property>
        <property name="targetMethod">  <!-- 要执行的方法名称 -->
            <value>execute</value>
        </property>
    </bean>
    <!--4.4配置调度触发器-->
    <bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
        <property name="jobDetail" ref="SpringQtzJobMethod"></property>
        <property name="cronExpression" value="0/1 * * * * ?"></property>
    </bean>
    <!--配置调度工厂-->
    <bean id="SpringJobSchedulerFactoryBean"
          class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerFactoryBean" />
            </list>
        </property>
    </bean>
</beans>

任务触发后,相当于异步调用__自动启用线程执行、观察发现:10个线程在来回切换_
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/e_shi_yi_p/article/details/84656749
今日推荐