【问题标题】:Creating an authenticated XA resource from ActiveMQConnectionFactory从 ActiveMQConnectionFactory 创建经过身份验证的 XA 资源
【发布时间】:2016-08-31 11:56:19
【问题描述】:

我有一个 Apache ActiveMQ Artemis (1.3) 实例,我正在尝试从我的团队目前正在开发的独立 Spring (4.3.2) 应用程序连接到该实例。它有 Spring JTATransactionManager 使用 Atomikos (4.0.4) UserTransactionManager 作为提供者,在这些事务期间,我需要连接到多个资源,包括上述 MQ。按照 Artemis 和 Atomikos 手册,我们设置了 ActiveMQConnectionFactory,然后将其传递给 AtomikosConnectionFactoryBean。这一切都发生在 Spring 容器中,但这似乎与我们的问题无关。

在我尝试向 MQ 连接添加身份验证之前,一切都运行良好。可以在 ActiveMQConnectionFactory 的实例上设置 userpassword 属性,但是,它们似乎仅在创建普通连接时才考虑在内:

@Override
public Connection createConnection() throws JMSException {
  return createConnection(user, password);
}

@Override
public Connection createConnection(final String username, final String password) throws JMSException {
  return createConnectionInternal(username, password, false, ActiveMQConnection.TYPE_GENERIC_CONNECTION);
}

Atomikos 正在调用 createXAConnection() 方法(来自 XAConnectionFactory 接口),正如我在其实现中看到的那样,除非明确传递,否则忽略凭据:

@Override
public XAConnection createXAConnection() throws JMSException {
  return createXAConnection(null, null);
}

@Override
public XAConnection createXAConnection(final String username, final String password) throws JMSException {
  return (XAConnection) createConnectionInternal(username, password, true, ActiveMQConnection.TYPE_GENERIC_CONNECTION);
}

这也是该类中其他一些方法的工作方式,因此我认为这不是错误。如果是这样,我应该如何获得经过身份验证的 XAConnection?我看不到 Atomikos 调用重载版本的可能性,查看它的代码。

问候, 雅库布

【问题讨论】:

    标签: spring-transactions spring-jms xa atomikos activemq-artemis


    【解决方案1】:

    作为一种解决方法,我们决定将 ActiveMQConnectionFactory 包装在一个实现必要接口的类中:

    public class ActiveMQConnectionFactoryWrapper implements XAConnectionFactory {
    
      private final ActiveMQConnectionFactory factory;
    
      public ActiveMQConnectionFactoryWrapper(ActiveMQConnectionFactory factory) {
        this.factory = factory;
      }
    
      @Override
      public XAConnection createXAConnection() throws JMSException {
        return factory.createXAConnection(factory.getUser(), factory.getPassword());
      }
    
      @Override
      public XAConnection createXAConnection(String userName, String password) throws JMSException {
        return factory.createXAConnection(userName, password);
      }
    
      @Override
      public XAJMSContext createXAContext() {
        return factory.createXAContext(factory.getUser(), factory.getPassword());
      }
    
      @Override
      public XAJMSContext createXAContext(String userName, String password) {
        return factory.createXAContext(userName, password);
      }
    }
    

    如果需要,也可以实现其他接口。

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2022-10-06
      • 2019-06-05
      • 2022-01-25
      • 2012-09-05
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多