两个使用sapjco连接到SAP BAPI的Web应用程序

时间:2014-07-03 04:34:59

标签: java sap jco

我在WebSphere Application Server 8.5上为RHEL6(x64)部署了两个Web应用程序。两个Web应用程序都使用SAPJCO库连接到SAP ECC6中部署的BAPI。两个应用程序都需要访问相同的BAPI才能创建采购申请。因此,我们使用从SAP的CustomDestinationDataProvider修改的相同代码进行连接。为了确保应用程序只注册一次,我们使用Spring将其设为单例。

但是,当应用程序启动时,一个应用程序(我们猜测它在第一个应用程序之后加载)在执行"java.lang.IllegalStateException: DestinationDataProvider already registered"时遇到"com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider)"

CustomDestinationDataProvider如下:     


    public class CustomDestinationDataProvider {
    public CustomDestinationDataProvider () {
    }

public static BapiConfigBean bapiConfigBean;

//The custom destination data provider implements DestinationDataProvider and
//provides an implementation for at least getDestinationProperties(String).
//Whenever possible the implementation should support events and notify the JCo runtime
//if a destination is being created, changed, or deleted. Otherwise JCo runtime
//will check regularly if a cached destination configuration is still valid which incurs
//a performance penalty.
public static class MyDestinationDataProvider implements DestinationDataProvider
{
    private DestinationDataEventListener eL;
    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();

    public MyDestinationDataProvider () {

    }
    public Properties getDestinationProperties(String destinationName)
    {
        try
        {
            //read the destination from DB
            Properties p = secureDBStorage.get(destinationName);

            if(p!=null)
            {
                //check if all is correct, for example
                if(p.isEmpty())
                    throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, 
                            "destination configuration is incorrect", null);
                return p;
            }

            return null;
        }
        catch(RuntimeException re)
        {
            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
        }
    }

    //An implementation supporting events has to retain the eventListener instance provided
    //by the JCo runtime. This listener instance shall be used to notify the JCo runtime
    //about all changes in destination configurations.
    public void setDestinationDataEventListener(DestinationDataEventListener eventListener)
    {
        this.eL = eventListener;
    }

    public boolean supportsEvents()
    {
        return true;
    }

    //implementation that saves the properties in a very secure way
    void changeProperties(String destName, Properties properties)
    {
        synchronized(secureDBStorage)
        {
            if(properties==null)
            {
                if(secureDBStorage.remove(destName)!=null)
                    eL.deleted(destName);
            }
            else 
            {
                secureDBStorage.put(destName, properties);
                eL.updated(destName); // create or updated
            }
        }
    }

    public void removeDestination(String destName) {
        // TODO Auto-generated method stub

    }
    public void addDestination(String destName,
            MyDestinationDataProvider myProvider) {
        // TODO Auto-generated method stub

    }

} // end of MyDestinationDataProvider


//business logic
void executeCalls(String destName)
{
    JCoDestination dest;
    try
    {
        dest = JCoDestinationManager.getDestination(destName);
        dest.ping();
        System.out.println("Destination " + destName + " works");
    }
    catch(JCoException e)
    {
        e.printStackTrace();
        System.out.println("Execution on destination " + destName+ " failed");
    }
}

static Properties getDestinationPropertiesFromUI()
{
    //adapt parameters in order to configure a valid destination
    Properties connectProperties = new Properties();
    connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, getBapiConfigBean().getServerIp());
    connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  getBapiConfigBean().getSystemNumber());
    connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, getBapiConfigBean().getClientId());
    connectProperties.setProperty(DestinationDataProvider.JCO_USER,   getBapiConfigBean().getUserName());
    connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, getBapiConfigBean().getUserPassword());
    connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   getBapiConfigBean().getClientLang());
    return connectProperties;
}

static MyDestinationDataProvider myProvider = null; //2014-06-30 00:30 

public static void initProvider(BapiConfigBean bapiConfigBean) {
    CustomDestinationDataProvider.destroy();
    setBapiConfigBean(bapiConfigBean);
    myProvider = new MyDestinationDataProvider();
    String destName = getBapiConfigBean().getSapDestname();

    try
    {

        com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
        CustomDestinationDataProvider test = new CustomDestinationDataProvider();
        myProvider.changeProperties(destName, getDestinationPropertiesFromUI());
        test.executeCalls(destName);

    }
    catch(IllegalStateException providerAlreadyRegisteredException)
    {
        try {
            JCoDestination jcodest = JCoDestinationManager.getDestination(getBapiConfigBean().getSapDestname());
        } catch (JCoException exJCo) {
            //TODO: Add exception handling and send friendly message to ui
        }
    }   
}

public static BapiConfigBean getBapiConfigBean() {
    return bapiConfigBean;
}

public static void setBapiConfigBean(BapiConfigBean bapiConfigBean) {
    CustomDestinationDataProvider.bapiConfigBean = bapiConfigBean;
}

