spring4.0 整合 Quartz 实现任务调度(一)

て烟熏妆下的殇ゞ 提交于 2019-11-28 23:02:56

前言

  项目需求:

    二维码推送到一体机上,给学生签到扫描用。然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过。自然 quartz 是首选。所以我就配置了下,搞了个小样例给大家。

正文 

  spring4.0 整合 Quartz 实现任务调度。这是期末项目的最后一篇,剩下到暑假吧。

Quartz 介绍

    Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; 

    Quartz框架是一个全功能、开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型的电子商务系统。Quartz可以执行上千上万的任务调度。

 

   核心概念

     Quartz核心的概念:scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节

     相关文档:http://www.quartz-scheduler.org/documentation/quartz-2.2.x/quick-start

实战 

  第一步 :spring、quartz 相应的jar包,添加到项目中(需要的call me)

    /WEB-INF/lib/quartz-2.2.1.jar

    以及spring的一些必要包

  

    第二步:web.xml中配置spring

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>quartz</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <!--Spring WebApplicationContext上下文,称为父上下文(父容器) -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--Srping <listener> <listener-class> org.springframework.web.context.request.RequestContextListener 
        </listener-class> </listener> -->
    <!--加载spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--Spring MVC 配置 DispatcherServlet -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--filter配置,解决编码问题 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--OpenSessionInViewFilter配置,解决延迟加载时Session会关闭的问题 -->
    <!-- <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->

    <!-- session过期时间: 20 -->
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

    <!-- 错误界面 -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/error/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>400</error-code>
        <location>/WEB-INF/error/400.jsp</location>
    </error-page>

</web-app>

 #有些你不用的,就不要写了。

 

    第三:在spring配置文件中配置quartz任务调度(applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<!--Quartz-->
            
    <!-- 集成方式:JobDetailFactoryBean,并且任务类需要继承QuartzJobBean-->
    <!-- 定义jobDetail -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <!-- durability 表示任务完成之后是否依然保留到数据库,默认false   -->
        <property name="durability" value="true" />  
        <!--     目标类  /wmuitp/src/test/SpringQuartz.java-->
        <property name="jobClass" value="test.SpringQuartzTest"></property>
        
           <!--  在这个例子中,jobDataAsMap没有用,此目标类中接受的参数 ,若参数为service,则可以在此进行参数配置,类似struts2 -->
           <!--
        <property name="jobDataAsMap">  
            <map>  
                <entry key="service"><value>simple is the beat</value></entry>  
            </map>  
        </property>
            -->
    </bean>
    
    <!-- 定义simpleTrigger触发器 -->
    <!--     
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="repeatCount">
            <value>8</value>
        </property>
        <property name="repeatInterval">
            <value>1000</value>
        </property>
        <property name="startDelay">
            <value>4</value>
        </property>
    </bean> 
    -->
    
    <!-- 另一种触发器是CornTrigger -->
     <bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
       <property name="jobDetail" ref="jobDetail"/>
       <!-- 每个10秒触发 -->
       <property name="cronExpression" value="0/10 * * * * ?"/>
    </bean> 
    
    <!-- 定义核心调度器 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
        <ref bean="cornTrigger"/>
      </property>
    </bean>
    
 </beans>


#目标类

<property name="jobClass" value="test.SpringQuartzTest"></property>
 下面第四步:编写目标类
package test;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SpringQuartzTest extends QuartzJobBean{
    
    /*业务实现*/
    public void work() {
//        System.out.println("执行调度任务:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.fff").format(new Date()+"开始执行"));
        System.out.println("执行调度任务:"+new Date());
    }

    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        this.work();
    }

}
#需要继承QuartzJobBean
测试运行结果(这个很重要 能服众)


需要源码的可以留言给我,一定奉上。还请多多指教

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!