【问题标题】:Custom XAResource (JTA)自定义 XAResource (JTA)
【发布时间】:2014-11-27 17:47:49
【问题描述】:

今天,我的团队负责人要求我为我们的一个内部产品实现一个自定义 XAResource 包装器(它提供类似于 SFTP 服务器的功能,但它是分布式的,用 Java 编写,并且有很多其他东西......没关系=))。

这里有一点很重要,我们的应用程序是一个独立的应用程序(因此我需要使用像 Atomikos 这样的嵌入式 JTA 事务管理器),它已经具有 Spring、JMS 和 Hibernate(也必须包括在内)在分布式事务中)里面。

问题是我已经用 Google 搜索了一段时间,但我还没有找到任何具有实现和配置自定义 XAResource 实现示例的资源。 附言其实我已经找到XADisk framework了,不过最好一步一步了解整个过程=)

有人可以分享这样一个例子的链接或资源吗? 提前致谢!

更新

有一个有用的资源可以帮助您了解整个事情:Spring documentation

【问题讨论】:

    标签: java spring jta xa


    【解决方案1】:

    我最终得到了以下结果。在我将 Bitronix 的库和所有源代码添加到我的项目后,我在其中看到了一个有趣的 EhCache XAResource 示例。我检查了它是如何工作的并复制了逻辑。在此之后剩下的唯一一个问题是:“如何为事务管理器征集我的资源?”。为了做到这一点,我编写了以下工厂(在我的情况下,我需要创建一个 SFTP 资源 XA 感知):

    @Service
    public class XaSftpSessionFactory {
    
       @Autowired
       private JtaTransactionManager transactionManager;
    
       public XaSftpSession getSession(final ConnectionSettings settings) {
          if (settings == null) {
               throw new IllegalArgumentException("The specified SFTP connection settings must be not null.");
          }
    
          final XaSftpSession xaSession = new XaSftpSession(settings);
          final XaSftpResource xaResource = new XaSftpResource(xaSession);
          xaSession.setXaResource(xaResource);
    
          XaSftpResourceProducer.registerXAResource(settings.getName(), xaResource);
    
          try {
               Transaction transaction = transactionManager.getTransactionManager().getTransaction();
               transaction.enlistResource(xaResource);
               transaction.registerSynchronization(
                new Synchronization() {
                    @Override
                    public void beforeCompletion() {
                    }
    
                    @Override
                    public void afterCompletion( int status ) {
                        XaSftpResourceProducer.unregisterXAResource(settings.getName(), xaResource );
                    }
                }
            );
           } catch (RollbackException | SystemException exception) {
              throw new IllegalStateException(
                String.format("Can't create an SFTP session for the '%s' instance.", settings.getName()),
                exception
            );
           }
    
           return xaSession;
       }
    }
    

    【讨论】:

    • Dmitry,我的情况与需要为当前不支持 XA 的资源编写自定义 XAResource 的情况相同。您能否与我分享您为此创建的自定义 XAResource 代码?我知道已经有好几年了——但到目前为止我还没有找到任何其他的例子。谢谢。
    猜你喜欢
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2017-12-30
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多