1. Network exception retry logic writing
If a network exception occurs during the process of connecting to the vendor, we need to make a compensation mechanism. In the task type enumeration class: There is a business status code in TaskTypeEnum that is for the failure of remote calls.
Step 1: In the method to connect to the supplier: in the recharge method in the SupplierServiceImpl class, add try{}catch{} to the code block that calls the supplier. After catching the exception, add the retry task. The task type enumeration is:
@Override public void recharge(RechargeRequest rechargeRequest) { //................... The previous code is omitted Result<RechargeResponse> result = null; try { result = doDispatchSupplier(rechargeRequest); } catch (Exception e) { ("recharge exception ,{}",()); //Add remote call retry task (); (rechargeRequest); return; } if(result !=null){ //Judgment success or failure if(() == ){ ("The order is successfully placed, wait for the recharge to process the callback!"); //Pay special attention to the order status that cannot be modified to be recharged successfully at this time------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- updateTrade((),());//Recharge process is waiting for confirmation return; }else { //There are several types of failures: insufficient balance rotation, failed ordering and retry, etc. if(() == StatusCode.BALANCE_NOT_ENOUGH){ //Insufficient simulation balance Rotation-to-extreme speed /* (); (0); (StatusCode.BALANCE_NOT_ENOUGH);*/ //Put our suppliers with insufficient balance into the reids exclusion collection (Constants.exclude_supplier,()); String nextSupply = nextSupply(); ("Switch to new suppliers as:"+nextSupply); if(nextSupply !=null){ (nextSupply); (0); (StatusCode.BALANCE_NOT_ENOUGH); }else { //No suppliers updateTrade((),()); return; } }else if(() == StatusCode.ORDER_REQ_FAILED) { //Writing retry logic---adding retry task (StatusCode.ORDER_REQ_FAILED); } (rechargeRequest); } } }
Step 2: Add the remote call exception retry method in the supplier task interface SupplierTask: rechargeException
/*** Retry remote call exception*/ public void rechargeException();
Step 3: Implement the remote call retry method
@Override @Scheduled(fixedRate = 1000) public void rechargeException() { retry(); }
Step 4: Test: Except for chongba_recharge_mock not started, everything else is started, and phone bill recharge is performed to simulate the scenario of remote call failure.
2. Successful logic writing of supplier phone bill recharge
After the docking call is successful, we need to change the order status to being processed. After a period of time, the supplier will call back our system. All we need to do is to change the order status to the recharge successfully.
Step 1: Simulate the successful docking situation. In the method doPostJisu (RechargeRequest rechargeRequest) in the SupplierServiceImpl class, the successful return of the simulated speed
//("req_status", ""+); ("req_status", ""+);
Step 2: Docking order method: recharge(RechargeRequest rechargeRequest) logic modification, add the judgment of successful docking, which is currently a failure
Judging the business status code in the Result result returned by docking. If it is successful, the docking order will be modified to be processed. Otherwise, it will be some of the current abnormal logic.
@Override public void recharge(RechargeRequest rechargeRequest) { //................The previous omission Result<RechargeResponse> result = null; try { result = doDispatchSupplier(rechargeRequest); } catch (Exception e) { ("recharge exception ,{}",()); //Add remote call retry task (); (rechargeRequest); return; } if(result !=null){ //Judgment success or failure if(() == ){ ("The order is successfully placed, wait for the recharge to process the callback!"); //Pay special attention to the order status that cannot be modified to be recharged successfully at this time------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- updateTrade((),());//Recharge process is waiting for confirmation return; }else { //There are several types of failures: insufficient balance rotation, failed ordering and retry, etc. if(() == StatusCode.BALANCE_NOT_ENOUGH){ //Insufficient simulation balance Rotation-to-extreme speed /* (); (0); (StatusCode.BALANCE_NOT_ENOUGH);*/ //Put our suppliers with insufficient balance into the reids exclusion collection (Constants.exclude_supplier,()); String nextSupply = nextSupply(); ("Switch to new suppliers as:"+nextSupply); if(nextSupply !=null){ (nextSupply); (0); (StatusCode.BALANCE_NOT_ENOUGH); }else { //No suppliers updateTrade((),()); return; } }else if(() == StatusCode.ORDER_REQ_FAILED) { //Writing retry logic---adding retry task (StatusCode.ORDER_REQ_FAILED); } (rechargeRequest); } } }
Step 3: After success, the Speed Platform will perform a callback. In the MockJisuRechargeController in the chongba_recharge_mock module, the callback charging system, the callback method is in the BaseController under the module: rechargeNotify
In actual business, the charging system is configured on the supplier platform, and the callback address is:
notify-url: http://127.0.0.1:99/order/notify
The method of receiving callbacks needs to be completed in the RechargeNotifyController class of the chongba_recharge_supplier module. In actual business, the service of calling orders is used to process the order status.
@Autowired protected OrderTradeMapper orderTradeMapper; @RequestMapping(value = "/order/notify") public String notify(@RequestBody String result) { JSONObject jsonObject = (JSONObject) (result); String orderNo= (String) ("orderNo"); int status= (("status").toString()); ("Recharge callback successfully modified order{}The status of{}",orderNo,status); updateTrade(orderNo, status); return "sucess"; } private void updateTrade(String orderNo, int orderStatus) { //Modify order status QueryWrapper<OrderTrade> queryWrapper = new QueryWrapper<>(); ("order_no", orderNo); OrderTrade orderTrade = (queryWrapper); if(orderTrade!=null) { (orderStatus); (orderTrade, queryWrapper); } }
Step 4: Test
Start all projects and perform phone bill recharge services. After the recharge is successful, enter the order list and check the order status. Because the current logic is that the supplier calls back to our system in 5 seconds, so refresh the order list page in 5 seconds to check that the order status has changed.