Biz-SIP中间件之xbank项目实战(6)——应用层对延迟服务的组装

xbank项目版本库:https://gitee.com/szhengye/xbank.git

项目实践:应用服务对延迟服务的组装

1. SAF存储转发的实现

在领域层中,开发Payment2SinkService,根据tranMode交易模式,实现处理超时、处理失败和处理成功的各种异常情况:

@Service
public class Payment2SinkService implements JSONObjectSinkBeanInterface {
    @Autowired
    private TimeoutCmdExe timeoutCmdExe;
    @Autowired
    private TimeoutAndFailCmdExe timeoutAndFailCmdExe;
    @Autowired
    private TimeoutAndSuccessCmdExe timeoutAndSuccessCmdExe;
    @Autowired
    private SuccessCmdExe successCmdExe;

    @Override
    public JSONObject process(JSONObject jsonObject) throws BizException {
        log.info("传入消息:\n{}", jsonObject.toString());
        AbstractSinkBeanCmdExe sinkBeanCmdExe;
        String tranMode = (String)jsonObject.get("tranMode");
        switch (tranMode) {
            case "timeout":
                // 收到交易后,永远返回超时
                return timeoutCmdExe.execute(jsonObject);
            case "3timeout-fail":
                // 收到交易后,前3次返回超时,第4次返回失败码
                return timeoutAndFailCmdExe.execute(jsonObject);
            case "3timeout-success":
                // 收到交易后,前3次返回超时,第4次成功返回原报文
                return timeoutAndSuccessCmdExe.execute(jsonObject);
            default:
                //其它情况,成功返回原报文
                return successCmdExe.execute(jsonObject);
        }
    }
}

注意,Payment2领域服务在封装时,涉及到多个接口调用,对每个接口采用命令执行器(Command Executor),这里是传入标准的JSONObject数据,所以所有的接口实现继承AbstractSinkBeanCmdExe类来实现:
image.png
在应用层中,在原有的PersonalAppInterface应用层接口、PersonalAppService服务类中,增加send2Payment2()、getCustomerAndSaf2Payment2()这2个方法接口和实现,通过personalAppDelayInterface延迟调用接口,实现对原有send2Payment2()方法的多次SAF调用:

@Service
public class PersonalAppService implements PersonalAppInterface {
    ...
        
    private BizMessageInterface payment2SinkInterface = IntegratorClientFactory
            .getSinkClient(BizMessageInterface.class,"payment2-sink");
    private PersonalAppInterface personalAppDelayInterface = IntegratorClientFactory
            .getDelayBizServiceClient(PersonalAppInterface.class,"app/personal",
                    0,1000,2000,4000,8000,16000,32000);
	...
        
    @Override
    public BizMessage send2Payment2(String tranMode, String tranCode, Object message) throws BizException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("tranCode",tranCode);
        jsonObject.set("tranMode",tranMode);
        jsonObject.set("message",message);
        return this.payment2SinkInterface.call(jsonObject);
    }

    @Override
    public Customer getCustomerAndSaf2Payment2(String tranMode,String customerId) throws BizException {
        Customer customer = this.customerSinkInterface.getCustomer(customerId);
        this.personalAppDelayInterface.send2Payment2(tranMode,"send-customer", customer);
        return customer;
    }
    ...
}

传入交易模式为”timeout“(收到交易后,永远返回超时),这将导致发送7次,最后交易状态为失败:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"getCustomerAndSaf2Payment2","params":["timeout","001"]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "c1d2ffb7f0b44bf4b0cb7e13e5c0c0fa",
  "parentTraceId": null,
  "timestamp": 1631274896174,
  "data": {
    "result": {
      "sex": "1",
      "customerName": "张三",
      "customerId": "001",
      "age": 30
    }
  }
}

Payment2SinkApplication日志:

2021-09-10 19:54:56.650  INFO 14993 --- [nio-8004-exec-3] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:54:56.651  INFO 14993 --- [nio-8004-exec-3] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:54:57.753  INFO 14993 --- [nio-8004-exec-4] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:54:57.754  INFO 14993 --- [nio-8004-exec-4] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:54:59.920  INFO 14993 --- [nio-8004-exec-5] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:54:59.920  INFO 14993 --- [nio-8004-exec-5] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:55:04.015  INFO 14993 --- [nio-8004-exec-6] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:55:04.015  INFO 14993 --- [nio-8004-exec-6] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:55:12.041  INFO 14993 --- [nio-8004-exec-7] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:55:12.042  INFO 14993 --- [nio-8004-exec-7] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:55:28.080  INFO 14993 --- [nio-8004-exec-8] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:55:28.080  INFO 14993 --- [nio-8004-exec-8] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:56:00.228  INFO 14993 --- [nio-8004-exec-9] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"timeout","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:56:00.228  INFO 14993 --- [nio-8004-exec-9] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!

传入交易码为”3timeout-fail“(收到交易后,前3次返回超时,第4次返回失败),这将导致发送4次,最后交易状态为失败:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"getCustomerAndSaf2Payment2","params":["3timeout-fail","001"]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "d3589b169d654a409c2f7d15f83f5113",
  "parentTraceId": null,
  "timestamp": 1631275051123,
  "data": {
    "result": {
      "sex": "1",
      "customerName": "张三",
      "customerId": "001",
      "age": 30
    }
  }
}

Payment2SinkApplication日志:

2021-09-10 19:57:31.379  INFO 14993 --- [nio-8004-exec-1] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-fail","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:57:31.379  INFO 14993 --- [nio-8004-exec-1] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:57:32.647  INFO 14993 --- [nio-8004-exec-2] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-fail","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:57:32.647  INFO 14993 --- [nio-8004-exec-2] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:57:34.672  INFO 14993 --- [nio-8004-exec-3] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-fail","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:57:34.673  INFO 14993 --- [nio-8004-exec-3] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:57:38.733  INFO 14993 --- [nio-8004-exec-4] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-fail","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:57:38.733  INFO 14993 --- [nio-8004-exec-4] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回交易处理失败!

传入交易码为”3timeout-success“(收到交易后,前3次返回超时,第4次成功返回原报文),这将导致发送4次,最后交易状态为成功:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"getCustomerAndSaf2Payment2","params":["3timeout-success","001"]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "75b76cf518164964bedf477ea488698c",
  "parentTraceId": null,
  "timestamp": 1631275117934,
  "data": {
    "result": {
      "sex": "1",
      "customerName": "张三",
      "customerId": "001",
      "age": 30
    }
  }
}

Payment2SinkApplication日志:

2021-09-10 19:58:38.146  INFO 14993 --- [nio-8004-exec-5] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-success","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:58:38.146  INFO 14993 --- [nio-8004-exec-5] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:58:39.200  INFO 14993 --- [nio-8004-exec-6] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-success","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:58:39.200  INFO 14993 --- [nio-8004-exec-6] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:58:41.409  INFO 14993 --- [nio-8004-exec-7] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-success","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:58:41.409  INFO 14993 --- [nio-8004-exec-7] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回超时异常!
2021-09-10 19:58:45.539  INFO 14993 --- [nio-8004-exec-8] c.x.d.p.service.Payment2DomainService    : 传入消息:
{"tranMode":"3timeout-success","message":{"sex":"1","customerName":"张三","customerId":"001","age":30},"tranCode":"send-customer"}
2021-09-10 19:58:45.539  INFO 14993 --- [nio-8004-exec-8] c.x.d.p.service.Payment2DomainService    : 交易:send-customer,返回交易成功!

2. 向前补偿的实现

向前补偿机制是指当交易无应答时,系统会对原交易进行查询,如查询超时会重试多次。如原交易成功则继续后续交易,如原交易失败则对前续交易进行交易补偿,向前补偿一般是针对第三方接入方无法提供补偿交易时的解决方案。
在金融领域,这类交易补偿动作常被称为“冲正”。
在应用层中,在原有的PersonalAppInterface应用层接口、PersonalAppService服务类中,增加payoutForward()、payoutForwardCompensate()这2个方法接口和实现,通过personalAppDelayInterface延迟调用接口,实现对原有payoutForwardCompensate()方法的多次重复延迟调用:

@Service
public class PersonalAppService implements PersonalAppInterface {
    private AccountSinkInterface accountSinkInterface = IntegratorClientFactory
            .getSinkClient(AccountSinkInterface.class,"account-sink");
    private CustomerSinkInterface customerSinkInterface = IntegratorClientFactory
            .getSinkClient(CustomerSinkInterface.class,"customer-sink");
    private BizMessageInterface payment1SinkInterface = IntegratorClientFactory
            .getSinkClient(BizMessageInterface.class,"payment1-sink");
    private BizMessageInterface payment2SinkInterface = IntegratorClientFactory
            .getSinkClient(BizMessageInterface.class,"payment2-sink");
    private PersonalAppInterface personalAppDelayInterface = IntegratorClientFactory
            .getDelayBizServiceClient(PersonalAppInterface.class,"app/personal",
                    0,1000,2000,4000,8000,16000,32000);
    
    ......
        
	@Override
    public void payoutForward(String tranMode,String accountId, long amount) throws BizException {
        log.info("account出金:{},{}",accountId,amount);
        this.accountDomainInterface.payout(accountId,amount);
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("tranCode","pay");
        jsonObject.set("tranMode",tranMode);
        jsonObject.set("accountId",accountId);
        jsonObject.set("tranAmount",amount);
        BizMessage<JSONObject> bizMessage = null;
        try {
            log.info("payment缴费...");
            bizMessage = this.payment2SinkInterface.call(jsonObject);
        } catch (BizException e) {
            if (e.isTimeOutException()) {
                log.info("payment交易超时,开始payout补偿...");
                this.personalAppDelayInterface.payoutForwardCompensate(jsonObject);
                return;
            }
            else {
                throw e;
            }
        }
        log.info("payment缴费成功!");
        log.info("payout成功!");
    }

    @Override
    public void payoutForwardCompensate(JSONObject jsonObject)  throws BizException{
        jsonObject.set("tranCode","pay-query");
        BizMessage<JSONObject> bizMessage = null;
        try {
            log.info("payment查询缴费订单...");
            bizMessage = this.payment2SinkInterface.call(jsonObject);
        } catch (BizException e) {
            if (e.isTimeOutException()) {
                log.info("payment交易超时...");
                throw e;
            }
            else {
                log.info("payment查询缴费订单返回错误(表示对方订单没执行)...");
                String accountId = (String)jsonObject.get("accountId");
                long amount = (Integer) jsonObject.get("tranAmount");
                log.info("account出金补偿:{},{}",accountId,amount);
                this.accountDomainInterface.payoutCompensation(accountId,amount);
                return;
            }
        }
        log.info("payment查询缴费订单成功(表示对方订单已执行)");
        log.info("payout成功!");
    }

传入交易模式为”3timeout-success“(收到订单交易后,前3次返回超时,第4次成功返回模拟订单已执行),这将导致发送4次,最后交易状态为成功:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"payoutForward","params":["3timeout-success","0001",1]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "e0127a25e2514da682ad77042917087a",
  "parentTraceId": null,
  "timestamp": 1631275840559,
  "data": {}
}

XbankAppApplication日志:

[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:40.560 INFO 16503 [11034350FE38437BB459F1E8DC6CCE17] [http-nio-8888-exec-5] c.xbank.app.service.PersonalAppService   account出金:0001,1
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:40.822 INFO 16503 [11034350FE38437BB459F1E8DC6CCE17] [http-nio-8888-exec-5] c.xbank.app.service.PersonalAppService   payment缴费...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:40.831 INFO 16503 [11034350FE38437BB459F1E8DC6CCE17] [http-nio-8888-exec-5] c.xbank.app.service.PersonalAppService   payment交易超时,开始payout补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:42.086 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   payment查询缴费订单...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:42.095 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   payment交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:43.268 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.xbank.app.service.PersonalAppService   payment查询缴费订单...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:43.277 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.xbank.app.service.PersonalAppService   payment交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:45.296 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.xbank.app.service.PersonalAppService   payment查询缴费订单...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:45.306 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.xbank.app.service.PersonalAppService   payment查询缴费订单成功(表示对方订单已执行)
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:10:45.306 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.xbank.app.service.PersonalAppService   payout成功!

传入交易模式为”3timeout-success“(收到订单交易后,前3次返回超时,第4次成功返回模拟订单已执行),这将导致发送4次,最后交易状态为成功:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"payoutForward","params":["3timeout-fail","0001",1]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "a04163bf05484fa28071650a085d4124",
  "parentTraceId": null,
  "timestamp": 1631275997342,
  "data": {}
}

