IMF_Tesk.java 3.1 KB
package com.sy.IMF;

import com.caac.imf.api.IMFClient;
import com.caac.imf.api.IMFClientFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zhangfan
 * @web www.sy.com
 * @company 郑州商友科技有限公司
 * @time 2018-11-27 16:05
 */
@Component
public class IMF_Tesk {

    protected static final Logger logger = Logger.getLogger(IMF_Tesk.class);

    public static boolean LOGIN_OK = false;
    public static boolean isSuc = true;
    public static IMFClient client = null;

    @Scheduled(fixedRate = 5000)
    private static void start() throws Exception {
        PropertyConfigurator.configure("config/log4j.properties");
        client = IMFClientFactory.createInstance();


        if (client != null) {
            if (!KAKO_Reader.isrunning) {
                KAKO_Reader kako_reader = new KAKO_Reader(client);
                kako_reader.start();
                logger.info("*********KAKO读取线程已开启***********");
            } else {
                logger.info("*********KAKO读取线程已开启-不再启动线程*********");
            }

            if(!KAKO_Sender.isrunning){
                KAKO_Sender kako_sender = new KAKO_Sender(client);
                kako_sender.start();
                logger.info("<<<<<<<<<KAKO发送线程已开启>>>>>>>>>>>>");
            }else {
                logger.info("<<<<<<<<<KAKO发送线程已开启-不再启动线程>>>>>>>>>>>>");
            }

        }
        if (!LOGIN_OK){
             loginIMF(client, "KAOR", "KAOR", "config/imf_config.properties");
        }



    }

    private static void loginIMF(IMFClient client, String userName, String password, String confFileName) {
        if (client.initial(confFileName)) {
            String message = client.login(userName, password);
            logger.info("message=" + message);
            if (message.indexOf("<CODE>1</CODE>") > 0) {
                logger.info("登陆成功");
                LOGIN_OK = true;
            } else {
                int times = 0;

                while(times <= 3) {
                    logger.info("try connection...");
                    ++times;
                    logger.info("message.=" + message);
                    if (message.indexOf("<CODE>1</CODE>") > 0) {
                        logger.info("登陆成功");
                        LOGIN_OK = true;
                        break;
                    }

                    logger.info("登录失败~~~~");
                    message = client.login(userName, password);

                    try {
                        Thread.sleep(4000L);
                    } catch (InterruptedException var7) {
                        var7.printStackTrace();
                    }
                }

                if (!LOGIN_OK) {
                    logger.info("多次尝试登录失败,退出登陆");
                    client.disconnect();
                    isSuc =false;
                    System.exit(-1);
                }
            }
        }

    }
}