【问题标题】:How to setup ContentType for Azure ServiceBus from Spring JMS如何从 Spring JMS 为 Azure ServiceBus 设置 ContentType
【发布时间】:2021-05-02 09:55:45
【问题描述】:

我正在尝试使用库 azure-servicebus-jms-spring-boot-starter 向主题发送消息。一切正常,但是消息以application/xml 类型存储在订阅中,我找不到如何正确设置它以将它们存储为application/json 的方法。 我尝试将消息转换器配置为按照here 的描述发送ContentType,但这也不起作用。

  @Bean
  public MessageConverter jacksonJmsMessageConverter() {
    final MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(){
      @Override
      protected TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter)
          throws JMSException, IOException {
        final TextMessage message = super.mapToTextMessage(object, session, objectWriter);
        message.setStringProperty("ContentType", "application/json");
        return message;
      }
    };
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    converter.setObjectMapper(objectMapper);
    return converter;
  }

【问题讨论】:

  • Content-Type 需要存储为系统属性,以便 SB 正确识别。 setStringProperty() 似乎只是设置了一个用户属性。问题是:如何在 JMS 中编写系统属性。没有很快找到东西
  • 改用content_type
  • content_type 也不起作用:/
  • @bilak 你是如何设置 ContentType 的。你能让它工作吗?
  • @SathishMurugesan 见here

标签: azure jms azureservicebus spring-jms


【解决方案1】:

没有公开的方法来设置从 Qpid JMS 客户端发送的消息的内容类型。客户端本身使用此字段作为 JMS 映射到 AMQP 的一部分,以区分它发送的某些消息类型,并在接收时确定某些消息应该呈现为什么。

在技术上可以使用反射来获取值,但是您必须从 JmsMessageFacade 类中使用的 API 不是公开的,并且可能随任何版本而更改,因此选择这样做会带来很大的风险。

【讨论】:

猜你喜欢
  • 2021-05-02
  • 2018-07-11
  • 2020-05-17
  • 2021-03-26
  • 2020-12-15
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多