在没有 Hibernate 的情况下使用 grails Quartz 插件
我正在开发一个后端 Grails 应用程序,该应用程序定期从 RESTful 服务中提取信息。为此,我安装了 Grails Quartz 插件。
grails install-plugin quartz
然后,我创建了一个作业,使用
grails create-job My
它生成一个 MyJob 文件,我使用 cron 触发器配置
static triggers = {
cron name: 'myTrigger', cronExpression: '0 0 * * * ?' // hourly
}
该文件在开发环境中本地运行应用程序工作正常,但是一旦我尝试构建测试或生产战争,运行触发器时会出现以下异常。
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 插件 按照建议< /a>, 但是一旦删除 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 使用吗?(我不关心任何这些数据的持久性。)
I am working on a backend Grails application that pulls information periodically from a RESTful service. To do this I installed the Grails Quartz plugin.
grails install-plugin quartz
I then created a job using
grails create-job My
which geneates a MyJob file which I configured with a cron trigger
static triggers = {
cron name: 'myTrigger', cronExpression: '0 0 * * * ?' // hourly
}
Running the application locally in the dev environment works correctly, however once I try to build a testing or production war I get the following exception when the trigger is run.
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
As I don't require a database, I tried removing the Hibernate plugin as suggested,
but I get compilation problems once the Hibernate plugin has been removed:
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;
...
Is there any way to use the Quartz plugin without the Hibernate plugin?
If not, would the best idea be to configure an in-memory database for Quartz to use? (I'm not concerned with the persistence of any of this data.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我让 Quartz 插件(0.4.2)在没有 Hibernate 插件和编辑 Quartz 插件的情况下工作得相当干净。
在 BuildConfig.groovy 中添加对 Hibernate 的运行时依赖项只是为了拉入 jar:
Quartz 仍然安装 SessionBinderJobListener 将 Hibernate 会话绑定到作业线程。像这样创建一个 NOP 会话绑定器:
并在 resources.groovy 中创建一个 Spring bean:
I got the Quartz plugin (0.4.2) working fairly cleanly without the Hibernate plugin and without editing the Quartz plugin.
Add a runtime dependency on Hibernate in BuildConfig.groovy just to pull in the jars:
Quartz still installs a SessionBinderJobListener to bind a Hibernate session to the job thread. Create a NOP session binder like this:
And create a Spring bean in resources.groovy:
似乎存在代码依赖,在
quartz-0.4.1\src\java\org\codehaus\groovy\grails\plugins\quartz\listeners\SessionBinderJobListener.java:1
然后就不能编译没有 hibernate 类的 quartz 插件。也许你可以将它们放在
lib
文件夹中?或者如果您使用 maven/gradle 将其添加为compile
依赖项Seems that there are code dependency, at
quartz-0.4.1\src\java\org\codehaus\groovy\grails\plugins\quartz\listeners\SessionBinderJobListener.java:1
And then you can't compile
quartz plugin
without hibernate classes. Maybe you can put them inlib
folder? or add it ascompile
dependency if you use maven/gradle我已经通过安装 Hibernate 插件并配置内存数据库来成功实现此功能。在 DataSource.groovy 中,
更改是在测试上设置“create-drop”生产数据库并将生产数据库设置为“mem”而不是“file”。
I've managed to get this working by leaving the Hibernate plugin installed and configuring the in-memory database. In DataSource.groovy
The change was to set "create-drop" on the test & production databases and set the production database to 'mem' instead of 'file'.
所以,
这是我想出的解决方案(请注意,我一开始就不愿意保持休眠状态)。该解决方案使用 Grails 1.2.1 和quartz 插件 0.4.1 以及 hibernate 擦除常规方式(
grails uninstall-plugin hibernate
)进行了测试。保存在内存数据库中也不是我能找到的最佳选择。创建或编辑
scripts/_Events.groovy
:现在为了补偿文件删除(它是从
GrailsQuartzPlugin.groovy
中的某处引用的),我们需要在以下位置创建该类的“安全”版本:项目,又名src/java/org/codehaus/groovy/grails/plugins/quartz/listeners/SessionBinderJobListener.java
这是我放在那里保留所有原始版权和作者完好无损 - 但没有留下休眠(让它 RIP):
警告:解决方案是“hack”,它会覆盖插件中的一些文件要删除 hack:
make。确保卸载quartz插件,
转到
${grailsSettings.projectPluginsDir}
(位置在每次运行grails编译期间由hack打印
和更高级别的脚本,包括grails war
),并确保没有遗留任何工件删除两个黑客工件
重新安装新的quartz插件。
So,
Here is the solution I came up with (please note that I was unhappy to keep hibernate at the first place). The solution tested with Grails 1.2.1 and quartz plugin 0.4.1 with hibernate wiped-out regular way (
grails uninstall-plugin hibernate
). Keeping in memory database is also not very best option I could find.Create or edit
scripts/_Events.groovy
:Now to compensate file deletion (it's referred from somewhere in
GrailsQuartzPlugin.groovy
, we need to create "safe" version of the class in project, aka atsrc/java/org/codehaus/groovy/grails/plugins/quartz/listeners/SessionBinderJobListener.java
this is what I put there keeping all original copyrights and author intact - but no hibernate left (let it R.I.P.) :
Warning: The solution is "hack" which overrides some files from plugin. To remove hack:
make sure you uninstall quartz plugin,
go to
${grailsSettings.projectPluginsDir}
(location is printed by hack during each run ofgrails compile
and higher-'level' scripts includinggrails war
) and make sure no artifacts left.Remove both hack artifacts
Reinstall fresh quartz plugin.
对于 1.0.RC7 版本的 Quartz grails 插件,我必须添加一些额外的步骤。它基于 @DavidTinker 的回应。
在 resources.groovy 中,我必须添加一个 Nop 会话工厂和一个 Nop 会话(确保它实现 org.hibernate.classic.Session)
NopSessionFactory.groovy:
而且,就我而言,我需要添加一个 No-op 事务管理注入到我的 Quartz 作业中的任何服务,或者将服务标记为非事务性的(我选择这样做)。
我还必须排除 hibernate 传递依赖项,但由于我使用 gradle 来构建项目,所以它看起来像这样:
For the 1.0.RC7 version of the Quartz grails plugin, I had to add some additional steps. It's based on @DavidTinker's response.
In resources.groovy, I had to add a Nop session factory and a Nop Session (make sure it's implementing org.hibernate.classic.Session)
NopSessionFactory.groovy:
And, in my case, I either need to add a No-op Transaction manager for any services injected into my Quartz jobs, or mark the services as non-transactional (which I chose to do).
I also had to exclude the hibernate transitive dependencies, but since I'm using gradle to build the project, it looked like this: