公共异步抽象类(多线程)

AsyncHandler

抽象类:

package com.finance.pabbank.util;

public abstract class AsyncHandler {
   public void callback(final Object obj ) {
       Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
        handle(obj);
        }
    });
    thread.start();
    }

    abstract public void handle(Object ...obj );
}

 

 

抽象类子类:

package com.finance.pabbank.support;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.finance.pabbank.agpay.config.SpaFireBankConfigInfo;
import com.finance.pabbank.agpay.dto.BatchAgpayFileUpReq;
import com.finance.pabbank.agpay.dto.BatchAgpayFileUpResultQueryReq;
import com.finance.pabbank.agpay.dto.BatchAgpayFileUpResultQueryRes;
import com.finance.pabbank.agpay.dto.BatchAgpayReq;
import com.finance.pabbank.agpay.dto.BatchAgpayRes;
import com.finance.pabbank.agpay.dto.SpaBatchFileDto;
import com.finance.pabbank.agpay.support.SpaFireBankChannel;
import com.finance.pabbank.util.AsyncHandler;
import com.finance.pabbank.util.ConcurrentFlagProvider;


@Service("pabBankBatchSendSupport")
public class PabBankBatchSendSupport extends AsyncHandler{
    
private static final Logger logger = LogManager.getLogger(PabBankBatchSendSupport.class);
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    
    private static String OBJECT_KEY="SPAB_BATCH_FILE";
    
    public void BatchPay(Object...obj) {
        
        logger.info("==========批量代付异步发送报文并确认========");
        SpaBatchFileDto req=(SpaBatchFileDto)obj[0];
         try {
                //查询未提交的代付文件
                SpaFireBankConfigInfo configInfo=req.getSpaFireBankConfigInfo();
                logger.info("[BatchPay]-=-=-=-=-=-=处理文件批次id"+req.getId()+"===代付批次=="+req.getBatch());
                //发送批量代付文件
                logger.info("[BatchPay]发送批量代付文件  1");
                BatchAgpayFileUpReq bafuq=new BatchAgpayFileUpReq();
                bafuq.setTradeSn(req.getId());
                bafuq.setFileName(req.getFileName());
                bafuq.setFileSize("");
                bafuq.setFilePath(req.getFilePath());
                SpaFireBankChannel.batchPayFileUp(bafuq,req.getSpaFireBankConfigInfo(),req.getId());

                
                //查询代付文件上传是否成功
                logger.info("[BatchPay]查询批量代付文件  2");
                BatchAgpayFileUpResultQueryReq bfrq=new BatchAgpayFileUpResultQueryReq();
                bfrq.setTradeSn(req.getId());   //唯一字段
                bfrq.setFileName(req.getFileName());
                BatchAgpayFileUpResultQueryRes bfrqres=null;
                for(int i=0;i<50;i++){
                    bfrqres = SpaFireBankChannel.batchPayFileUpResQuery(bfrq, req.getSpaFireBankConfigInfo());
                    if(bfrqres.getCode().equals("F0")){
                        break;
                    }
                    Thread.sleep(1000);
                }

                if(bfrqres!=null&&bfrqres.getCode().equals("F0")){
                    logger.info("[BatchPay]发送批量代付消息  3");
                    //发送上传代付文件通知
                    BatchAgpayReq baq=new BatchAgpayReq();
                        baq.setAcctNo(configInfo.getAcctNo());
                        baq.setCorpId(configInfo.getCorpId());
                        baq.setBatchNo(req.getBatch());
                        baq.setBusiType("00000");
                        baq.setTotalNum(req.getTotalNum()+"");
                        baq.setTotalAmount(req.getTotalAmount());
                        baq.setRemark("");
                        baq.setFileName(req.getFileName());
                        baq.setRandomPwd(bfrqres.getRandomPwd());
                        baq.setHashData("");
                        baq.setSignData("");
                    BatchAgpayRes res=SpaFireBankChannel.batchPay(baq, configInfo,req.getId());
                    logger.info("[BatchPay]发送批量代付消息银行返回结果"+res.getRetCode());
                }else{
                    logger.info("[BatchPay]查询文件上传结果失败 "+req.getBatch());
                    redisTemplate.opsForHash().put(OBJECT_KEY,req.getBatch(), req.getBatch());
                }
        } catch (Exception e) {
            logger.error("job批量代付上传文件  发送代付消息异常",e);
        }finally{
            // 无论代付成功还是失败清除 缓存中当前批次信息
            logger.info("删除日志标志============batch===================");
            ConcurrentFlagProvider.removeFlag(redisTemplate, req.getBatch());
        }
    }

    @Override
    public void handle(Object...obj) {
        BatchPay(obj);
    }

}

 

调用抽象类子类的方法:

package com.finance.pabbank.spiImpl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.finance.chn.pabbank.spi.AgpayPabBankSpi;
import com.finance.pabbank.agpay.dto.SpaBatchFileDto;
import com.finance.pabbank.support.PabBankBatchSendSupport;
import com.finance.pabbank.support.PabBankSupport;
import com.finance.pabbank.util.ConfigRedis;
import com.pisgah.finance.agpay.body.rep.AgPayBatchQueryRep;
import com.pisgah.finance.agpay.body.rep.AgPayBatchTransferRep;
import com.pisgah.finance.agpay.body.rep.AgPaySingleQueryRep;
import com.pisgah.finance.agpay.body.rep.AgPaySingleTransferRep;
import com.pisgah.finance.agpay.body.req.AgPayBatchQueryReq;
import com.pisgah.finance.agpay.body.req.AgPayBatchTransferReq;
import com.pisgah.finance.agpay.body.req.AgPaySingleQueryReq;
import com.pisgah.finance.agpay.body.req.AgPaySingleTransferReq;
import com.pisgah.finance.lang.config.wrapper.ConfigCodeWrapper;

@Service("agpayBatchPabBankSpiImpl")
public class AgpayBatchPabBankSpiImpl implements AgpayPabBankSpi {
    
    private static final Logger logger = LogManager.getLogger(AgpayBatchPabBankSpiImpl.class);
    @Autowired
    ConfigRedis configRedis;

    @Autowired
    PabBankSupport pabBankSupport;
    
    @Autowired
    PabBankBatchSendSupport pabBankBatchSendSupport;
    
    @Override
    public AgPaySingleTransferRep SinglePay(AgPaySingleTransferReq req) {
        return null;
    }

    @Override
    public AgPayBatchTransferRep BatchPay(AgPayBatchTransferReq req) {
        logger.info("进入渠道平安银行 批量 代付  start 传入参数"+req );
        ConfigCodeWrapper wrapper = configRedis.getWrapper(req.getChnCode(), req.getChnAcctId());
        SpaBatchFileDto spaBatchFile = new SpaBatchFileDto();
        AgPayBatchTransferRep rep = pabBankSupport.creatBatchFile(req,wrapper,spaBatchFile);
        /**
         * 判断批量代付 文件生成结果  
         */
        if(rep.isSuccess() && rep.isAvailable()){// 成功后 进行 代付文件发送动作
            logger.info("成功后进行代付文件发送动作start");
            pabBankBatchSendSupport.callback(spaBatchFile);
            logger.info("成功后进行代付文件发送动作end");
        }
        
        logger.info("进入渠道平安银行 批量 代付  end 传出参数"+rep );
        return rep;
    }

    @Override
    public AgPaySingleQueryRep querySingleStatus(AgPaySingleQueryReq req) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public AgPayBatchQueryRep queryBatchResult(AgPayBatchQueryReq req) {
        logger.info("进入渠道平安银行 批量 代付查询  start 传入参数"+req );
        ConfigCodeWrapper wrapper = configRedis.getWrapper(req.getChnCode(), req.getChnAcctId());

        AgPayBatchQueryRep rep = pabBankSupport.queryBatchResult(req,wrapper);
        logger.info("进入渠道平安银行 批量 代付  end 传出参数"+rep );
        return rep;
    }

}

posted on 2018-01-02 16:19  释迦&牟尼  阅读(403)  评论(0)    收藏  举报

导航