JCO重连SAP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JCO重连SAP相关的知识,希望对你有一定的参考价值。

记录一下

...

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的主要内容,如果未能解决你的问题,请参考以下文章

SAP R3和JAVA交换数据之JCO

JCo for SAP中的嵌套结构?

调用JCO连接SAP的问题

如何使用RFC在JCO表中设置多个数据?

java程序无法远程调用sap函数

在 GlassFish v2.1 中永久加载的 SAP JCo 连接器(无法卸载)