您好,我在我的 Grails 应用程序中使用 RabbitMQ,并且正在寻找延迟监听器启动的方法。它们现在在我的应用程序的整个连接完成之前启动,所以我想在启动后启动它们。关于如何做到这一点有什么建议吗?
我正在为此使用 xml 配置..
<rabbit:listener-container connection-factory="connectionFactory" message converter="jsonMessageConverter" acknowledge="auto">
<rabbit:listener queue-names="log.soap.in" ref="logSoapInListener" />
</rabbit:listener-container>
因此,当我的 grails 应用程序启动时,上下文也会启动,但需要一段时间才能连接好所有内容。因此,在我的应用程序启动期间,监听器已被调用但无法正确处理消息。
class LogSoapListenerService implements MessageListener {
LogSoapService logSoapService
JsonMapper jsonMapper
@Override
void onMessage(Message message) {
String body = new String(message.body, message.properties.contentEncoding ?: "UTF-8")
logSoapService.process(body)
}
}
最佳答案
我遇到了一个类似的问题,这就是我最终添加到 RabbitmqGrailsPlugin 文件中的内容,我的项目中有一个本地版本,
在 doWithApplicationContext
方法内部,我改变了,
// Now that the listener is properly configured, we can start it.
bean.start()
到
// Now that the listener is properly configured, we can start it.
def startDelay = application.config.rabbitmq.startDelay ?: 0
new Timer().runAfter(startDelay, {
bean.start()
})
然后您只需配置 rabbitmq.startDelay
来设置您想要的任何延迟。
一个更优雅的解决方案是确保 rabbitmq 在其他插件之后加载,但 Grails 似乎对此没有很好的支持。参见 Can I control plugin load order from outside a plugin in Grails?
另一种解决方案(仅当您在本地安装了插件时才可用)是指定插件在导致问题的任何插件之后加载,
例如,
def loadAfter = ['services']
成为
def loadAfter = ['services', 'converters', 'mail']
关于grails - 如何在应用程序启动后启动 RabbitMQ 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11029279/