public static void destroy() {
    try {
        Environment.unregisterDestinationDataProvider(myProvider);  
        System.out.println("Unregistered connection to SAP");
    } catch (IllegalStateException e) {  
    System.out.println("Failed to unregister connection to SAP: "+ e);
    }
}
}
</code></pre>

public static BapiConfigBean bapiConfigBean; //The custom destination data provider implements DestinationDataProvider and //provides an implementation for at least getDestinationProperties(String). //Whenever possible the implementation should support events and notify the JCo runtime //if a destination is being created, changed, or deleted. Otherwise JCo runtime //will check regularly if a cached destination configuration is still valid which incurs //a performance penalty. public static class MyDestinationDataProvider implements DestinationDataProvider { private DestinationDataEventListener eL; private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>(); public MyDestinationDataProvider () { } public Properties getDestinationProperties(String destinationName) { try { //read the destination from DB Properties p = secureDBStorage.get(destinationName); if(p!=null) { //check if all is correct, for example if(p.isEmpty()) throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null); return p; } return null; } catch(RuntimeException re) { throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re); } } //An implementation supporting events has to retain the eventListener instance provided //by the JCo runtime. This listener instance shall be used to notify the JCo runtime //about all changes in destination configurations. public void setDestinationDataEventListener(DestinationDataEventListener eventListener) { this.eL = eventListener; } public boolean supportsEvents() { return true; } //implementation that saves the properties in a very secure way void changeProperties(String destName, Properties properties) { synchronized(secureDBStorage) { if(properties==null) { if(secureDBStorage.remove(destName)!=null) eL.deleted(destName); } else { secureDBStorage.put(destName, properties); eL.updated(destName); // create or updated } } } public void removeDestination(String destName) { // TODO Auto-generated method stub } public void addDestination(String destName, MyDestinationDataProvider myProvider) { // TODO Auto-generated method stub } } // end of MyDestinationDataProvider //business logic void executeCalls(String destName) { JCoDestination dest; try { dest = JCoDestinationManager.getDestination(destName); dest.ping(); System.out.println("Destination " + destName + " works"); } catch(JCoException e) { e.printStackTrace(); System.out.println("Execution on destination " + destName+ " failed"); } } static Properties getDestinationPropertiesFromUI() { //adapt parameters in order to configure a valid destination Properties connectProperties = new Properties(); connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, getBapiConfigBean().getServerIp()); connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, getBapiConfigBean().getSystemNumber()); connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, getBapiConfigBean().getClientId()); connectProperties.setProperty(DestinationDataProvider.JCO_USER, getBapiConfigBean().getUserName()); connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, getBapiConfigBean().getUserPassword()); connectProperties.setProperty(DestinationDataProvider.JCO_LANG, getBapiConfigBean().getClientLang()); return connectProperties; } static MyDestinationDataProvider myProvider = null; //2014-06-30 00:30 public static void initProvider(BapiConfigBean bapiConfigBean) { CustomDestinationDataProvider.destroy(); setBapiConfigBean(bapiConfigBean); myProvider = new MyDestinationDataProvider(); String destName = getBapiConfigBean().getSapDestname(); try { com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider); CustomDestinationDataProvider test = new CustomDestinationDataProvider(); myProvider.changeProperties(destName, getDestinationPropertiesFromUI()); test.executeCalls(destName); } catch(IllegalStateException providerAlreadyRegisteredException) { try { JCoDestination jcodest = JCoDestinationManager.getDestination(getBapiConfigBean().getSapDestname()); } catch (JCoException exJCo) { //TODO: Add exception handling and send friendly message to ui } } } public static BapiConfigBean getBapiConfigBean() { return bapiConfigBean; } public static void setBapiConfigBean(BapiConfigBean bapiConfigBean) { CustomDestinationDataProvider.bapiConfigBean = bapiConfigBean; } public static void destroy() { try { Environment.unregisterDestinationDataProvider(myProvider); System.out.println("Unregistered connection to SAP"); } catch (IllegalStateException e) { System.out.println("Failed to unregister connection to SAP: "+ e); } } } </code></pre>

我的问题是:
1)如何使两个应用程序能够使用相同的CustomDestinationDataProvider?
2)如果2个应用程序不能使用相同的提供程序,如何使用相同的服务器/用户/客户端号码访问同一个BAPI? 在这种情况下的约束是(1)我们应该使用sapjco和(2)我们只有BAPI函数。如果您认为是一个问题,可以丢弃customDestinationDataProvider。 顺便说一句,我为长代码和糟糕的格式道歉 谢谢。

1 个答案:

答案 0 :(得分:2)

针对您的问题的一个简单解决方案是为您的两个应用程序使用不同的目标名称(尽管它们在您的情况下是相同的)。 使用两个不同的目标名称,每个应用程序一个。例如PROD_APP1,PROD_APP2

我相信所有其他事情都可以保持不变。

在理想情况下,我更喜欢更强大的解决方案。可能我认为是自定义类加载器等等

祝你好运!