Using Grail Quartz Plugin Without Hibernate

I am working on Grails software that periodically displays information from a RESTful service. To do this, I installed the Grails Quartz plugin.

grails install-plugin quartz

Then I created a task using

grails create-job My

which creates the MyJob file that I configured with cron trigger

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

Running the application locally in the dev environment works correctly, however, as soon as I try to build a test or production war, I get the following exception when the trigger fires.

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

Since I do not need a database, I tried to remove the Hibernate plugin as suggested , but I get compilation problems after removing the Hibernate plugin:

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;  
...

Quartz Hibernate?
, Quartz?
( - .)

+3
5

, 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"
    }
  }
}
...

, "create-drop" "mem" "file".

0

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'
    }
}

Quartz - SessionBinderJobListener Hibernate . NOP :

import org.quartz.listeners.JobListenerSupport

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

Spring bean .groovy:

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

, , quartz-0.4.1\src\java\org\codehaus\groovy\grails\plugins\quartz\listeners\SessionBinderJobListener.java:1

quartz plugin . , lib? compile, maven/gradle

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

, , , - ( R.I.P.):


/* 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 (nebolsin@gmail.com)
 * 
 * @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)
  {
  }

}

: - "", . :

  • , quartz,

  • ${grailsSettings.projectPluginsDir} ( grails compile , grails war), , .

  • hack

  • .

0

1.0.RC7 Quartz grails . @DavidTinker.

resources.groovy Nop factory 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
}  

No-op , Quartz, ( ).

class FooService {     
   static transactional = false

I also had to exclude hibernate transitive dependencies, but since I use gradle to create a project, it looks like this:

// 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'
}
0
source

Source: https://habr.com/ru/post/1733328/


All Articles