代码实现如下,从上到下依次调用,直到被获取连接为止。
JCOProvider类似注册中心,注册连接信息。
如有问题请参考:
package pmo.basiset.web.customer;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
public class JCOProvider implements DestinationDataProvider {
private Map providers = new HashMap();
@Override
public Properties getDestinationProperties(String destName) {
if (destName == null)
throw new NullPointerException("请指定目的名称");
if (providers.size() == 0)
throw new IllegalStateException("请加入一个目的连接参数属性给提供者");
return (Properties)providers.get(destName);
}
// 没有实现事件处理
@Override
public boolean supportsEvents(){
return false;
}
@Override
public void setDestinationDataEventListener(DestinationDataEventListener listener) {
throw new UnsupportedOperationException();
}
public void addDestinationProperties(String destName, Properties provider) {
providers.put(destName, provider);
}
}
package pmo.modules.cost.web.cntc.web.materialtract;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Properties;
import org.springframework.stereotype.Service;
import pmo.basiset.web.customer.JCOProvider;
import com.pm360.epip.base.context.Context;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.Environment;
@Service("SAPConnectionPool")
public class SAPConnectionPool {
private static final String SAP_MATER_CONN="SAP_CONN";
public static JCoDestination getSAPDestination(){
try {
JCoDestination dest = JCoDestinationManager.getDestination(SAP_CONN);
return dest;
} catch (JCoException ex) {
return RegetJocodestination();
}
}
/**
* 重新获取JCODestination
* @return
*/
public static JCoDestination RegetJocodestination(){
try{
System.out.println("开始读取properties文件!!!");
Properties connectProperties = new Properties();
//获取配置文件系统路径解析
String filePath = Context.ROOT_DISK+"//WEB-INF//xxx.properties";
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
if(bufferedReader != null){
connectProperties.load(bufferedReader);
}
System.out.println("properties信息="+connectProperties+"!!!");
JCOProvider provider = new JCOProvider();
provider.addDestinationProperties(SAP_MATER_CONN, connectProperties);
Environment.registerDestinationDataProvider(provider);
try {
JCoDestination dest = JCoDestinationManager.getDestination(SAP_CONN);
return dest;
} catch (JCoException ex) {
System.out.println(ex);
System.out.println("重新连接失败");
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
private List<Map<String,Object>> querySapMaterialTractGridData(boolean result,Map<String,Object> whereMap,String user) throws ServiceBussiException {
List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
try {
System.out.println("开始获取sap连接!!!");
JCoDestination dest = SAPConnectionPool.getSAPDestination();//获取连接
dest.ping();
if(Validator.isEmpty(dest)){
result = false;
throw new ServiceBussiException("sap通道连接失败!");
}
System.out.print("获取连接成功!!!");
} catch (JCoException e) {
result = false;
throw new ServiceBussiException("sap通信失败!");
}
}