Apache Camel -- Websphere MQ 集成 [英] Apache Camel -- Websphere MQ integration
问题描述
我有一个使用 apache-camel 解决方案的应用程序,想通过 jms 向 Websphere MQ 服务器发送消息,将 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,我为camel 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;
}
}
我可以在接收器上获取 JMS_IBM_MQMD_MsgId
,同时将 mdReadEnabled
设置为 true.但是,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.
推荐答案
我钻研camel代码并找到根本原因
I drill down camel code and find the root casue
设置jms属性时,会运行org.apache.camel.component.jms.JmsBinding
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;
}
似乎camel拒绝字节数组值并返回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 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!