From f4774578fb4d66e039cadde997b1c6efefdfa855 Mon Sep 17 00:00:00 2001 From: x_z Date: Fri, 11 Nov 2022 18:15:08 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=8C=89=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=AE=B5=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=202.=E8=B0=83=E6=95=B4redis=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 33 +++-- .../glxp/udidl/admin/constant/Constant.java | 10 ++ .../udidl/admin/dao/info/CompanyMapper.java | 2 + .../dataSync/DeviceDownloadService.java | 7 +- .../admin/service/info/CompanyService.java | 2 + .../service/info/impl/CompanyServiceImpl.java | 5 + .../service/udi/impl/JobLogServiceImpl.java | 5 +- .../com/glxp/udidl/admin/util/RedisUtil.java | 126 +++++++++++++++--- src/main/resources/application-dev.yml | 13 +- .../mybatis/mapper/info/CompanyMapper.xml | 4 +- .../udidl/admin/ApiAdminApplicationTests.java | 13 -- 11 files changed, 166 insertions(+), 54 deletions(-) delete mode 100644 src/test/java/com/glxp/udidl/admin/ApiAdminApplicationTests.java diff --git a/pom.xml b/pom.xml index a7e58a5..4702528 100644 --- a/pom.xml +++ b/pom.xml @@ -107,6 +107,11 @@ 1.4.2 + + org.springframework.boot + spring-boot-starter-data-redis + + org.springframework.boot @@ -120,11 +125,6 @@ mysql-connector-java runtime - - org.springframework.boot - spring-boot-starter-test - test - org.projectlombok @@ -138,6 +138,12 @@ fastjson 2.0.2 + + + com.google.code.gson + gson + 2.10 + @@ -152,6 +158,11 @@ true + + cn.hutool + hutool-all + 5.8.9 + org.apache.commons commons-lang3 @@ -161,12 +172,6 @@ poi-ooxml 4.1.0 - - cn.hutool - hutool-all - 5.5.1 - - javax.servlet javax.servlet-api @@ -174,12 +179,6 @@ provided - - redis.clients - jedis - 4.2.3 - - javax.xml.bind jaxb-api diff --git a/src/main/java/com/glxp/udidl/admin/constant/Constant.java b/src/main/java/com/glxp/udidl/admin/constant/Constant.java index 3db3499..ba0de36 100644 --- a/src/main/java/com/glxp/udidl/admin/constant/Constant.java +++ b/src/main/java/com/glxp/udidl/admin/constant/Constant.java @@ -35,4 +35,14 @@ public interface Constant { */ String LOG_TYPE_ERROR = "error"; + /** + * 任务类型 - 下载任务 + */ + Integer TASK_TYPE_DOWNLOAD = 0; + + /** + * 任务类型 - 更新任务 + */ + Integer TASK_TYPE_UPDATE = 1; + } diff --git a/src/main/java/com/glxp/udidl/admin/dao/info/CompanyMapper.java b/src/main/java/com/glxp/udidl/admin/dao/info/CompanyMapper.java index 08a71ea..c8b1697 100644 --- a/src/main/java/com/glxp/udidl/admin/dao/info/CompanyMapper.java +++ b/src/main/java/com/glxp/udidl/admin/dao/info/CompanyMapper.java @@ -10,4 +10,6 @@ public interface CompanyMapper extends BaseMapper { boolean modifyCompany(CompanyEntity companyEntity); boolean insertCompany(CompanyEntity companyEntity); + + CompanyEntity findByTaskType(Integer taskType); } diff --git a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java index 2f0cf43..24d64d3 100644 --- a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java +++ b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java @@ -81,7 +81,8 @@ public class DeviceDownloadService { public String getToken() { token = redisUtil.get("UDISYNC_TOKEN"); if (StrUtil.isEmpty(token)) { - companyEntity = companyService.findCompany(); + //查询下载数据使用的appid + companyEntity = companyService.findByTaskType(Constant.TASK_TYPE_DOWNLOAD); TokenRequest tokenRequest = new TokenRequest(); tokenRequest.setAppId(companyEntity.getAppId()); tokenRequest.setAppSecret(companyEntity.getAppSecret()); @@ -106,7 +107,7 @@ public class DeviceDownloadService { } } catch (Exception e) { - e.printStackTrace(); + log.error("国家库网络服务器异常", e); return "国家库网络服务器异常!"; } } @@ -604,7 +605,7 @@ public class DeviceDownloadService { jobLog.setCreateTime(new Date()); jobLogService.insert(jobLog); } catch (Exception e) { - e.printStackTrace(); + log.error(e.getMessage(), e); JobLog jobLog = new JobLog(); jobLog.setDownloadType(Constant.DOWNLOAD_TYPE_MANUAL); jobLog.setType(Constant.LOG_TYPE_ERROR); diff --git a/src/main/java/com/glxp/udidl/admin/service/info/CompanyService.java b/src/main/java/com/glxp/udidl/admin/service/info/CompanyService.java index 423a74e..8e195bc 100644 --- a/src/main/java/com/glxp/udidl/admin/service/info/CompanyService.java +++ b/src/main/java/com/glxp/udidl/admin/service/info/CompanyService.java @@ -9,6 +9,8 @@ public interface CompanyService { CompanyEntity findCompany(); + CompanyEntity findByTaskType(Integer taskType); + CompanyEntity findByTyshxydm(String tyshxydm); boolean modifyCompany(CompanyEntity companyEntity); diff --git a/src/main/java/com/glxp/udidl/admin/service/info/impl/CompanyServiceImpl.java b/src/main/java/com/glxp/udidl/admin/service/info/impl/CompanyServiceImpl.java index ffbc44b..de53ffd 100644 --- a/src/main/java/com/glxp/udidl/admin/service/info/impl/CompanyServiceImpl.java +++ b/src/main/java/com/glxp/udidl/admin/service/info/impl/CompanyServiceImpl.java @@ -28,6 +28,11 @@ public class CompanyServiceImpl implements CompanyService { return companyDao.findCompany(); } + @Override + public CompanyEntity findByTaskType(Integer taskType) { + return companyDao.findByTaskType(taskType); + } + @Override public CompanyEntity findByTyshxydm(String tyshxydm) { return companyDao.selectOne(new QueryWrapper().eq("tyshxydm", tyshxydm)); diff --git a/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java b/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java index 6b3bb7f..cc9b5be 100644 --- a/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java +++ b/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java @@ -19,6 +19,7 @@ import java.util.List; @Service @Slf4j +@Transactional(rollbackFor = Exception.class) public class JobLogServiceImpl implements JobLogService { @Resource @@ -41,18 +42,16 @@ public class JobLogServiceImpl implements JobLogService { return jobLogMapper.selectById(jobId); } - @Transactional(rollbackFor = Exception.class) + public int insert(JobLog jobLog) { return jobLogMapper.insert(jobLog); } - @Transactional(rollbackFor = Exception.class) public int update(JobLog jobLog) { return jobLogMapper.updateById(jobLog); } @Override - @Transactional(rollbackFor = Exception.class) public int insertLog(JobLog jobLog) { int insert = jobLogMapper.insert(jobLog); return insert; diff --git a/src/main/java/com/glxp/udidl/admin/util/RedisUtil.java b/src/main/java/com/glxp/udidl/admin/util/RedisUtil.java index 58a60cd..1aa2d71 100644 --- a/src/main/java/com/glxp/udidl/admin/util/RedisUtil.java +++ b/src/main/java/com/glxp/udidl/admin/util/RedisUtil.java @@ -1,11 +1,11 @@ package com.glxp.udidl.admin.util; import cn.hutool.core.collection.CollUtil; -import cn.hutool.db.nosql.redis.RedisDS; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import redis.clients.jedis.Jedis; +import javax.annotation.Resource; import java.util.Set; /** @@ -13,32 +13,96 @@ import java.util.Set; */ @Slf4j @Component -public class RedisUtil { +public class RedisUtil { - private final Jedis jedis = RedisDS.create().getJedis(); + @Resource + private RedisTemplate redisTemplate; + /** + * 获取字符串类型key值 + * + * @param key + * @return + */ public String get(String key) { - return jedis.get(key); + return String.valueOf(redisTemplate.opsForValue().get(key)); } - public String setEx(String key, String value, long expireTime) { - return jedis.setex(key, expireTime, value); + /** + * 获取Object类型key值 + * + * @param key + * @return + */ + public Object getObj(String key) { + return redisTemplate.opsForValue().get(key); } + /** + * 设置过期key + * + * @param key + * @param value + * @param expireTime + * @return + */ + public boolean setEx(String key, String value, long expireTime) { + boolean result = false; + try { + redisTemplate.opsForValue().set(key, value, expireTime); + result = true; + } catch (Exception e) { + log.error("RedisUtil setEx 发生异常,参数列表:key {} value {}", key, value, e); + } + return result; + } + + /** + * 判断key是否存在 + * + * @param key + * @return + */ public boolean exist(String key) { - return jedis.exists(key); + return redisTemplate.hasKey(key); } + /** + * 获取过期时间 + * + * @param key + * @return + */ public long getExpireTime(String key) { - return jedis.ttl(key); + return redisTemplate.getExpire(key); } - public String set(String key, String value) { - return jedis.set(key, value); + /** + * 设置key + * + * @param key + * @param value + * @return + */ + public boolean set(String key, T value) { + boolean result = false; + try { + redisTemplate.opsForValue().set(key, value); + result = true; + } catch (Exception e) { + log.error("RedisUtil set 发生异常,参数列表:key {} value {}", key, value, e); + } + return result; } + /** + * 获取锁 + * + * @param key + * @return + */ public boolean getLock(String key) { - if (jedis.exists(key)) { + if (exist(key)) { long expireTime = getExpireTime(key); try { Thread.sleep(expireTime * 1000); @@ -49,13 +113,43 @@ public class RedisUtil { return true; } + /** + * 获取模糊匹配key集合 + * + * @param key + * @return + */ + public Set keys(String key) { + return redisTemplate.keys(key); + } + + /** + * 删除key + * + * @param key + * @return + */ + public boolean del(String key) { + boolean result = false; + try { + redisTemplate.delete(key); + result = true; + } catch (Exception e) { + log.error("RedisUtil del 删除key值,key {} ", key, e); + } + return result; + } - //模糊匹配删除 + /** + * 模糊匹配删除 + * + * @param prex + */ public void deleteByPrex(String prex) { - Set keys = jedis.keys(prex); + Set keys = keys(prex); if (CollUtil.isNotEmpty(keys)) { - for (String str : keys) { - jedis.del(str); + for (String key : keys) { + del(key); } } } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 555e15d..2dda414 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -4,7 +4,7 @@ spring: matching-strategy: ant_path_matcher datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://192.168.0.66:3360/udidl_test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true + url: jdbc:mysql://127.0.0.1:3306/udidl_test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true username: root password: root hikari: @@ -12,6 +12,17 @@ spring: connection-timeout: 100000 #设置等待超时时间为10秒 maximum-pool-size: 60 minimum-idle: 10 + redis: + host: 127.0.0.1 + port: 6379 + password: + connect-timeout: 5000ms + lettuce: + pool: + max-active: 60 + max-idle: 10 + min-idle: 5 + max-wait: 5000ms servlet: multipart: diff --git a/src/main/resources/mybatis/mapper/info/CompanyMapper.xml b/src/main/resources/mybatis/mapper/info/CompanyMapper.xml index d1117d2..a7c0cb1 100644 --- a/src/main/resources/mybatis/mapper/info/CompanyMapper.xml +++ b/src/main/resources/mybatis/mapper/info/CompanyMapper.xml @@ -38,5 +38,7 @@ #{tyshxydm}) - + \ No newline at end of file diff --git a/src/test/java/com/glxp/udidl/admin/ApiAdminApplicationTests.java b/src/test/java/com/glxp/udidl/admin/ApiAdminApplicationTests.java deleted file mode 100644 index af3efba..0000000 --- a/src/test/java/com/glxp/udidl/admin/ApiAdminApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.glxp.udidl.admin; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class ApiAdminApplicationTests { - - @Test - void contextLoads() { - } - -}