diff --git a/src/main/java/io/qyi/e5/controller/TestController.java b/src/main/java/io/qyi/e5/controller/TestController.java index 3475272..5009e66 100644 --- a/src/main/java/io/qyi/e5/controller/TestController.java +++ b/src/main/java/io/qyi/e5/controller/TestController.java @@ -47,5 +47,12 @@ public class TestController { return "ok"; } + @GetMapping("/emptyRedis") + public String emptyRedis() { + redisUtil.deleteALL(); + return "ok"; + } + + } diff --git a/src/main/java/io/qyi/e5/outlook/controller/AuthController.java b/src/main/java/io/qyi/e5/outlook/controller/AuthController.java index 61ef64d..a9a497c 100644 --- a/src/main/java/io/qyi/e5/outlook/controller/AuthController.java +++ b/src/main/java/io/qyi/e5/outlook/controller/AuthController.java @@ -6,6 +6,7 @@ import io.qyi.e5.bean.result.ResultEnum; import io.qyi.e5.config.security.UsernamePasswordAuthenticationToken; import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.service.IOutlookService; +import io.qyi.e5.service.task.ITask; import io.qyi.e5.util.EncryptUtil; import io.qyi.e5.util.ResultUtil; import io.qyi.e5.util.redis.RedisUtil; @@ -42,6 +43,9 @@ public class AuthController { @Value("${outlook.authorize.url}") String authorizeUrl; + @Autowired + ITask Task; + @RequestMapping("/receive") public Result Receive(String code, String state, String session_state) throws Exception { if (!redisUtil.hasKey(states + state)) { @@ -62,6 +66,8 @@ public class AuthController { if (!authorization_code) { return ResultUtil.error(-3, "clientId 或 clientSecret 填写错误!授权失败!"); } + /*添加此用户进消息队列*/ + Task.executeE5(outlook.getGithubId()); return ResultUtil.success(); } diff --git a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java index 29d0a44..88b7bb4 100644 --- a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java +++ b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.service.IOutlookService; import io.qyi.e5.service.task.ITask; +import io.qyi.e5.util.redis.RedisUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.MessageProperties; @@ -33,6 +34,9 @@ public class TaskImpl implements ITask { @Autowired RabbitTemplate rabbitTemplate; + @Autowired + RedisUtil redisUtil; + @Override @Async public void sendTaskOutlookMQ(int github_id) { @@ -42,9 +46,12 @@ public class TaskImpl implements ITask { return; } /*根据用户设置生成随机数*/ - String Expiration = getRandom(Outlook.getCronTimeRandomStart(), Outlook.getCronTimeRandomEnd()); - send(github_id, Expiration); - + int Expiration = getRandom(Outlook.getCronTimeRandomStart(), Outlook.getCronTimeRandomEnd()); + /*将此用户信息加入redis,如果存在则代表在队列中,同时提前10秒过期*/ + if (!redisUtil.hasKey("user.mq:" + github_id)) { + redisUtil.set("user.mq:" + github_id, 0, Expiration - 10); + send(github_id, Expiration* 1000); + } } @Override @@ -55,14 +62,16 @@ public class TaskImpl implements ITask { while (iterator.hasNext()) { Outlook next = iterator.next(); /*根据用户设置生成随机数*/ - String Expiration = getRandom(next.getCronTimeRandomStart(), next.getCronTimeRandomEnd()); -// System.out.println("生成随机调用时间,github ID" + next.getGithubId() + ",时间:" + Expiration); - send(next.getGithubId(), Expiration); + int Expiration = getRandom(next.getCronTimeRandomStart(), next.getCronTimeRandomEnd()); + /*将此用户信息加入redis,如果存在则代表在队列中,同时提前10秒过期*/ + if (!redisUtil.hasKey("user.mq:" + next.getGithubId())) { + redisUtil.set("user.mq:" + next.getGithubId(), 0, Expiration - 10); + send(next.getGithubId(), Expiration * 1000); + } } } @Override - @Async public void executeE5(int github_id) { Outlook Outlook = outlookService.getOne(new QueryWrapper().eq("github_id", github_id)); if (Outlook == null) { @@ -82,7 +91,7 @@ public class TaskImpl implements ITask { * @Author: 落叶随风 * @Date: 2020/4/16 */ - public void send(Object msg, String Expiration) { + public void send(Object msg, int Expiration) { CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); rabbitTemplate.convertAndSend("delay", "delay", msg, message -> { @@ -90,7 +99,7 @@ public class TaskImpl implements ITask { // 设置这条消息的过期时间 // messageProperties.setExpiration(Expiration); - messageProperties.setHeader("x-delay",Expiration); + messageProperties.setHeader("x-delay", Expiration); return message; }, correlationData); } @@ -105,9 +114,9 @@ public class TaskImpl implements ITask { * @Author: 落叶随风 * @Date: 2020/4/16 */ - public String getRandom(int start, int end) { + public int getRandom(int start, int end) { Random r = new Random(); - String Expiration = String.valueOf((r.nextInt(end - start + 1) + start) * 1000); + int Expiration = (r.nextInt(end - start + 1) + start); return Expiration; } } diff --git a/src/main/java/io/qyi/e5/util/redis/RedisUtil.java b/src/main/java/io/qyi/e5/util/redis/RedisUtil.java index 9a6bddb..ec89cf2 100644 --- a/src/main/java/io/qyi/e5/util/redis/RedisUtil.java +++ b/src/main/java/io/qyi/e5/util/redis/RedisUtil.java @@ -49,6 +49,19 @@ public class RedisUtil { } } + /** + * 删除所有键值对 + * @title deleteALl + * @description + * @author 落叶随风 + * @updateTime 2020/4/22 22:53 + * @throws + */ + public void deleteALL() { + Set keys = redisTemplate.keys("*"); + redisTemplate.delete(keys); + } + /** * 根据key 获取过期时间 *