无法从 JMS 侦听器获取域对象
在我的应用程序中,我尝试在某些“事件发生”时向我们的客户发送电子邮件,例如。就像当我们收到付款或新客户登录时...
我成功地能够捕获这些事件,并且我正在尝试发送邮件,但问题来了,我正在使用 JMS 在背景..
因此,当新客户注册时,我将其详细信息保存在数据库中,并生成一个事件。现在要发送邮件,我需要使用下面的代码访问客户(或任何其他相关的域对象)的信息。
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
}
问题是该代码在我的本地计算机上运行(速度较慢),但不在生产服务器上运行。
我认为正在发生的事情是这个后台事件的执行时间早于对象保存到数据库的时间。 (当我保存实例时,我确实有 .save(flush:true) )。这是堆栈跟踪,我
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
对如何解决这个问题有任何想法..??
谨致问候。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在事务中创建对象?
如果是这样,请确保在交易完成后发送消息。
如果没有,也许实施某种重试系统。
干杯
李
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