动态SendTo批注 [英] Dynamic SendTo annotation
问题描述
我有以下Java代码,用于从队列接收消息,对其进行处理并将已处理的消息写入另一个队列.
I have Java code as following to receive messages form a queue, process it and write processed message to another queue.
@RabbitListener(queues = "rawFusion")
@SendTo("Fusion")
public String receiverFusion(Object _message) {
String message = getMessage(_message);
return messageParser.parse(message);
}
在这里,我总是从"rawFusion"队列中获取消息,并将其写入"Fusion"队列中.
Here i always get messages from "rawFusion" queue and write it into "Fusion" queue.
我要做的是根据某些条件将消息写入不同的队列.因此,我想在收到来自"RawFusion"队列的消息后决定@SendTo
参数(或者可能不使用@SendTo
).
The thing I want to do is, writing messages different queues depending on some conditions. So i would like decide @SendTo
parameter (or maybe without using @SendTo
) after i receive message from the "RawFusion" queue.
有什么想法吗?
预先感谢
推荐答案
如果发件人设置了replyTo
消息属性(队列名称或exchange/routkingKey
),则容器将使用该属性. @SendTo
仅在消息中没有replyTo
时使用.
If the sender sets the replyTo
message property (either a queue name or exchange/routkingKey
), the container will use that. The @SendTo
is only used if there's not replyTo
in the message.
如果您不能更改发送者,则在使用@RabbitListener时,@SendTo
可以包含SpEL表达式,但是它没有请求中的任何上下文,我怀疑您需要智能地路由答复.
If you can't change the sender, when using @RabbitListener, the @SendTo
can contain a SpEL expression but it doesn't have any context from the request, which I suspect you would need to intelligently route the reply.
您可以将某些内容存储在ThreadLocal中并使用
You could store something in a ThreadLocal and use
@SendTo("#{someBean.someMethod()}")
找回它.
我将打开一个JIRA问题,看看我们是否可以提出一种为评估提供一些上下文(例如输入参数)的机制.
I will open a JIRA issue to see if we can come up with a mechanism to provide some context (e.g. input arguments) to the evaluation.
编辑
从1.6版开始, @SendTo可以是SpEL表达式,可以在运行时根据请求和回复进行评估. @SendTo("!{'some.reply.queue.with.' + result.queueName}")
这篇关于动态SendTo批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!