XbankAppApplication日志:

[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:17.342 INFO 16503 [5E5951618A27430684C5552E1F782D8B] [http-nio-8888-exec-7] c.xbank.app.service.PersonalAppService   account出金:0001,1
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:17.568 INFO 16503 [5E5951618A27430684C5552E1F782D8B] [http-nio-8888-exec-7] c.xbank.app.service.PersonalAppService   payment缴费...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:17.579 INFO 16503 [5E5951618A27430684C5552E1F782D8B] [http-nio-8888-exec-7] c.xbank.app.service.PersonalAppService   payment交易超时,开始payout补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:17.775 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-5] c.xbank.app.service.PersonalAppService   payment查询缴费订单...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:17.788 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-5] c.xbank.app.service.PersonalAppService   payment交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:18.827 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.xbank.app.service.PersonalAppService   payment查询缴费订单...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:18.836 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.xbank.app.service.PersonalAppService   payment交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:20.890 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   payment查询缴费订单...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:20.900 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   payment查询缴费订单返回错误(表示对方订单没执行)...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:13:20.901 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   account出金补偿:0001,1

3. 向后补偿的实现

向后补偿机制是指当交易无应答时,系统会对原交易发起多次交易补偿重试,如补偿交易成功则对前续交易进行补偿。
在应用层中,在原有的PersonalAppInterface应用层接口、PersonalAppService服务类中,增加payoutBackward()、payoutBackwardCompensate()这2个方法接口和实现,通过personalAppDelayInterface延迟调用接口,实现对原有payoutForwardCompensate()方法的多次重复延迟调用:

@Service
public class PersonalAppService implements PersonalAppInterface {
    private AccountSinkInterface accountSinkInterface = IntegratorClientFactory
            .getSinkClient(AccountSinkInterface.class,"account-sink");
    private CustomerSinkInterface customerSinkInterface = IntegratorClientFactory
            .getSinkClient(CustomerSinkInterface.class,"customer-sink");
    private BizMessageInterface payment1SinkInterface = IntegratorClientFactory
            .getSinkClient(BizMessageInterface.class,"payment1-sink");
    private BizMessageInterface payment2SinkInterface = IntegratorClientFactory
            .getSinkClient(BizMessageInterface.class,"payment2-sink");
    private PersonalAppInterface personalAppDelayInterface = IntegratorClientFactory
            .getDelayBizServiceClient(PersonalAppInterface.class,"app/personal",
                    0,1000,2000,4000,8000,16000,32000);
    
    ......
        
	@Override
    public void payoutBackward(String tranMode, String accountId, long amount) throws BizException {
        log.info("account出金:{},{}",accountId,amount);
        this.accountDomainInterface.payout(accountId,amount);
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("tranCode","pay");
        jsonObject.set("tranMode",tranMode);
        jsonObject.set("accountId",accountId);
        jsonObject.set("tranAmount",amount);
        BizMessage<JSONObject> bizMessage = null;
        try {
            log.info("payment缴费...");
            bizMessage = this.payment2SinkInterface.call(jsonObject);
        } catch (BizException e) {
            if (e.isTimeOutException()) {
                log.info("payment交易超时,开始payout冲正...");
                this.personalAppDelayInterface.payoutBackwardCompensate(jsonObject);
                return;
            }
            else {
                log.info("payment缴费交易返回错误");
                log.info("account出金补偿:{},{}",accountId,amount);
                this.accountDomainInterface.payoutCompensation(accountId,amount);
                log.info("payout交易补偿成功!");
                return;
            }
        }
        log.info("payment缴费成功");
        log.info("payout成功!");
    }

