IMF_Sender.java 2.8 KB
package com.tianbo.imf;

import com.caac.imf.api.IMFClient;
import com.tianbo.Main;
import com.tianbo.util.Date.DateUtil;
import com.tianbo.util.IO.FileTool;
import com.tianbo.util.XML.MakeImfMeta;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;

public class IMF_Sender extends Thread{

    protected static final Logger logger = Logger.getLogger(IMF_Sender.class);
    public static  boolean isrunning;
    private IMFClient client;
    private String content;
    public IMF_Sender(IMFClient client) {
        this.client = client;
    }
    public IMF_Sender(IMFClient client, String content) {
        this.client = client;
        this.content = content;
    }

    public void run(){
        try {
            String sendDir   = FileTool.readProperties("readDirectory");
            String SNDR = FileTool.readProperties("loginname");
            String TYPE =  FileTool.readProperties("TYPE");
            String STYP =  FileTool.readProperties("STYP");
            String RCVR =  FileTool.readProperties("RCVR");
            String DDTM = DateUtil.getDDTM();
            String SEQN = DDTM;

            if (this.client == null) {
                logger.info("IMFClient has been closed");
                return;
            }
            isrunning=true;
            if(Main.isSuc){
//                logger.info("<<<<<<<<<<<主线程初始化成功>>>>>>>>>>>>");
                if(Main.LOGIN_OK){
                    while (true){
                        //发送报文
                        List<File> files = FileTool.readDirectoryFiles(new File(sendDir));
                        for (File file:files) {
                            String fileContent =  FileTool.readfile(file);
                            String sendMsg = MakeImfMeta.makeImfDocument(SNDR,RCVR,TYPE,STYP,DDTM,SEQN,fileContent);
                            String returnMsg = this.client.sendMSG(sendMsg);

                            //发送成功处理,删除已发送成功的目录下的文件
                            if(returnMsg.indexOf("<CODE>9</CODE>")>=0){
                                FileUtils.forceDelete(file);
                            }
                        }
                        try {
                            Thread.sleep(500L);
                        } catch (InterruptedException var3) {
                            var3.printStackTrace();

                        }
                    }



                }else{
                    logger.info("<<<<<<<<<<<登陆中>>>>>>>>>>>>");
                }
            }

        }catch (Exception var14) {
            var14.printStackTrace();
            logger.info(var14.getMessage());
        }




        isrunning=false;
        logger.info("<<<<<<<<<<<发送线程结束>>>>>>>>>>>>");
    }
}