From 77742e3c2fab838dbadb8667a11e6f350f1a49f1 Mon Sep 17 00:00:00 2001 From: zhaoxiaolin Date: Thu, 9 Nov 2023 09:26:08 +0800 Subject: [PATCH] =?UTF-8?q?scada=E6=9C=8D=E5=8A=A1=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../op/scada/config/DestinationManager.java | 74 +++++++++++++++++++ .../op/scada/config/DynamicDatasource.java | 51 +++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 op-modules/op-scada/src/main/java/com/op/scada/config/DestinationManager.java create mode 100644 op-modules/op-scada/src/main/java/com/op/scada/config/DynamicDatasource.java diff --git a/op-modules/op-scada/src/main/java/com/op/scada/config/DestinationManager.java b/op-modules/op-scada/src/main/java/com/op/scada/config/DestinationManager.java new file mode 100644 index 00000000..8f860aa3 --- /dev/null +++ b/op-modules/op-scada/src/main/java/com/op/scada/config/DestinationManager.java @@ -0,0 +1,74 @@ +package com.op.scada.config; + + +import com.sap.conn.jco.ext.DestinationDataProvider; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Properties; + + +@Configuration +public class DestinationManager { + @Value("${sap.ashost}") + private String ashost; + + @Value("${sap.sysnr}") + private String sysnr; + + @Value("${sap.client}") + private String client; + + @Value("${sap.user}") + private String user; + + @Value("${sap.passwd}") + private String passwd; + + @Value("${sap.lang}") + private String lang; + + @Value("${sap.pool_capacity}") + private String pool_capacity; + + @Value("${sap.peak_limit}") + private String peak_limit; + + @Value("${sap.saprouter}") + private String saprouter; + + private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; + @Bean + public void CreateABAP(){ + + Properties connectProperties = new Properties(); + connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, ashost); + connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, sysnr); + connectProperties.setProperty(DestinationDataProvider.JCO_USER, user); + connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, passwd); + connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, client); + connectProperties.setProperty(DestinationDataProvider.JCO_LANG, lang); + connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,peak_limit); + connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY,pool_capacity); + connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER,saprouter); + createDataFile(ABAP_AS_POOLED,"jcoDestination",connectProperties); + } + private static void createDataFile(String name, String suffix, Properties properties) { + // TODO Auto-generated method stub + File cfg = new File(name + "." + suffix); + if (cfg.exists()) { + cfg.deleteOnExit(); + } + try { + FileOutputStream fos = new FileOutputStream(cfg,false); + properties.store(fos, "for test only!"); + } catch (Exception e) { + // TODO: handle exception +// System.out.println("Create Data file fault,error msg:" + e.toString()); + throw new RuntimeException("无法创建目标文件" + cfg.getName()); + } + } +} diff --git a/op-modules/op-scada/src/main/java/com/op/scada/config/DynamicDatasource.java b/op-modules/op-scada/src/main/java/com/op/scada/config/DynamicDatasource.java new file mode 100644 index 00000000..68c1e50f --- /dev/null +++ b/op-modules/op-scada/src/main/java/com/op/scada/config/DynamicDatasource.java @@ -0,0 +1,51 @@ +package com.op.scada.config; + +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; +import com.op.common.core.domain.R; +import com.op.common.datasource.creator.DynamicDatasourceCreator; +import com.op.system.api.RemoteUserService; +import com.op.system.api.domain.SysUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +/** + * 初始化动态数据源 + * + * @ClassName: DynamicDatasource + * @Description: TODO + * @author shichangzhou + * @date 2023年4月19日 下午1:01:30 + */ +@Component +public class DynamicDatasource { + + @Resource + private DynamicDatasourceCreator dynamicDatasourceCreator; + @Autowired + private RemoteUserService remoteUserService; + + @PostConstruct + public void init() { + // 加载sf-cloud库的sys_datasource + SysUser sysUser = new SysUser(); + sysUser.setUserId(1L); + R>> dateSources0 = remoteUserService.getPoolNameList(sysUser); + List> dateSources = dateSources0.getData(); + for (Map dateSource : dateSources) { + DataSourceProperty sdp = new DataSourceProperty(); + sdp.setUrl(dateSource.get("url")); + sdp.setUsername(dateSource.get("userName")); + sdp.setPassword(dateSource.get("password")); + sdp.setDriverClassName(dateSource.get("driveClassName")); + sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key + sdp.setLazy(false); + dynamicDatasourceCreator.createDynamicDataSource(sdp); + } + + } +}