码迷,mamicode.com
首页 > 其他好文 > 详细

JCO重连SAP

时间:2020-07-21 22:07:32      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:nec   user   warnings   warning   tab   isa   连接   host   init   

记录一下

...

import com.sap.mw.jco.JCO;

// true:组登录,false:ip登录
private static boolean groupFlag = false;
private final static String prefsfile = "config-saphttp.properties";

    @SuppressWarnings("deprecation")
    public static JCO.Client getConnectionInPool(String poolName) {
        JCO.Client connection = null;
        if (groupFlag) { // 组登录
            connection = JCO.createClient(SAP_client, SAP_user, SAP_passwd,
                    "zh", SAP_mshost, SAP_r3name, SAP_group);
        } else { // ip登录
            JCO.Pool pool = JCO.getClientPoolManager().getPool(poolName);
            if (pool != null) {
                connection = JCO.getClient(poolName);
                // pool.setAbapDebug(true);
            }
        }
        return connection;
    }

/**
    * 递归获取可用的SAP连接,如没有就是null
    * /
private static JCO.Client getAvailableConnection(boolean groupFlag, String interfaceId, String nextId) {
        JCO.Client connection = null;

    if (interfaceId.equals(nextId)) {
        POOL_NAME = "";
        System.out.println("======error:没有可用连接池");
        return connection;
    }

    String id = StringUtils.isBlank(nextId) ? interfaceId : nextId;

    // 组登录
    if (groupFlag) {
        try {
            init(id);
            connection = getConnectionInPool("");
        } catch (JCO.Exception ex) {
            System.out.println("SAP连接池[" + id + "]组登录异常:(" + ex.getKey() + ")" + ex.getMessage());
            connection = null;
        }
    } else {// ip登录
        POOL_NAME = "SAP_POOL" + id;
        try {
            connection = getConnectionInPool(POOL_NAME);
            if (connection == null) {
                init(id);
                connection = getConnectionInPool(POOL_NAME);
            }
        } catch (JCO.Exception ex) {
            System.out.println("SAP连接池[" + id + "]IP登录异常:(" + ex.getKey() + ")" + ex.getMessage());
            connection = null;
        }
    }

    if (connection == null) {
        return getAvailableConnection(groupFlag, interfaceId, getNextId(id));
    } else {
        connection.connect();
        if (!connection.isAlive()) {
            System.out.println("SAP连接池[" + id + "]没有可用连接,切换下一个");
            return getAvailableConnection(groupFlag, interfaceId, getNextId(id));
        }
    }

    return connection;
}

JCO重连SAP

标签:nec   user   warnings   warning   tab   isa   连接   host   init   

原文地址:https://blog.51cto.com/gleolee/2512110

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!