zoukankan      html  css  js  c++  java
  • quartz 执行定时任务配置及案例

    准备工作
    导入jar包:WEB-INFlibquartz-1.8.5.jar

    web.xml中的配置如下

    <!-- quartz job start -->
        <context-param>
            <param-name>quartz:wait-on-shutdown</param-name>
            <param-value>true</param-value>
        </context-param>
        <servlet>     
            <servlet-name>QuartzInitializer</servlet-name>     
            <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>     
            <init-param>     
                <param-name>config-file</param-name> 
                <param-value>config/quartz_jobs.properties</param-value>     
            </init-param>     
            <init-param>     
                <param-name>shutdown-on-unload</param-name>     
                <param-value>true</param-value>     
            </init-param>    
            <init-param>     
                <!-- Start delay time 60 seconds -->
                <param-name>start-delay-seconds</param-name>     
                <param-value>60</param-value>     
            </init-param>
            <init-param>     
                <param-name>start-scheduler-on-load</param-name>     
                <param-value>true</param-value>     
            </init-param>   
            <load-on-startup>     
                 5    
            </load-on-startup>     
           </servlet>
    <!-- quartz job end -->

    web.xml中配置的config/quartz_jobs.properties文件内的详细内容如下

    #============================================================================
    
    # Configure Main Scheduler Properties  
    
    #============================================================================
    
    org.quartz.scheduler.instanceName = TestScheduler
    
    org.quartz.scheduler.instanceId = SchedulerTID
    
    #============================================================================
    
    # Configure ThreadPool  
    
    #============================================================================
    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    
    org.quartz.threadPool.threadCount = 3
    
    org.quartz.threadPool.threadPriority = 5 
    
    #============================================================================
    
    # Configure JobStore  
    
    #============================================================================
    
    org.quartz.jobStore.misfireThreshold = 60000
    
    org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
    #org.quartz.jobStore.class = org.quartz.simpl.JDBCJobStore
    #org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX   
    #org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate   
    #org.quartz.jobStore.useProperties = false  
     
    #config dataSource
    #org.quartz.jobStore.dataSource = myDS
    #table prefix
    #org.quartz.jobStore.tablePrefix = QRTZ_
    #setting true Activate cluster characteristics
    #org.quartz.jobStore.isClustered = false
    #org.quartz.jobStore.maxMisfiresToHandleAtATime=1  
    #============================================================================
    # Configure Datasources connect
    #============================================================================
    #one 
    #org.quartz.dataSource.myDS.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
    ##jdbc:microsoft:sqlserver://CANGZDWSQL10;DatabaseName=COMPUPTC
    #org.quartz.dataSource.myDS.URL = jdbc:microsoft:sqlserver://CANGZDWSQL10:1433;DatabaseName=COMPUPTC
    #org.quartz.dataSource.myDS.user = fvpuser
    #org.quartz.dataSource.myDS.password = fvpuser
    #org.quartz.dataSource.myDS.maxConnections = 5
    # tow jndi
    #dataSource的JNDI url
    #org.quartz.dataSource.myDS.jndiURL = java:comp/env/jdbc/app4-hdcp3r7ccmp01-fvp
    ## used JNDI InitialContextFactory class name.
    #org.quartz.dataSource.myDS.java.naming.factory.initial = 
    ##JNDI URL
    #org.quartz.dataSource.myDS.java.naming.provider.url = 
    ##JNDI user name
    #org.quartz.dataSource.myDS.java.naming.security.principal = fvpuser
    ##connect to JNDI
    #org.quartz.dataSource.myDS.java.naming.security.credentials = fvpuser
    
    #============================================================================
    
    # Configure Plugins 
    
    #============================================================================
    
    org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
    # v1.8 before JobInitializationPlugin 
    #org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
    org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
    # /WEB-INF/config/quartz_jobs.xml,/WEB-INF/config/quartz_savejobs.xml
    org.quartz.plugin.jobInitializer.fileNames = /WEB-INF/quartzjobs.xml
    
    #org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
    
    org.quartz.plugin.jobInitializer.failOnFileNotFound = true
    
    org.quartz.plugin.jobInitializer.scanInterval = 10
    
    org.quartz.plugin.jobInitializer.wrapInUserTransaction = false


    quartz_jobs.properties文件内的/WEB-INF/quartzjobs.xml 详细配置 主要是指定去定时执行那个类 请看配置如下

    <?xml version='1.0' encoding='utf-8'?>
    <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
      version="1.8">
       
        <schedule>
                    
            <job>
                <name> ws_soap_job</name>
                <group>ErrLog</group>
                <description>The job description </description>
                <job-class>com.gmw9.quartz.WsSoapQuartzJob</job-class>
            </job>
            <trigger>
                <cron>
                    <name>wsSoap-trigger</name>
                    <group>ErrLog_GROUP</group>
                    <job-name>ws_soap_job</job-name>
                    <job-group>ErrLog</job-group>
                    <cron-expression>0 0 8,12,16,20 * * ?</cron-expression>
                  
                </cron> 
            </trigger>       
            
            <!-- 
             <job>
                <name>jabName</name>
                <group>upload</group>
                <description>The job description </description>
                <job-class>com.gmw9.quartz.hongshanQuartzJob</job-class>
            </job>
            <trigger>
                <cron>
                    <name>uploadFile-trigger</name>
                    <group>upload_GROUP</group>
                    <job-name>jabName</job-name>
                    <job-group>upload</job-group>
                    <cron-expression>0 10 15 22 4 ? 2016</cron-expression>
                </cron> 
            </trigger> -->
            
        </schedule> 
    </job-scheduling-data>

    被执行类的案例代码如下

    package com.gmw9.quartz;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class WsSoapQuartzJob implements Job {
    
        private final String JOBNAME = Constant.JOBNAME;
        private final static Log log = LogFactory.getLog(WsSoapQuartzJob.class);
    
        //定时任务框架会调用这个方法
        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            System.out.println("IVS wsSoap quartz job launch time ..."
                    + System.currentTimeMillis());
            
                this.wsSoapCall();
            
        }
        //这里就是定时执行的内容
        public void wsSoapCall(){
            System.out.println("========wsSoap Call=======");
        }
    
    }
  • 相关阅读:
    Tensor:Pytorch神经网络界的Numpy
    你真的懂语音特征吗?
    ES高级(17) 使用基础(5)安装(5) Linux 集群
    ES高级(16) 使用基础(4)安装(4) Linux 单机
    ES高级(15) 使用基础(3)安装(3) Windows 集群
    ES高级(14) 使用基础(2)安装(2) 概念
    ES入门 (13)Java API 操作(4)DQL(1) 请求体查询/term 查询,查询条件为关键字/分页查询/数据排序/过滤字段/Bool 查询/范围查询/模糊查询/高亮查询/聚合查询/分组查询
    ES入门 (12)Java API 操作(3)DML 新增文档/修改文档/查询文档/删除文档/批量操作
    ES入门 (11)Java API 操作(2)DDL 索引操作
    ES入门 (10)Java API 操作(1)准备
  • 原文地址:https://www.cnblogs.com/nmdzwps/p/6767298.html
Copyright © 2011-2022 走看看