jms - 如何为 JMS 消息设置 Content-Type header

标签 jms ibm-mq mq

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

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

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

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

这是可以在代码中完成的事情吗?或者它实际上是在队列设置中配置的?

最佳答案

属性名称“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" .

关于jms - 如何为 JMS 消息设置 Content-Type header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022453/

相关文章:

java - JBoss EAP6 + HornetQ - 不确定如何创建到 HornetQ 的队列连接

java - 如何在使用 apache qpid 时处理 jms 消息传递中的生产者流控制

c# - IBM MQ.NET 异步发送消息并提交()

xml - Websphere 消息代理 : Accessing XML elements in ESQL

jms - 查看 JMS 调用中的所有 websphere MQ 消息

java - 未找到架构 : jms 的组件

javax.jms.JMSException : No ManagedConnections available within configured blocking timeout

spring - MQ SSL 错误,协议(protocol)被禁用或密码套件不合适

java - JMS QueueConnectionFactory 与 ConnectionFactory

message-queue - MQ 和 MQTT 有什么区别?