问题描述
我有一个使用apache-camel解决方案的应用程序,并且想通过jms将消息发送到Websphere MQ Server,将jms属性JMS_IBM_MQMD_MsgId
转换为MQMD字段MQMD.MsgId
,以便我通过骆驼在消息上设置此值
I have an application using apache-camel solution, and would like to send message to Websphere MQ Server through jms, convert jms property JMS_IBM_MQMD_MsgId
to MQMD field MQMD.MsgId
, so that I set this value on message through camel
exchange.getIn().setHeader(WMQConstants.JMS_IBM_MQMD_MSGID, "XXXXXXXXXXXXXXXXXXXXXXXX".getBytes());
根据 Apache Camel-IBM MQ集成,看来我们需要在目标对象上进行其他属性设置.在 http://camel.apache上,参考在目标上设置JMS提供程序选项. org/jms.html ,我为骆驼jms组件提供了一个自定义的DestinationResolver,为目标对象设置了mdWriteEnabled
,mdReadEnabled
.
According to Apache Camel - IBM MQ integration, seems we need another properties setting on destination object. Reference Setting JMS Provider Options on the Destination on http://camel.apache.org/jms.html, I provide a custom DestinationResolver for camel jms component, set mdWriteEnabled
, mdReadEnabled
for destination object.
<bean id="ibmMQServer01" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory" ref="ibmMQCredentialConnectionFactory01" />
<property name="destinationResolver" ref="wmqDestinationResolver" />
</bean>
和
public class WMQDestinationResolver implements DestinationResolver {
@Override
public Destination resolveDestinationName(Session session, String destinationName,
boolean pubSubDomain) throws JMSException {
MQSession mqSession = (MQSession) session;
MQQueue queue = (MQQueue) mqSession.createQueue("queue:///" + destinationName);
queue.setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
queue.setBooleanProperty(WMQConstants.WMQ_MQMD_READ_ENABLED, true);
queue.setIntProperty(WMQConstants.WMQ_MQMD_MESSAGE_CONTEXT, WMQConstants.WMQ_MDCTX_SET_ALL_CONTEXT);
return queue;
}
}
当设置mdReadEnabled
等于true时,我可以在接收器上获取JMS_IBM_MQMD_MsgId
.但是,mdWriteEnabled
似乎对我不起作用,并且我得到JMS_IBM_MQMD_MsgId
作为意外值AMQ CS.QA.CBSA.Q�Y�b
(从byte []解析,总共24个字节).
I can get JMS_IBM_MQMD_MsgId
on receiver while setting mdReadEnabled
equals true. However, mdWriteEnabled
seems not works for me, and I get JMS_IBM_MQMD_MsgId
as an unexpected value AMQ CS.QA.CBSA.Q�Y�b
(been parsed from byte[], totally 24 bytes).
接收到的JMSMessageID
是ID:414d512043532e51412e434253412e511987055902cc6222
,可以将其解析为上面的混乱字符串.
The received JMSMessageID
is ID:414d512043532e51412e434253412e511987055902cc6222
, which can be parsed to the messy string above.
推荐答案
我深入研究骆驼代码并找到根情况
I drill down camel code and find the root casue
设置jms属性时,它将运行org.apache.camel.component.jms.JmsBinding
的方法getValidJMSHeaderValue
While setting jms property, it will run method getValidJMSHeaderValue
of org.apache.camel.component.jms.JmsBinding
protected Object getValidJMSHeaderValue(String headerName, Object headerValue) {
if (headerValue instanceof String) {
return headerValue;
} else if (headerValue instanceof BigInteger) {
return headerValue.toString();
} else if (headerValue instanceof BigDecimal) {
return headerValue.toString();
} else if (headerValue instanceof Number) {
return headerValue;
} else if (headerValue instanceof Character) {
return headerValue;
} else if (headerValue instanceof CharSequence) {
return headerValue.toString();
} else if (headerValue instanceof Boolean) {
return headerValue;
} else if (headerValue instanceof Date) {
return headerValue.toString();
}
return null;
}
似乎骆驼拒绝字节数组值并返回null,因此jms提供程序无法应用JMS_IBM_MQMD_MsgId
的属性.我重写了此方法以解决它.
Seems camel reject byte array value and return null, so that jms provider cannot apply property of JMS_IBM_MQMD_MsgId
. I override this method to sovle it.
注意:我只是在源文件夹src/main/java
中创建相同的类org.apache.camel.component.jms.JmsBinding
,类加载器将默认加载该类,而不是maven库中的类.
Note: I simply create the same class org.apache.camel.component.jms.JmsBinding
in source folder src/main/java
, class loader would default load this class instead of the class from maven library.
这篇关于Apache Camel-Websphere MQ集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!