修复能重复添加队列问题

新用户授权后自动添加进队列
(待添加功能,1、执行过程中授权失败将取消此api添加进队列2、)
This commit is contained in:
APLS 2020-04-22 23:17:14 +08:00
parent 8a5f152315
commit 516812f380
4 changed files with 46 additions and 11 deletions

View File

@ -47,5 +47,12 @@ public class TestController {
return "ok";
}
@GetMapping("/emptyRedis")
public String emptyRedis() {
redisUtil.deleteALL();
return "ok";
}
}

View File

@ -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();
}

View File

@ -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<Outlook>().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;
}
}

View File

@ -49,6 +49,19 @@ public class RedisUtil {
}
}
/**
* 删除所有键值对
* @title deleteALl
* @description
* @author 落叶随风
* @updateTime 2020/4/22 22:53
* @throws
*/
public void deleteALL() {
Set<String> keys = redisTemplate.keys("*");
redisTemplate.delete(keys);
}
/**
* 根据key 获取过期时间
*