在Spring Integration中使用CachingConnectionFactory时,JMS本地事务无法正常工作

时间:2017-12-19 15:07:24

标签: java spring spring-integration ibm-mq spring-integration-dsl

我正在使用CachingConnectionFactory来缓存我的IBM MQ JMS代理连接。即使我抛出显式异常,也不会回滚本地事务下的消息。但是当我删除缓存并仅使用普通的JMS连接工厂时,如果抛出异常,则会回滚该消息。 @Gary Russell指出我们可以简单地抛出异常来回滚事务中的任何消息。 Gary's Answer

编辑: 这是如何设置我的JMS Broker:

@Bean
public IntegrationFlow primaryInitiationListenerFlow() {
      return IntegrationFlows.from(Jms
                .messageDrivenChannelAdapter(
                        context.getBean("connection" + environment.getProperty("primaryConnection"), ConnectionFactory.class),
                        DefaultMessageListenerContainer.class)
                .autoStartup(false)
                .destination(environment.getProperty("sourceName"))
                .configureListenerContainer(listenerContainerSpec -> listenerContainerSpec
                        .destinationResolver((session, destinationName, pubSubDomain) -> destinationName.toUpperCase().endsWith("TOPIC") ? 
                    session.createTopic(destinationName) : session.createQueue(destinationName))
                        .subscriptionDurable(false))
                .id(environment.getProperty("id") + "PrimaryIn")
                .get())
            .channel("idEnrichmentChannel")
            .get();
}

@Bean
public ConnectionFactory connection301() {
    MQConnectionFactory factory = new MQConnectionFactory();
    try {
        factory.setHostName("xxxxxxx");
        factory.setPort(1416);
        factory.setQueueManager("xxxxxxx");
        factory.setChannel("xxxxxxx");
        factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
    } catch (JMSException e) {
        e.printStackTrace();
    }
    return factory;
}

这是我使用CachingConnectionFactory的配置:

@Bean
public JmsTemplate template301() {
    CachingConnectionFactory cachedConnection = new CachingConnectionFactory();
    cachedConnection.setTargetConnectionFactory(connection301());
    return new JmsTemplate(cachedConnection); 
}

这是删除CachingConnectionFactory:

@Bean
public JmsTemplate template301() {
    return new JmsTemplate(connection301());
}

0 个答案:

没有答案