• 项目ITP(五) spring4.0 整合 Quartz 实现任务调度


    版权声明:本文为博主原创文章,未经博主同意不得转载。

    https://blog.csdn.net/u010378410/article/details/26016025


    2014-05-16 22:51 by Jeff Li

    前言

      系列文章:[传送门]

      项目需求:

        二维码推送到一体机上,给学生签到扫描用。然后须要的是 上课前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任务细节

    实战 

      第一步 :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_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>wmuitp</display-name>
          
          <!--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任务调度
        
    复制代码
    <!--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>
    复制代码
      #目标类
        <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 Date());
        }
    
        @Override
        protected void executeInternal(JobExecutionContext arg0)
                throws JobExecutionException {
            this.work();
        }
    }
    复制代码
    #须要继承QuartzJobBean
    
    測试运行结果(这个非常重要 能服众)
    

    总结

      spring quartz

      

    感谢及资源共享

        

        http://url.cn/RzETYu 加入我的群

        

        路上走来一步一个脚印。希望大家和我一起。

        感谢读者!

    非常喜欢你们给我的支持。假设支持,点个赞。

        知识来源: 《spring in action》

  • 相关阅读:
    Linux 开机启动 php socket
    Linux 判断进程是否运行
    应用容器公共免费部署平台
    dos2unix 批量转化文件
    django中多个app放入同一文件夹apps
    django无法同步mysql数据库 Error:1064
    cos migration工具webhook推送
    腾讯云 COS 对象存储使用
    docker nginx 运行后无法访问
    makefile中 = := += 的区别
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10885607.html
  • Copyright © 2020-2023  润新知