diff --git a/src/main/java/com/zcloud/config/MesDataSourceConfig.java b/src/main/java/com/zcloud/config/MesDataSourceConfig.java new file mode 100644 index 0000000..438a02f --- /dev/null +++ b/src/main/java/com/zcloud/config/MesDataSourceConfig.java @@ -0,0 +1,66 @@ +package com.zcloud.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; + +import javax.sql.DataSource; +import java.sql.SQLException; + +/** + * 说明:MES数据源配置 + */ +@Configuration +@MapperScan(basePackages = MesDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mesSqlSessionFactory") +public class MesDataSourceConfig { + + static final String PACKAGE = "com.zcloud.mapper.dsno2.monitoring"; //mapper 目录 + static final String MAPPER_LOCATION = "classpath:mybatis/dsno2/monitoring/*.xml"; //扫描的 xml 目录 + static final String CONFIG_LOCATION = "classpath:mybatis/dsno2/mybatis-config.xml"; //自定义的mybatis config 文件位置 + static final String TYPE_ALIASES_PACKAGE = "com.zcloud.entity"; //扫描的 实体类 目录 + + @Value("${datasource.mes.url}") + private String url; + + @Value("${datasource.mes.username}") + private String user; + + @Value("${datasource.mes.password}") + private String password; + + @Value("${datasource.mes.driver-class-name}") + private String driverClass; + + @Bean(name = "mesDataSource") + public DataSource mesDataSource() throws SQLException { + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setDriverClassName(driverClass); + dataSource.setUrl(url); + dataSource.setUsername(user); + dataSource.setPassword(password); + return dataSource; + } + + @Bean(name = "mesTransactionManager") + public DataSourceTransactionManager mesTransactionManager() throws SQLException { + return new DataSourceTransactionManager(mesDataSource()); + } + + @Bean(name = "mesSqlSessionFactory") + public SqlSessionFactory mesSqlSessionFactory(@Qualifier("mesDataSource") DataSource mesDataSource) throws Exception { + final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); + sessionFactory.setDataSource(mesDataSource); + sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MesDataSourceConfig.MAPPER_LOCATION)); + sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MesDataSourceConfig.CONFIG_LOCATION)); + sessionFactory.setTypeAliasesPackage(MesDataSourceConfig.TYPE_ALIASES_PACKAGE); + return sessionFactory.getObject(); + } +} \ No newline at end of file diff --git a/src/main/java/com/zcloud/mapper/dsno2/monitoring/MesDataMapper.java b/src/main/java/com/zcloud/mapper/dsno2/monitoring/MesDataMapper.java new file mode 100644 index 0000000..18e0801 --- /dev/null +++ b/src/main/java/com/zcloud/mapper/dsno2/monitoring/MesDataMapper.java @@ -0,0 +1,34 @@ +package com.zcloud.mapper.dsno2.monitoring; + +import com.zcloud.entity.PageData; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +/** + * MES数据访问接口 + */ +@Repository +public interface MesDataMapper { + + /** + * 获取MES系统中的所有数据点 + * @return 数据点列表 + */ + List> listAllMesData(); + + /** + * 根据条件查询MES数据 + * @param pd 查询条件 + * @return 数据列表 + */ + List listMesDataByCondition(PageData pd); + + /** + * 批量保存从MES获取的数据 + * @param dataList 数据列表 + */ + void saveBatchFromMes(List> dataList); +} \ No newline at end of file diff --git a/src/main/java/com/zcloud/scheduled/dataDocking/MesDataScheduled.java b/src/main/java/com/zcloud/scheduled/dataDocking/MesDataScheduled.java new file mode 100644 index 0000000..c5517bd --- /dev/null +++ b/src/main/java/com/zcloud/scheduled/dataDocking/MesDataScheduled.java @@ -0,0 +1,93 @@ +package com.zcloud.scheduled.dataDocking; + +import com.alibaba.fastjson.JSONObject; +import com.xxl.job.core.context.XxlJobHelper; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.XxlJob; +import com.zcloud.mapper.dsno2.monitoring.MesDataMapper; +import com.zcloud.util.DateUtil; +import com.zcloud.util.RedisUtil; +import com.zcloud.util.UuidUtil; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.*; + +/** + * MES系统数据对接定时任务 + * 从MES系统的Oracle数据库视图中获取数据 + * + * @author + */ +@Component +public class MesDataScheduled extends IJobHandler { + + @Resource + private MesDataMapper mesDataMapper; + + @Resource + private RedisUtil redisUtil; + + @Override + @XxlJob(value = "mesDataJob") + public void execute() throws Exception { + XxlJobHelper.log("开始执行MES系统数据对接任务"); + + // 处理批次 ID + String processingBatchId = UuidUtil.get32UUID(); + // 批处理时间 + String processingTime = DateUtil.date2Str(new Date()); + + try { + // 从MES系统获取数据 + List> mesDataList = mesDataMapper.listAllMesData(); + XxlJobHelper.log("从MES系统获取到{}条数据", mesDataList.size()); + + // 处理数据 + for (Map item : mesDataList) { + // 添加处理时间和批次ID + item.put("PROCESSING_TIME", processingTime); + item.put("PROCESSING_BATCH_ID", processingBatchId); + + // 处理空值 + item.replaceAll((k, v) -> v == null ? "" : v); + } + + // 保存数据到数据库 +// if (!mesDataList.isEmpty()) { +// mesDataMapper.saveBatchFromMes(mesDataList); +// XxlJobHelper.log("成功保存{}条MES数据到本地数据库", mesDataList.size()); +// +// // 将数据存入Redis缓存 +// redisUtil.set("MES_DATA_LIST", JSONObject.toJSONString(mesDataList)); +// XxlJobHelper.log("已将MES数据存入Redis缓存"); +// } + + XxlJobHelper.log("MES系统数据对接任务执行完成"); + } catch (Exception e) { + XxlJobHelper.log("MES系统数据对接任务执行异常: {}", e.getMessage()); + throw e; + } + } + + @Override + public void init() { + // 初始化操作 + } + + @Override + public void destroy() { + // 销毁操作 + } + + @Scheduled(cron ="*/5 * * * * ?") + public void scheduled(){ + try { + System.out.println( "============定时查询MES数据=========="); + List> mesDataList = mesDataMapper.listAllMesData(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties new file mode 100644 index 0000000..8021c66 --- /dev/null +++ b/src/main/resources/application-local.properties @@ -0,0 +1,62 @@ +datasource.no1.driver-class-name: com.mysql.cj.jdbc.Driver +datasource.no1.url=jdbc:mysql://127.0.0.1:3306/integrated_yjb?allowPublicKeyRetrieval=true&serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 +datasource.no1.username=root +datasource.no1.password=Mysql@zcloud33080 +# +datasource.no2.driver-class-name:org.postgresql.Driver +datasource.no2.url=jdbc:postgresql://127.0.0.1:3306/postgres?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=UTC +datasource.no2.username=postgres +datasource.no2.password=DjIGYM8k0h +#MES系统数据库配置 +datasource.mes.driver-class-name=oracle.jdbc.OracleDriver +datasource.mes.url=jdbc:oracle:thin:@172.16.90.252:1521/jymes +datasource.mes.username=drzf +datasource.mes.password=drzf +#redis +spring.redis.host=10.199.64.30 +spring.redis.password=5.5pYdZqHxpR#9%W. +spring.redis.database=4 +spring.redis.port=16379 + +#druid连接池 +spring.datasource.type:com.alibaba.druid.pool.DruidDataSource +#最大活跃数 +spring.datasource.maxActive:20 +#初始化数量 +spring.datasource.initialSize:1 +#最大连接等待超时时间 +spring.datasource.maxWait:60000 +#打开PSCache,并且指定每个连接PSCache的大小 +spring.datasource.poolPreparedStatements:true +spring.datasource.maxPoolPreparedStatementPerConnectionSize:20 +#通过connectionProperties属性来打开mergeSql功能;慢SQL记录 +#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 +spring.datasource.minIdle:1 +spring.datasource.timeBetweenEvictionRunsMillis:60000 +spring.datasource.minEvictableIdleTimeMillis:300000 +spring.datasource.validationQuery:select 1 from dual +spring.datasource.testWhileIdle:true +spring.datasource.testOnBorrow:false +spring.datasource.testOnReturn:false +#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙 +filters:stat, wall, log4j +#缓存配置文件位置 +spring.cache.ehcache.cofnig=ehcache.xml +#配置这句话,控制台输出sql语句 +logging.level.com.zcloud.mapper=debug +#上传文件大小限制 +spring.servlet.multipart.max-file-size=500MB +spring.servlet.multipart.max-request-size=500MB +#activiti模型检测 +spring.activiti.check-process-definitions=false +#字符 +server.servlet.encoding.charset=UTF-8 +server.servlet.encoding.force=true +server.servlet.encoding.enabled=true +spring.main.banner-mode=off + +xxl.job.admin.addresses=http://192.168.20.230:28080/xxl-job-admin +xxl.job.accessToken=yjb.prod +xxl.job.port=1211 +xxl.job.executor.appname=yjb-job-normal-task +xxl.job.executor.logretentiondays=30 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6bbc7d5..9f7fd8b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ spring.application.name=integrated_whb_scheduled server.port=19099 -spring.profiles.active=master +spring.profiles.active=local diff --git a/src/main/resources/mybatis/dsno2/monitoring/MesDataMapper.xml b/src/main/resources/mybatis/dsno2/monitoring/MesDataMapper.xml new file mode 100644 index 0000000..13da764 --- /dev/null +++ b/src/main/resources/mybatis/dsno2/monitoring/MesDataMapper.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + INSERT INTO target_table ( + ID, + DEVICE_ID, + DEVICE_NAME, + CURRENT_VALUE, + GATHER_TIME, + PROCESSING_TIME, + PROCESSING_BATCH_ID + ) VALUES + + ( + #{item.ID, jdbcType=VARCHAR}, + #{item.DEVICE_ID, jdbcType=VARCHAR}, + #{item.DEVICE_NAME, jdbcType=VARCHAR}, + #{item.CURRENT_VALUE, jdbcType=VARCHAR}, + #{item.GATHER_TIME, jdbcType=TIMESTAMP}, + #{item.PROCESSING_TIME, jdbcType=TIMESTAMP}, + #{item.PROCESSING_BATCH_ID, jdbcType=VARCHAR} + ) + + + + \ No newline at end of file