如何在spring-amqp请求/回复消息中设置回复的内容类型?

时间:2013-06-13 04:54:28

标签: java rabbitmq amqp spring-amqp spring-rabbit

我正在按照Stock Trading示例的说明进行操作,其中概述了如何在spring-amqp中使用请求/回复消息:http://static.springsource.org/spring-amqp/docs/1.2.x/reference/html/sample-apps.html#d4e742

查看ServerHandler类,它是一个消息驱动的POJO,恰好也能发送回复消息......我没有看到我们可以为content-type注入MessageProperties的地方

我已经调整了这个示例,我想返回一个xml消息,冒充一个简单的字符串......但是当ServerHandler将我的回复发布到RabbitMQ时,它看起来像:

properties:
  correlation_id:   9873f420-89e5-465d-aa60-ec9281ee48ae
  priority: 0
  delivery_mode:    2
  headers:  
    __TypeId__: java.lang.String
  content_encoding: UTF-8
  content_type: application/json

payload:
  "<?xml version='1.0' encoding='UTF-8'?>\n<stuff></stuff>

最终(我的猜测是)我的客户端应该正在阅读这个...因为content_type被错误地设置为application/json而无法解析对象而我的用例失败所以我收到了null回复了。

问)有没有人知道如何在spring-amqp请求/回复消息中为回复设置内容类型?


更新#1

问:或许我需要的是确保jsonMessageConverter仅用于读取requestQueue之外的消息,而不是当我想将回复放回responseQueue时...任何关于如何获取的想法配置?这是我当前的appContext.xml文件的片段:

<listener-container
    concurrency="5"
    connection-factory="connectionFactory"
    message-converter="jsonMessageConverter"
    xmlns="http://www.springframework.org/schema/rabbit">
    <listener ref="serverHandler" method="handleMessage" queue-names="#{queue.name}" />
</listener-container>

1 个答案:

答案 0 :(得分:2)

如果您说您正在接收JSON但想要返回XML,则可以使用自定义MessageConverter,该自定义JsonMessageConverter委托入站方SimpleMessageConverter和出站content-type侧;它还可以根据需要设置{{1}}消息属性。

相关问题