Cairne.Chan
Cairne.Chan

Reputation: 81

Apache Camel -- Websphere MQ integration

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());

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>

and

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;
    }
}

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).

The received JMSMessageID is ID:414d512043532e51412e434253412e511987055902cc6222, which can be parsed to the messy string above.

Upvotes: 1

Views: 1908

Answers (2)

Cairne.Chan
Cairne.Chan

Reputation: 81

I drill down camel code and find the root casue

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;
}

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.

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.

Upvotes: 1

Roger
Roger

Reputation: 7456

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).

The received JMSMessageID is "ID:414d512043532e51412e434253412e511987055902cc6222", which can be parsed to the messy string above.

What you are seeing is correct. The MsgId is a byte array of 24 bytes. It is made up of BOTH string and binary values. Hence, you CANNOT use it as a String.

Upvotes: 0

Related Questions