Java 使用sapjco连接到SAP BAPI的两个web应用程序
我在WebSphereApplicationServer8.5上为RHEL6(x64)部署了两个web应用程序。两个web应用程序都使用SAPJCO库连接到SAP ECC6中部署的BAPI。两个应用程序都需要访问相同的BAPI才能创建购买请求。因此,我们使用从SAP的CustomDestinationDataProvider修改的相同代码进行连接。为了确保应用程序只注册一次,我们使用Spring将其设置为单例 但是,当应用程序启动时,当执行Java 使用sapjco连接到SAP BAPI的两个web应用程序,java,sap,jco,Java,Sap,Jco,我在WebSphereApplicationServer8.5上为RHEL6(x64)部署了两个web应用程序。两个web应用程序都使用SAPJCO库连接到SAP ECC6中部署的BAPI。两个应用程序都需要访问相同的BAPI才能创建购买请求。因此,我们使用从SAP的CustomDestinationDataProvider修改的相同代码进行连接。为了确保应用程序只注册一次,我们使用Spring将其设置为单例 但是,当应用程序启动时,当执行“com.sap.conn.jco.ext.Enviro
“com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider)”
时,一个应用程序(我们猜它是在第一个应用程序之后加载的)遇到“java.lang.IllegalStateException:DestinationDataProvider已注册”
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>
公共类CustomDestinationDataProvider{
公共CustomDestinationDataProvider(){
}
公共静态BapiConfigBean BapiConfigBean;
//自定义目标数据提供程序实现DestinationDataProvider和
//提供至少getDestinationProperties(字符串)的实现。
//只要可能,实现应该支持事件并通知JCo运行时
//如果正在创建、更改或删除目标,则为JCo运行时
//将定期检查缓存的目标配置是否仍然有效,从而导致
//表演罚款。
公共静态类MyDestinationDataProvider实现DestinationDataProvider
{
私人目的地数据;
私有HashMap secureDBStorage=newhashmap();
公共MyDestinationDataProvider(){
}
公共属性getDestinationProperties(字符串destinationName)
{
尝试
{
//从数据库读取目标
属性p=secureDBStorage.get(destinationName);
如果(p!=null)
{
//例如,检查是否全部正确
if(p.isEmpty())
抛出新的DataProviderException(DataProviderException.Reason.INVALID_配置,
“目标配置不正确”,空);
返回p;
}
返回null;
}
捕获(运行时异常re)
{
抛出新的DataProviderException(DataProviderException.Reason.INTERNAL_ERROR,re);
}
}
//支持事件的实现必须保留提供的eventListener实例
//此侦听器实例应用于通知JCo运行时
//关于目标配置中的所有更改。
public void setDestinationDataEventListener(DestinationDataEventListener eventListener)
{
this.eL=eventListener;
}
public boolean supportsEvents()
{
返回true;
}
//以非常安全的方式保存属性的实现
void changeProperties(字符串destName、属性)
{
已同步(secureDBStorage)
{
如果(属性==null)
{
if(secureDBStorage.remove(destName)!=null)
eL.删除(名称);
}
其他的
{
secureDBStorage.put(destName,properties);
eL.updated(destName);//创建或更新
}
}
}
公共void removeDestination(字符串destName){
//TODO自动生成的方法存根
}
public void addDestination(字符串destName,
MyDestinationDataProvider(myProvider){
//TODO自动生成的方法存根
}
}//MyDestinationDataProvider的结尾
//业务逻辑
void executeCalls(字符串destName)
{
JCoDestination dest;
尝试
{
dest=jcodestationmanager.getDestination(destName);
目标ping();
System.out.println(“目的地”+destName+“工作”);
}
捕获(JCoException e)
{
e、 printStackTrace();
System.out.println(“在目标上执行”+destName+“失败”);
}
}
静态属性getDestinationPropertiesFromUI()
{
//调整参数以配置有效的目标
Properties connectProperties=新属性();
setProperties(DestinationDataProvider.JCO_ASHOST,getBapiConfigBean().getServerIp());
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,getBapiConfigBean().getSystemNumber());
setProperties(DestinationDataProvider.JCO_客户端,getBapiConfigBean().getClientId());
setProperties(DestinationDataProvider.JCO_用户,getBapiConfigBean().getUserName());
setProperties(DestinationDataProvider.JCO_PASSWD,getBapiConfigBean().getUserPassword());
setProperties(DestinationDataProvider.JCO_LANG,getBapiConfigBean().getClientLang());
归还财产;
}
静态MyDestinationDataProvider myProvider=null;//2014-06-30 00:30
公共静态void initProvider(BapiConfigBean BapiConfigBean){
CustomDestinationDataProvider.destroy();
setBapiConfigBean(bapiConfigBean);
myProvider=新的MyDestinationDataProvider();
字符串destName=getBapiConfigBean().getSapDestname();
尝试
{
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
CustomDestinationDataProvider测试=新建CustomDestinationDataProvider();
changeProperties(destName,getDestinationPropertiesFromUI());
test.executeCalls(destName);
}
catch(非法状态异常ProviderReadyRegisteredException)
{
试一试{
JCoDestination jcodest=JCoDestinationManager.getDestination(getBapiConfigBean().getSapDestname());
}捕获(JCoException exJCo){
//TODO:添加异常处理并向ui发送友好消息
}
}
}
公共静态BapiConfigBean getBapiConfigBean(){
返回bapiConfigB