JMS listener starts before Hibernate is configured when server starts

I have a grails 2.2 application that uses a JMS plugin (using version 1.3).

My situation is that when my server starts up, the JMS plugin is initialized and the Listener service grabs any pending messages in the queue before the server finishes configuring.

Specifically, it handles the first hibernate request in code and fails with the following error:

| Error 2014-10-14 11:06:56,535 [ruleInputDataListenerJmsListenerContainer-1] ERROR drms.RuleInputDataListenerService  - Message Exception: Failed to process JMS Message.
groovy.lang.MissingMethodException: No signature of method: au.edu.csu.drms.Field.executeQuery() is applicable for argument types: () values: []
Possible solutions: executeQuery(java.lang.String), executeQuery(java.lang.String, java.util.Collection), executeQuery(java.lang.String, java.util.Map), executeQuery(java.lang.String, java.util.Collection, java.util.Map), executeQuery(java.lang.String, java.util.Map, java.util.Map)

      

Correct code:

String query = "SELECT f FROM field f WHERE (attributeName = :attributeName AND entityName = :entityName)"
def fieldList = Field.executeQuery(query, [attributeName: _attributeName, entityName: _entityName]) 

      

From what I can tell, this is a hibernate issue that is not initialized when the JMS listener executes the method onMessage

. This also happens with withCriteria

the hibernation method or any other method.

This only happens if there are messages in the queue when the server starts and there are failures for each message waiting. Once the queue is complete and is processing new messages, it works fine.

Is there a way to either initialize hibernation in time or delay execution of the listener service (like the Quartz plugin with a start delay timer)?

Update:

I am not using bean config because this is a daemon type application - we cannot define beans.

Is there a way to use @DependsOn and make my listener depend on the hibernate itself?

+3


source to share


2 answers


Unfortunately, the notation @DependsOn

doesn't work due to the nature of my application (no bean config).

Considering there are several bugs / problems with the JMS Grails plugin, the solution to my problem was to use the following code before processing the JMS message:



def onMessage(msg) {
    try {
        Rule.withNewTransaction {
            log.info("Hibernate is up and running!")
        }
    } catch (Exception e) {
        resendMessage(msg)
    }

    // rest of code...
}

      

Where I use transaction for testing, if Hibernate is fully initialized (already verified that this is not the case when the JMS listener is triggered at startup) and if it catches an exception it will post the message back to the queue for reprocessing.

0


source


Let's say you have the following EntityManagerFactory configuration:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaDialect" ref="jpaDialect"/>
</bean>

      



You need your JMS connection factory to depend on entityManagerFactory:

<bean id="jmsConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
      destroy-method="stop" depends-on="jmsBroker, entityManagerFactory">
    <property name="connectionFactory" ref="activeMQConnectionFactory"/>
</bean>

      

+1


source







All Articles