Dynamic SendTo annotation

回眸只為那壹抹淺笑 提交于 2019-12-25 05:37:05

问题


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);
    }

Here i always get messages from "rawFusion" queue and write it into "Fusion" queue.

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.

Any idea how to do that?

Thanks in advance


回答1:


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.

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.

You could store something in a ThreadLocal and use

@SendTo("#{someBean.someMethod()}")

to retrieve it.

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.

EDIT

Starting with version 1.6, the @SendTo can be a SpEL expression that is evaluated at runtime against the request and reply. @SendTo("!{'some.reply.queue.with.' + result.queueName}")




回答2:


Unfortunately I cannot make any change on sender's message and it seems SpEL expression does not solve my problem in this case. But I found a workaround. I am not sure if its a good practice but it solves my problem for now.

I used @SendTo("exchange/") i didnt use any routing key. In my message i set a header value like "destination=testQueue" and i created an headers exchange which route my message depending on destination value.

Thanks for your answer and i hope we can have a mechanism that we can determine @SendTo value after we receive the Message.



来源:https://stackoverflow.com/questions/35583708/dynamic-sendto-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!