Spring整合Quartz,并持久化Job到MySQL

1、创建项目

Spring整合Quartz,并持久化Job到MySQL_第1张图片


2、创建quartz数据库,并导入Quartz的SQL脚本

mysql> use quartz

Database changed
mysql> show tables;
+--------------------------+
| Tables_in_quartz         |
+--------------------------+
| QRTZ_BLOB_TRIGGERS       |
| QRTZ_CALENDARS           |
| QRTZ_CRON_TRIGGERS       |
| QRTZ_FIRED_TRIGGERS      |
| QRTZ_JOB_DETAILS         |
| QRTZ_LOCKS               |
| QRTZ_PAUSED_TRIGGER_GRPS |
| QRTZ_SCHEDULER_STATE     |
| QRTZ_SIMPLE_TRIGGERS     |
| QRTZ_SIMPROP_TRIGGERS    |
| QRTZ_TRIGGERS            |
+--------------------------+
11 rows in set (0.00 sec)

mysql> 

3、maven依赖

    
        
            org.springframework
            spring-context-support
            4.1.7.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.1.7.RELEASE
        

        
            org.quartz-scheduler
            quartz
            2.2.1
        

        
            mysql
            mysql-connector-java
            5.1.25
        

        
            org.springframework
            spring-test
            4.1.7.RELEASE
        
        
            junit
            junit
            4.11
            test
        
    

4、实现Job

package hello;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

public class DemoJob extends QuartzJobBean {
    private String hello;

    public void setHello(String hello) {
        this.hello = hello;
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("Job executing……");
        System.out.println("hello " + hello);
        try {
            SchedulerContext context = jobExecutionContext.getScheduler().getContext();
            List list = (List) context.get("list");
            for (Object item : list)
                System.out.println(item.getClass().toString() + " - " + item);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

5、在quartz_data.xml文件配置DemoJob的执行规则,每5秒执行一次




    
        
            DemoJob
            hello.DemoJob
            true
            true
            
                
                    hello
                    world
                
            
        
        
            
                DemoTrigger
                DemoJob
                0/5 * * * * ?
            
        
    


6、配置Spring的beans.xml




    
        
        
        
        
        
        
        
        
        
        
        
        
    

    
        
        
        

        
            
                
                    
                        1
                        5
                        9
                    
                
            
        

        
            
                AUTO
                true

                org.quartz.simpl.SimpleThreadPool
                30
                5

                org.quartz.impl.jdbcjobstore.JobStoreTX
                org.quartz.impl.jdbcjobstore.StdJDBCDelegate
                false
                QRTZ_
                false
                60000
            
        
    

7、创建DemoApp

package hello;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApp {
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("beans.xml");
    }
}

8、执行DemoApp,控制台输出如下

Sep 29, 2015 6:10:37 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@607dcb35: startup date [Tue Sep 29 18:10:37 CST 2015]; root of context hierarchy
Sep 29, 2015 6:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Sep 29, 2015 6:10:38 PM com.mchange.v2.log.MLog 
INFO: MLog clients using java 1.4+ standard logging.
Sep 29, 2015 6:10:38 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1.1 [built 15-March-2007 01:32:31; debug? true; trace: 10]
Sep 29, 2015 6:10:38 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 5, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> z8kflt9c6tjqqdwlq9oq|52ecb5eb, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kflt9c6tjqqdwlq9oq|52ecb5eb, idleConnectionTestPeriod -> 60, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false, lastAcquisitionFailureDefaultUser -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1000, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 100, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
Sep 29, 2015 6:10:39 PM org.springframework.context.support.DefaultLifecycleProcessor start
INFO: Starting beans in phase 2147483647
Sep 29, 2015 6:10:39 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
INFO: Starting Quartz Scheduler now
Job executing……
hello world
class java.lang.Integer - 1
class java.lang.Integer - 5
class java.lang.Integer - 9
Job executing……
hello world
class java.lang.Integer - 1
class java.lang.Integer - 5
class java.lang.Integer - 9
Job executing……
hello world
class java.lang.Integer - 1
class java.lang.Integer - 5
class java.lang.Integer - 9
……

9、至此,已经整合完成,并能够正常运行。另外补充一点,也可以将quartz_data.xml中的配置,也放在beans.xml中,并去掉quartz_data.xml文件;不过Job多的情况下,会使得beans.xml中配置的内容多而乱。参考如下:




    
        
        
        
        
        
        
        
        
        
        
        
        
    

    
        
        
        

        
            
                
                    
                        1
                        5
                        9
                    
                
            
        

        
            
                AUTO
                true

                org.quartz.simpl.SimpleThreadPool
                30
                5

                org.quartz.impl.jdbcjobstore.JobStoreTX
                org.quartz.impl.jdbcjobstore.StdJDBCDelegate
                false
                QRTZ_
                false
                60000
            
        

        
            
                
            
        

        
            
                
            
        
    
    
    
        
        
        
    

    
        
        
    

你可能感兴趣的:(整合)