2010-02-18 69 views
3

我正在從後端Grails應用程序中定期從RESTful服務中獲取信息。爲此,我安裝了Grails Quartz插件。在沒有休眠的情況下使用grails Quartz插件

grails install-plugin quartz 

我然後創建使用

grails create-job My 

這geneates我與一個cron觸發

static triggers = { 
    cron name: 'myTrigger', cronExpression: '0 0 * * * ?' // hourly 
} 

配置在開發環境本地運行的應用程序工作正常一個MyJob文件中的作業,但是,一旦我嘗試構建測試或生產戰爭,觸發器運行時會出現以下異常。

2010-02-18, 00:04:32 ERROR org.codehaus.groovy.grails.web.context.GrailsContextLoader - Error occurred shutting down plug-in manager: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzScheduler': 
Cannot resolve reference to bean 'sessionBinderListener' while setting bean property 'jobListeners' with key [0]; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionBinderListener': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException : Access is denied: Session is closed 

正如我不需要數據庫,我試圖消除Hibernate插件as suggested, 但我得到編譯問題一旦Hibernate插件已被刪除:

Running script C:\Downloads\grails-1.2.1\scripts\RunApp.groovy 
Environment set to development 
[groovyc] Compiling 18 source files to C:\Projects\myapp\target\classes 
[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Compile error during compilation with javac. 
[groovyc] ...\myapp\plugins\quartz-0.4.1\src\java\org\codehaus\groovy\grails\plugins\quartz\listeners\SessionBinderJobListener.java:19: package org.hibernate does not exist 
[groovyc] import org.hibernate.FlushMode; 
... 

有什麼辦法在沒有Hibernate插件的情況下使用Quartz插件?
如果沒有,最好的辦法是配置Quartz的內存數據庫來使用?
(我不關心的此類數據的持久性。)

回答

0

我設法通過安裝Hibernate插件和配置內存數據庫得到這個工作。在DataSource.groovy的

... 
environments { 
    development { 
    dataSource { 
     dbCreate = "create-drop" // one of 'create', 'create-drop','update' 
     url = "jdbc:hsqldb:mem:myDevDb" 
    } 
    } 
    test { 
    dataSource { 
     dbCreate = "create-drop" 
     url = "jdbc:hsqldb:mem:myTestDb" 
    } 
    } 
    production { 
    dataSource { 
     dbCreate = "create-drop" 
     url = "jdbc:hsqldb:mem:myProdDb;shutdown=true" 
    } 
    } 
} 
... 

的變化是設置在測試&生產數據庫「創造降」和生產數據庫設置爲「MEM」而不是「文件」。

0

似乎有代碼的依賴,在quartz-0.4.1\src\java\org\codehaus\groovy\grails\plugins\quartz\listeners\SessionBinderJobListener.java:1

然後你就不能沒有Hibernate類編譯quartz plugin。也許你可以把它們放在lib文件夾中?或者添加它作爲compile依賴如果你使用maven/gradle

+0

感謝splix,如果我捆綁在Hibernate罐子然後我可以保留Hibernate插件。我設法通過確保所有數據庫都在內存中來實現它,請參閱我的答案。 – 2010-02-18 11:39:20

0

所以,

這裏是我想出了(請注意,我就不高興了,以保持休眠放在第一位)的解決方案。該解決方案使用Grails 1.2.1和石英插件0.4.1進行測試,並使用休眠擦除常規方式(grails uninstall-plugin hibernate)。保持內存數據庫也不是我能找到的最佳選擇。

創建或編輯scripts/_Events.groovy

eventCompileStart = {bindings-> 
println "Compile Start: Plugin dir is ${grailsSettings.projectPluginsDir}" 
// Kill standard listener which actually wants to use hibernate (to restore hibernate session)! 
Ant.delete(file:"${grailsSettings.projectPluginsDir}/quartz-0.4.1/src/java/org/codehaus/groovy/grails/plugins/quartz/listeners/SessionBinderJobListener.java") 
} 

我們補償文件刪除(它是從某處GrailsQuartzPlugin.groovy稱,我們需要src/java/org/codehaus/groovy/grails/plugins/quartz/listeners/SessionBinderJobListener.java

創建項目類的「安全」版本,又名

這是我放在那裏保留所有原始版權和作者完好 - 但沒有休眠留下(讓它RIP):


/* Copyright 2006-2008 the original author or authors. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
package org.codehaus.groovy.grails.plugins.quartz.listeners; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
import org.quartz.listeners.JobListenerSupport; 

/** 
* JobListener implementation which binds Hibernate Session to thread before 
* execution of job and flushes it after job execution. 
* 
* @author Sergey Nebolsin ([email protected]) 
* 
* @since 0.2 
*/ 
public class SessionBinderJobListener extends JobListenerSupport 
{ 
    private static final transient Log LOG = LogFactory.getLog(SessionBinderJobListener.class); 

    public static final String NAME = "sessionBinderListener"; 

    public String getName() 
    { 
    return NAME; 
    } 

    @Override 
    public void jobToBeExecuted(final JobExecutionContext context) 
    { 
    } 

    @Override 
    public void jobWasExecuted(final JobExecutionContext context, final JobExecutionException exception) 
    { 
    } 

} 

警告:的解決方案是 「黑客」,從插件會覆蓋一些文件。要刪除黑客:

  • 確保您卸載插件石英,

  • ${grailsSettings.projectPluginsDir}(位置由黑客grails compile每次運行和更高'level中印有」腳本包括grails war)和確保沒有遺留物。

  • 取出兩個劈工件

  • 重新安裝新鮮石英插件。

+1

嘿瓦迪姆,看起來像很多脆弱的黑客讓Grails在沒有Hibernate的情況下運行Quartz。感謝分享,特別是破解黑客!對我來說,運行內存數據庫是一個更簡單的解決方案:) – 2010-02-21 11:06:52

4

我得到了Quartz插件(0.4.2)工作相當乾淨,沒有Hibernate插件,也沒有編輯Quartz插件。

添加運行時依賴基於Hibernate在BuildConfig.groovy只是在罐子拉:

dependencies { 
    ... 
    // the Quartz plugin depends on some Hibernate classes being available 
    runtime('org.hibernate:hibernate-core:3.6.7.Final') { 
     exclude group:'commons-logging', name:'commons-logging' 
     exclude group:'commons-collections', name:'commons-collections' 
     exclude group:'org.slf4j', name:'slf4j-api' 
     exclude group:'xml-apis', name:'xml-apis' 
     exclude group:'dom4j', name:'dom4j' 
     exclude group:'antlr', name:'antlr' 
    } 
} 

石英還是安裝到一個Hibernate會話綁定工作線程SessionBinderJobListener。創建一個NOP會話粘結劑是這樣的:

import org.quartz.listeners.JobListenerSupport 

class NopSessionBinderJobListener extends JobListenerSupport { 
    String getName() { return "sessionBinderListener" } 
} 

,並創建resources.groovy一個Spring bean:

beans = { 
    ... 
    // dummy session binder to work around issue with Quartz requiring Hibernate 
    sessionBinderListener(NopSessionBinderJobListener) { } 
} 
0

對於1.0.RC7版本石英Grails的插件,我不得不添加一些額外的腳步。它基於@DavidTinker的迴應。

在resources.groovy,我不得不添加一個NOP會話工廠和NOP會議(確保它的實現org.hibernate.classic.Session)

beans = { 
    // dummy session binder to work around issue with Quartz requiring Hibernate 
    sessionBinderListener(NopSessionBinderJobListener) { } 
    sessionFactory(NopSessionFactory) {} 
} 

NopSessionFactory.groovy:

class NopSessionFactory implements SessionFactory 
{ 

    @Override 
    Session openSession() throws HibernateException { 
     return new NopSession() 
    } 
// Implement all of the methods required by this interface and return a NopSession for any method that returns a session 
} 

而且,就我而言,我需要爲注入到Quartz作業中的任何服務添加No-op事務管理器,或將服務標記爲非事務性(我選擇這樣做)。

class FooService {  
    static transactional = false 

我也不得不排除休眠傳遞依賴,但因爲我使用gradle這個建設項目,它是這樣的:

// Needed by quartz 
runtime('org.hibernate:hibernate-core:3.6.10.Final') { 
    exclude group:'commons-logging', module:'commons-logging' 
    exclude group:'commons-collections', module:'commons-collections' 
    exclude group:'org.slf4j', module:'slf4j-api' 
    exclude group:'xml-apis', module:'xml-apis' 
    exclude group:'dom4j', module:'dom4j' 
    exclude group:'antlr', module:'antlr' 
} 
相關問題