incubator-seata icon indicating copy to clipboard operation
incubator-seata copied to clipboard

[feature] phrase two handler filter

Open elrond-g opened this issue 2 years ago • 0 comments

  • [x] I have registered the PR changes.

Ⅰ. Describe what this PR did

作用:

  • 给二阶段 handler 增加一个Filter, 用于处理共性问题(比如流量染色、透传数据梳理等待)

用法:

  • 实现一个 Filter
public class RMTestFilter implements RMFilter {

        @Override
        public void commitFilter(BranchCommitRequest request, BranchCommitResponse response, RMFilterChain chain)
                throws TransactionException {
            // 这里写提交前需要执行的内容
            chain.next().commitFilter(request, response, chain);
            // 这里写提交后需要执行的内容
        }

        @Override
        public void rollbackFilter(BranchRollbackRequest request, BranchRollbackResponse response, RMFilterChain chain)
                throws TransactionException {
            // 这里写回滚前需要执行的内容
            chain.next().rollbackFilter(request, response, chain);
            // 这里写回滚后需要执行的内容
        }
    }
  • 注册 Filter
// 注入点最好放在@Bean 方法中,或者@PostConstruct方法中,避免出现多次加载的问题
RMFilterManager.addLastFilter(new RMTestFilter());

注意事项:

  • filter 中自行处理好异常,避免以外的提交、回滚失败
  • 如果需要 response ,则需要在 chain 运行结束以后
  • 不要随意给 request 和 response 赋值

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

在 sample 应用中测试

Ⅳ. Describe how to verify it

创建使用中的 RMFilter,打印日志并发起事务

Ⅴ. Special notes for reviews

elrond-g avatar Aug 11 '22 17:08 elrond-g