Grails集成了quartz jobs示例代码和所需的配置

时间:2011-09-20 02:33:37

标签: grails cluster-computing quartz-scheduler

我正在使用带有Grails 1.3.7的石英插件。我需要加载平衡/群集使用石英作业的服务器应用程序。显然这是支持但我发现所有谷歌搜索结果和文档中的链接都被破坏了。我发现了一些原始的Java示例,但我认为Grails有更粗略的方法来做到这一点。我需要的只是一个用作模板的简单示例。我知道我需要以某种方式启用quartz来使用JDBC来存储作业和管理锁定。

我认为单个样本的链接可以实现。但实际上,每当我发现一些看起来很有前途的东西时,它就会指出兵马俑网站上的链接断了。几乎每个网站最终都会把我带到这里:http://www.opensymphony.com/quartz/wikidocs/TutorialLesson9.html但是当我看到兵马俑的网站时,我看到了Java的东西,但没有grails。如果Java是唯一的方法,那么就这样吧,但我觉得必须在某处有一些grails专业知识!

TIA。

2 个答案:

答案 0 :(得分:13)

要在Grails中集成Quartz插件,您需要在项目中包含一些文件。首先,安装grails-app/conf/QuartzConfig.groovy并确保已启用jdbcStore

quartz {
    autoStartup = true
    jdbcStore = true
    waitForJobsToCompleteOnShutdown = true
}

接下来,安装与要连接的数据库相关的Hibernate配置文件。例如,对于Oracle,grails-app/conf/hibernate/hibernate.cfg.xml的基本Hibernate xml配置为:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
'-//Hibernate/Hibernate Configuration DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>

<session-factory>
    <mapping resource="Quartz.oracle.hbm.xml"/>
</session-factory>

</hibernate-configuration>

此示例的实际Quartz-Hibernate SQL文件将命名为Quartz.oracle.hbm.xml,并将驻留在同一目录中。这些文件应该在src/templates/sql下的GitHub(https://github.com/nebolsin/grails-quartz)上的Quartz插件中提供。请注意,这些脚本似乎只适用于DataSource createcreate-drop,因此您需要在update上手动创建Quartz表,如果它们尚不存在以前的运行。

创建grails-app/conf/quartz/quartz.properties文件,编辑以满足您的业务需求:

/* Have the scheduler id automatically generated for
 * all schedulers in a cluster */
org.quartz.scheduler.instanceId = AUTO
/* Don't let Quartz "Phone Home" to see if new versions
 * are available */
org.quartz.scheduler.skipUpdateCheck = true

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
/* Configure Quartz for only one thread as the only job
 * should run once per day */
org.quartz.threadPool.threadCount = 4
/* Give the thread a Thread.MIN_PRIORITY level*/
org.quartz.threadPool.threadPriority = 1

/* Allow a minute (60,000 ms) of non-firing to pass before 
 * a trigger is called a misfire */
org.quartz.jobStore.misfireThreshold = 60000
/* Handle only 2 misfired triggers at a time */
org.quartz.jobStore.maxMisfiresToHandleAtATime = 2
/* Check in with the cluster every 5000 ms*/
org.quartz.jobStore.clusterCheckinInterval = 5000

/* Use the Oracle Quartz Driver to communicate via JDBC */
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
/* Have Quartz handle its own transactions with the database */
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

/* Define the prefix for the Quartz tables in the database*/
org.quartz.jobStore.tablePrefix = QRTZ_
/* Tell Quartz it is clustered */
org.quartz.jobStore.isClustered = true
/* Tell Quartz that properties passed to the job call are
 * NOT all String objects */
org.quartz.jobStore.useProperties = false

/* Detect the jvm shutdown and call shutdown on the scheduler */
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true

/* Log the history of triggers and jobs */
org.quartz.plugin.triggerHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.jobHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin

请注意,从上述属性中,您可以在org.quartz.plugins的Log4j设置中设置Config.groovy以记录相关作业并触发触发信息。我认为info级别应该足够了。

编辑或创建scripts/_Events.groovy并添加以下war修改闭包。这修复了一个已知的Quartz插件错误,将正确的quartz.properties安装到最终的war文件中,而不是插件中的空白插件。

eventCreateWarStart = { warName, stagingDir ->
    // Make sure we have the correct quartz.properties in the
    // correct place in the war to enable clustering
    ant.delete(dir:"${stagingDir}/WEB-INF/classes/quartz")
    ant.copy(file:"${basedir}/grails-app/conf/quartz/quartz.properties",
        todir:"${stagingDir}/WEB-INF/classes")
}

你应该完成......

P.S。如果您使用的是Oracle数据库,请将以下内容添加到依赖项块中的BuildConfig.groovy,以便您可以访问Quartz-Oracle通信驱动程序:

runtime("org.quartz-scheduler:quartz-oracle:1.7.2") {
    // Exclude quartz as 1.7.3 is included from the plugin
    excludes('quartz')
}

P.P.S上面链接中的sql文件只是SQL。要使其进入休眠文件,只需用Hibernate database-object节点包围每个单独的SQL命令,就像这样(再次使用Oracle示例):

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
    'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>

<hibernate-mapping>

    <database-object>
        <create>
        CREATE TABLE QRTZ_JOB_DETAILS (
        JOB_NAME VARCHAR2(200) NOT NULL,
        JOB_GROUP VARCHAR2(200) NOT NULL,
        DESCRIPTION VARCHAR2(250) NULL,
        JOB_CLASS_NAME VARCHAR2(250) NOT NULL,
        IS_DURABLE VARCHAR2(1) NOT NULL,
        IS_VOLATILE VARCHAR2(1) NOT NULL,
        IS_STATEFUL VARCHAR2(1) NOT NULL,
        REQUESTS_RECOVERY VARCHAR2(1) NOT NULL,
        JOB_DATA BLOB NULL,
        PRIMARY KEY (JOB_NAME,JOB_GROUP)
        )
        </create>
        <drop>DROP TABLE QRTZ_JOB_DETAILS</drop>
        <dialect-scope name='org.hibernate.SomeOracleDialect' />
    </database-object>
...
    <database-object>
        <create>INSERT INTO QRTZ_LOCKS VALUES('TRIGGER_ACCESS')</create>
        <drop></drop>
        <dialect-scope name='org.hibernate.SomeOracleDialect' />
    </database-object>
...
</hibernate-mapping>

dialect-scope告诉Hibernate哪个数据库方言应该使用创建和删除节点。您可以尝试将其删除,看看它是否有效,否则您可能需要添加Grails数据源使用的MySql方言。

答案 1 :(得分:0)

接受的答案有点过时了。有关更新版Grails的更简单解决方案,请参阅此问题:Using grails datasources in quartz plugin