| | |
| | | package org.jeecg.config.sap; |
| | | |
| | | import com.sap.conn.jco.*; |
| | | import com.sap.conn.jco.JCoDestination; |
| | | import com.sap.conn.jco.JCoDestinationManager; |
| | | import com.sap.conn.jco.JCoException; |
| | | import com.sap.conn.jco.ext.DestinationDataProvider; |
| | | import com.sap.conn.jco.ext.Environment; |
| | | import lombok.Getter; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.util.Properties; |
| | | |
| | | @Component |
| | | @Slf4j |
| | | @Getter |
| | | public class SapRfcConnectionManager { |
| | | |
| | | @Value("${sap.rfc.destination}") |
| | |
| | | @Value("${sap.rfc.lang}") |
| | | private String lang; |
| | | |
| | | private String poolSize = "5"; |
| | | @Value("${sap.rfc.poolSize}") |
| | | private String poolSize; |
| | | |
| | | private String idleTimeout = "10000"; |
| | | @Value("${sap.rfc.expirationTime}") |
| | | private String expirationTime; |
| | | |
| | | @Value("${sap.rfc.peekLimit}") |
| | | private String peekLimit = "10"; |
| | | |
| | | private JCoDestination destination; |
| | |
| | | connectProperties.setProperty(DestinationDataProvider.JCO_LANG, lang); |
| | | connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, peekLimit); |
| | | connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, poolSize); |
| | | connectProperties.setProperty(DestinationDataProvider.JCO_EXPIRATION_TIME, idleTimeout); |
| | | connectProperties.setProperty(DestinationDataProvider.JCO_EXPIRATION_TIME, expirationTime); |
| | | |
| | | // 创建动态目的地(避免依赖 SM59 配置) |
| | | CustomDestinationDataProvider provider = new CustomDestinationDataProvider(); |
| | | provider.addDestination(destinationName, connectProperties); |
| | | CustomDestinationDataProvider provider = new CustomDestinationDataProvider(destinationName, connectProperties); |
| | | |
| | | // 设置全局目的地提供者 |
| | | // Environment.registerDestinationDataProvider(provider); |
| | | |
| | | log.info("properties = " + connectProperties); |
| | | |
| | | //生成配置文件,JCoDestinationManager.getDestination()调用时会需要该连接配置文件,后缀名需要为jcoDestination |
| | | FileOutputStream fos = null; |
| | | String suffix = "jcoDestination"; |
| | | File cfg = new File(destinationName + "." + suffix); |
| | | if (!cfg.exists()) { |
| | | try { |
| | | fos = new FileOutputStream(cfg, false); |
| | | connectProperties.store(fos, "for tests only !"); |
| | | fos.close(); |
| | | } catch (Exception var9) { |
| | | throw new Exception("Unable to create the destination file " + cfg.getName(), var9); |
| | | } finally { |
| | | if (null != fos) { |
| | | fos.close(); |
| | | } |
| | | } |
| | | } |
| | | Environment.registerDestinationDataProvider(provider); |
| | | |
| | | // 获取目的地 |
| | | this.destination = JCoDestinationManager.getDestination(destinationName); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @return |
| | | */ |
| | | public JCoDestination getDestination() { |
| | | return destination; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | */ |
| | | public void destroy() { |
| | | if (destination != null) { |
| | | // destination; |
| | | } |
| | | } |
| | | } |