IMF_Task.java 3.7 KB
package com.tianbo.imfClient.schedul;

import com.caac.imf.api.IMFClient;
import com.caac.imf.api.IMFClientFactory;
import com.tianbo.imfClient.IMF_Reader;
import com.tianbo.imfClient.IMF_Sender;
import com.tianbo.util.IO.FileTool;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;

@Component
public class IMF_Task {
    protected static final Logger logger = Logger.getLogger(IMF_Task.class);
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static boolean LOGIN_OK = false;
    public static boolean isSuc = true;
    public static IMFClient client = null;
    public static String loginname;
    public static String loginpass;
    public static String isNeedSend = "N";


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


        //登录
        if (!LOGIN_OK) {
            loginIMF(client, loginname, loginpass, "config/imf_config.properties");
        }

        //启动读取线程
        if (client != null) {
            IMF_Reader reader = new IMF_Reader(client);

            if (!IMF_Reader.isrunning) {
                reader.start();
                logger.info("*********读取线程已开启***********");
            } else {
                //                    logger.info("*********读取线程已开启-不再启动线程*********");
            }

            if("Y".equals(isNeedSend)){
                IMF_Sender kako_sender = new IMF_Sender(client);
                if(!IMF_Sender.isrunning) {
                    kako_sender.start();
                }
            }

        }

    }

    public void sendMsg(String msg){
        if (!msg.equals(null) && !msg.isEmpty()){
            if(LOGIN_OK){
                IMF_Sender sender = new IMF_Sender(client,msg);
                sender.setContent(msg);
                sender.start();
            }

        }

    }

    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);
                }
            }
        }

    }

}