tags:

views:

21

answers:

1

Hi All,

In my application I am trying to send e-mails to our customers when some "event occurs" for eg. like when we receive a payment or a new customer sign in...

I am successfully able to capture to those events and I am trying to send the mail and here comes the problem I am using JMS to have all this sending mail functionality in the back ground..

So when a new customer signs up I am saving its details in DB and an event is generated. Now to send mail I need to access the information of customer (or any other domain object which is relevant) using the code below

protected Map getObjectsMap(Map domainMap){
    Map objectsMap = [:]
    domainMap.each{key,value->
        def dc = grailsApplication.domainClasses.find{it.getFullName().equals(key)}
       // println "dc=$dc; dump=${dc.dump()}" // org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass

        def obj = dc.clazz.get(value)
        //println "value=$value return=$obj; dc.clazz=$dc.clazz; dump=${dc.clazz.dump()}"
        if(!obj) {
            log.error "Could not find object of type $key with id=$value"
            //def session = sessionFactory.currentSession
            //println "session=$session; dump=${session?.dump()}"

        }
        String objectName = key.substring(key.lastIndexOf(".")+1)
        objectName = objectName.charAt(0).toLowerCase().toString() + objectName.substring(1)
        objectsMap.put(objectName, obj)
    }
    return objectsMap
}

The problem is that this code runs on my local machine (which is slower) but not on production server.

I think what is happening is that this background thing is getting executed earlier than object getting saved to DB. (I do have .save(flush:true) when I save the instance). Here is the stack trace which I get

biz.MailService Could not find object of type customer.Customer with id=5
biz.MailService Could not find object of type customer.Customer with id=5
biz.MailService Could not find object of type payment.Payment with id=7
biz.MailService Could not find object of type payment.Payment with id=7
Mail$$EnhancerByCGLIB$$25d09edf.send Exception raised in message listener
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method 'send' threw exception; nested exception is java.lang.AssertionError: Could not find the org property. Dumping objectMap: [customer:null, payment:null]. Dumping domainMap: [com.blusynergy.blubilling.customer.Customer:5, com.blusynergy.blubilling.payment.Payment:7]. Expression: org. Values: org = null
    at org.springframework.jms.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:471)
    at grails.plugin.jms.listener.adapter.LoggingListenerAdapter.super$2$invokeListenerMethod(LoggingListenerAdapter.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1049)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:923)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:127)
    at grails.plugin.jms.listener.adapter.PersistenceContextAwareListenerAdapter.invokeListenerMethod(PersistenceContextAwareListenerAdapter.groovy:29)
    at org.springframework.jms.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:355)
    at grails.plugin.jms.listener.adapter.LoggingListenerAdapter.super$2$onMessage(LoggingListenerAdapter.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1049)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:923)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:127)
    at grails.plugin.jms.listener.adapter.LoggingListenerAdapter.onMessage(LoggingListenerAdapter.groovy:33)
    at grails.plugin.jms.listener.adapter.PersistenceContextAwareListenerAdapter.super$3$onMessage(PersistenceContextAwareListenerAdapter.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1049)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:923)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:127)
    at grails.plugin.jms.listener.adapter.PersistenceContextAwareListenerAdapter.onMessage(PersistenceContextAwareListenerAdapter.groovy:18)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:534)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:495)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:323)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:261)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:977)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:969)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:871)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.AssertionError: Could not find the org property. Dumping objectMap: [customer:null, payment:null]. Dumping domainMap: [com.blusynergy.blubilling.customer.Customer:5, com.blusynergy.blubilling.payment.Payment:7]. Expression: org. Values: org = null
    at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:379)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:662)
    at com.blusynergy.blubilling.biz.MailService.send(MailService.groovy:185)
    at com.blusynergy.blubilling.biz.MailService$$FastClassByCGLIB$$88e80e6a.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
    at com.blusynergy.blubilling.biz.MailService$$EnhancerByCGLIB$$25d09edf.send(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
    at org.springframework.jms.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:463)
    ... 43 more

Any Ideas on how I can solve this problem..??

With Regards.

A: 

Are you creating the objects in side a transaction?

If so, make sure you send the message after the transaction completes.

If not, maybe implement some kind of retry system.

cheers

Lee

leebutts