    @Override
    public void payoutBackwardCompensate(JSONObject jsonObject) throws BizException {
        jsonObject.set("tranCode","pay-reversal");
        BizMessage<JSONObject> bizMessage;
        try {
            log.info("payment缴费补偿...");
            bizMessage = this.payment2SinkInterface.call(jsonObject);
            log.info("payment缴费补偿成功");
        } catch (BizException e) {
            if (e.isTimeOutException()) {
                log.info("payment缴费补偿交易超时...");
                throw e;
            }
            log.info("payment缴费补偿交易失败,需要人工干预调整!");
            return;
        }
        String accountId = (String)jsonObject.get("accountId");
        long amount = (Integer)jsonObject.get("tranAmount");
        log.info("account出金补偿:{},{}",accountId,amount);
        this.accountDomainInterface.payoutCompensation(accountId,amount);
        log.info("payout补偿成功!");
    }

传入交易模式为”3timeout-success“(收到订单交易后,前3次返回补偿交易超时,第4次成功返回补偿交易执行成功),这将导致发送4次,最后交易状态为成功:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"payoutBackward","params":["3timeout-success","0001",1]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "26c58fe44b3f4ceba462762494fa5981",
  "parentTraceId": null,
  "timestamp": 1631276405513,
  "data": {}
}

XbankAppApplication日志:

[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:05.513 INFO 16503 [84936F44451449C5A92183EF93C3690C] [http-nio-8888-exec-2] c.xbank.app.service.PersonalAppService   account出金:0001,1
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:05.952 INFO 16503 [84936F44451449C5A92183EF93C3690C] [http-nio-8888-exec-2] c.xbank.app.service.PersonalAppService   payment缴费...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:05.961 INFO 16503 [84936F44451449C5A92183EF93C3690C] [http-nio-8888-exec-2] c.xbank.app.service.PersonalAppService   payment交易超时,开始payout冲正...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:06.396 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.xbank.app.service.PersonalAppService   payment缴费补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:06.405 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.xbank.app.service.PersonalAppService   payment缴费补偿交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:07.468 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   payment缴费补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:07.475 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.xbank.app.service.PersonalAppService   payment缴费补偿交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:09.560 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.xbank.app.service.PersonalAppService   payment缴费补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:09.570 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.xbank.app.service.PersonalAppService   payment缴费补偿成功
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:09.571 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.xbank.app.service.PersonalAppService   account出金补偿:0001,1
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:20:09.726 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.xbank.app.service.PersonalAppService   payout补偿成功!

传入交易模式为”3timeout-fail“(收到订单交易后,前3次返回超时,第4次成功返回模拟补偿交易失败),这将导致发送4次,最后交易状态为失败:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/personal" -X POST --data '{"methodName":"payoutBackward","params":["3timeout-fail","0001",1]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "40aed8f06bba436ba67ff93e769d5a33",
  "parentTraceId": null,
  "timestamp": 1631276555599,
  "data": {}
}

XbankAppApplication日志:

[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:35.599 INFO 16503 [B54A535614A64492A7A00D987F12B529] [http-nio-8888-exec-4] c.xbank.app.service.PersonalAppService   account出金:0001,1
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:35.760 INFO 16503 [B54A535614A64492A7A00D987F12B529] [http-nio-8888-exec-4] c.xbank.app.service.PersonalAppService   payment缴费...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:35.767 INFO 16503 [B54A535614A64492A7A00D987F12B529] [http-nio-8888-exec-4] c.xbank.app.service.PersonalAppService   payment交易超时,开始payout冲正...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:35.979 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.xbank.app.service.PersonalAppService   payment缴费补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:35.993 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.xbank.app.service.PersonalAppService   payment缴费补偿交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:37.073 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-5] c.xbank.app.service.PersonalAppService   payment缴费补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:37.083 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-5] c.xbank.app.service.PersonalAppService   payment缴费补偿交易超时...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:39.111 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.xbank.app.service.PersonalAppService   payment缴费补偿...
[bizsip-integrator:192.169.1.101:8888] 2021-09-10 20:22:39.121 INFO 16503 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.xbank.app.service.PersonalAppService   payment缴费补偿交易失败,需要人工干预调整!

Biz-SIP官方网站:http://bizsip.bizmda.com
Gitee:https://gitee.com/szhengye/biz-sip

posted @ 2021-10-10 16:33  抽游烟鸡  阅读(79)  评论(0)    收藏  举报