简体   繁体   English

使用spring和activemq的XA事务

[英]XA transactions using spring and activemq

I am trying to prove I don't need the activemq rar deployed to JBoss EAP 6.3 in order to use XA transactions...I'd like to use just the active mq client jar. 我试图证明我不需要为了使用XA事务而部署到JBoss EAP 6.3的activemq rar ...我只想使用活动的mq客户端jar。 I created a simple spring-boot project and exposed a method which gets exposed via a restful web service. 我创建了一个简单的spring-boot项目,并公开了通过静态Web服务公开的方法。 The following code correctly rolls back if I have the active-mq rar deployed. 如果部署了active-mq rar,则以下代码可以正确回滚。

@Transactional
public void work() throws Exception {

    ConnectionFactory connectionFactory = (ConnectionFactory) this.context
            .getBean("jmsConnectionFactory");

    // Send a message
    MessageCreator messageCreator = new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage("Test message!");
        }
    };

    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    System.out.println("Sending a new message.");
    jmsTemplate.send("test-destination", messageCreator);

    throw new Exception("Something bad happened!!");
}

However, when I create my own ConnectionFactory via JNDI, the code doesn't rollback and the message still gets sent. 但是,当我通过JNDI创建自己的ConnectionFactory时,代码不会回滚,并且仍然会发送消息。

@Transactional
public void work() throws Exception {

    ConnectionFactory connectionFactory = null;
    try {
        Context ctx = new InitialContext();
        connectionFactory = (ConnectionFactory) ctx.lookup("java:/ConnectionFactory");
    } catch (NamingException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    // Send a message
    MessageCreator messageCreator = new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage("Test message!");
        }
    };

    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    System.out.println("Sending a new message.");
    jmsTemplate.send("test-destination", messageCreator);

    throw new Exception("Something bad happened!!");
}

What I'd like to understand is what spring-boot is doing at boot time to provide XA support when the activemq rar is deployed as a resource adaptor on EAP. 我想了解的是,当activemq rar被部署为EAP上的资源适配器时,spring-boot在引导时在做什么以提供XA支持。 If I understand that, I think I should be able to just package the active-mq client jar and my database jar in my spring app (not spring-boot based) and still provide XA support ie get spring to manage the XA transactions by delegating to the PlatformTransactionManager. 如果我理解这一点,我认为我应该能够将active-mq客户端jar和数据库jar打包在我的spring应用程序中(而不是基于spring-boot),并且仍然提供XA支持,即让Spring通过委派来管理XA事务。到PlatformTransactionManager。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks, Will 谢谢,威尔

Spring Boot is all about conventions. Spring Boot与约定有关。 You need to indicate Spring Boot that you are connecting to a XA connection factory. 您需要指示Spring Boot正在连接到XA连接工厂。 According to Spring Boot documentation ( 32.3 Using a Java EE managed transaction manager ) : 根据Spring Boot文档( 32.3使用Java EE管理的事务管理器 ):

Spring Boot will attempt to auto-configure JMS by looking for a ConnectionFactory at the JNDI path java:/JmsXA or java:/XAConnectionFactory Spring Boot将通过在JNDI路径java:/ JmsXAjava:/ XAConnectionFactory上查找ConnectionFactory来尝试自动配置JMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM