【问题标题】:How to set Content-Type header for JMS message如何为 JMS 消息设置 Content-Type 标头
【发布时间】:2015-07-13 09:15:18
【问题描述】:

我们有一个通过 IBM WebSphere MQ 发送 JMS 消息的 Java 应用程序。消费者应用程序要求将消息内容类型设置为“application/json”。我该怎么做呢?

我检查了一些参考资料,似乎我可以通过方法“setStringProperty(headerKey, headerName)”设置额外的标题,例如

Message jmsMsg = session.createTextMessage(msgStr);
jmsMsg.setStringProperty("Content-Type", "application/json");

问题是“Content-Type”不是有效的属性键,因为它包含“-”字符。

这是可以在代码中完成的吗?还是真的在队列设置中配置了?

【问题讨论】:

    标签: jms ibm-mq mq


    【解决方案1】:

    属性名称“Content-Type”有一个“-”字符。根据 JMS 规范,属性名称可以包含 Java Character.isJavaIdentifierPart 方法为其返回 true 的任何字符。对于'-' 字符isJavaIdentifierPart 方法返回false。因此,setStringProperty("Content-Type", "application/json") 方法调用失败并出现以下异常。

    com.ibm.msg.client.jms.DetailedMessageFormatException: JMSCC0049: The property name 'Content-Type' is not a valid Java(tm) identifier.
    The supplied property name does not conform to the allowed format described in the JMS specification.
    Check the characters used in the property name and modify as necessary.
    

    如果可以更改接收应用程序,您可以选择"Content_Type"(使用下划线)作为属性名称而不是"Content-Type"

    【讨论】:

    • 我们与接收应用程序进行了讨论,并选择使用“ContentType”作为属性键。
    【解决方案2】:

    作为 SOAP 格式的示例,w3c 需要使用名为“SOAPJMS_contentType”的 JMS 属性 (http://www.w3.org/TR/soapjms/)。看起来标准中没有关于 JSON 格式的内容,但是您可以使用这样的名称。 IBM JMS 库将正确处理此类名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-27
      • 2013-12-10
      • 2015-05-05
      • 2011-01-04
      相关资源
      最近更新 更多