177.20. 在目的地上设置 JMS 提供程序选项

某些 JMS 提供程序(如 IBM 的 WebSphere MQ)需要在 JMS 目的地上设置选项。例如,您可能需要指定 targetClient 选项。由于 targetClient 是 WebSphere MQ 选项而不是 Camel URI 选项,因此您需要在 JMS 目标名称上设置该选项,如下所示:

// ...
.setHeader("CamelJmsDestinationName", constant("queue:///MY_QUEUE?targetClient=1"))
.to("wmq:queue:MY_QUEUE?useMessageIDAsCorrelationID=true");

某个版本的 WMQ 不会在目标名称上接受此选项,您会收到例外情况:

com.ibm.msg.client.jms.DetailedJMSException: JMSCC0005: The specified
value 'MY_QUEUE?targetClient=1' is not allowed for
'XMSC_DESTINATION_NAME'

临时解决方案是使用自定义 DestinationResolver:

JmsComponent wmq = new JmsComponent(connectionFactory);

wmq.setDestinationResolver(new DestinationResolver() {
    public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) throws JMSException {
        MQQueueSession wmqSession = (MQQueueSession) session;
        return wmqSession.createQueue("queue:///" + destinationName + "?targetClient=1");
    }
});