From 9bcc0d83f77586d090a79d120638cc7145a34fb7 Mon Sep 17 00:00:00 2001 From: LuoYe_MyWork Date: Thu, 16 Apr 2020 17:33:34 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=A2=9E=E5=8A=A0=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=202=E3=80=81=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=98=9F=E5=88=97=E7=9A=84=E6=B6=88=E6=81=AF=E6=8D=A2?= =?UTF-8?q?=E4=B8=BAgithub=5Fid=203=E3=80=81=E6=A0=B9=E6=8D=AE=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=AE=BE=E5=AE=9A=E7=9A=84=E9=9A=8F=E6=9C=BA=E5=80=BC?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=98=9F=E5=88=97=E6=97=B6=E9=97=B4=204?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E6=94=B9=E9=98=9F=E5=88=97=E6=8E=A5=E6=94=B6?= =?UTF-8?q?=E5=8F=82=E6=95=B0outlook=E5=AF=B9=E8=B1=A1=E4=B8=BAgithub=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/qyi/e5/controller/TestController.java | 14 +-- .../service/impl/OutlookServiceImpl.java | 55 ++++++++--- .../service/rabbitMQ/impl/ListenerImpl.java | 19 +++- .../java/io/qyi/e5/service/task/ITask.java | 13 +++ .../io/qyi/e5/service/task/impl/TaskImpl.java | 98 +++++++++++++++++++ src/test/java/io/qyi/e5/string/dome01.java | 7 ++ 6 files changed, 177 insertions(+), 29 deletions(-) create mode 100644 src/main/java/io/qyi/e5/service/task/ITask.java create mode 100644 src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java diff --git a/src/main/java/io/qyi/e5/controller/TestController.java b/src/main/java/io/qyi/e5/controller/TestController.java index eb15c77..e6b1314 100644 --- a/src/main/java/io/qyi/e5/controller/TestController.java +++ b/src/main/java/io/qyi/e5/controller/TestController.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.google.gson.Gson; import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.service.IOutlookService; +import io.qyi.e5.service.task.ITask; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; @@ -29,16 +30,11 @@ public class TestController { @Autowired IOutlookService outlookService; + @Autowired + ITask Task; + @GetMapping("/send") public void aaa() { - CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); - Outlook Outlook = outlookService.getOne(new QueryWrapper().eq("github_id", 19658189)); - - rabbitTemplate.convertAndSend("delay", "delay", Outlook, message -> { - MessageProperties messageProperties = message.getMessageProperties(); - // 设置这条消息的过期时间 - messageProperties.setExpiration("5000"); - return message; - }, correlationData); + Task.sendTaskOutlookMQ(19658189); } } diff --git a/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java b/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java index 12101e0..a1f9cb8 100644 --- a/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java +++ b/src/main/java/io/qyi/e5/outlook/service/impl/OutlookServiceImpl.java @@ -5,6 +5,9 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.mapper.OutlookMapper; import io.qyi.e5.outlook.service.IOutlookService; @@ -134,21 +137,22 @@ public class OutlookServiceImpl extends ServiceImpl impl try { String s = MailList(outlook.getAccessToken()); JSONObject json = JSON.parseObject(s); -// 报错 + /*错误情况,一般是令牌过期*/ if (json.containsKey("error")) { String code = json.getJSONObject("error").getString("code"); String message = json.getJSONObject("error").getString("message"); + /*如果出现得错误是没有message中收集的,那么就认为是无法刷新的情况。比如 用户取消了授权、删除了key*/ if (!errorCheck(message)) { outlookLogService.addLog(outlook.getGithubId(), "无法刷新令牌!code:3", 0, message); return false; } -// CompactToken validation failed with reason code: 80049228 - logger.info("令牌过期!"); + /*刷新令牌*/ String token = refresh_token(outlook); if (token == null) { return false; } + /*再次获取邮件列表*/ s = MailList(token); json = JSON.parseObject(s); if (json.containsKey("error")) { @@ -156,7 +160,10 @@ public class OutlookServiceImpl extends ServiceImpl impl return false; } } - outlookLogService.addLog(outlook.getGithubId(), "ok", 1, ""); + logger.info("邮件列表请求成功!" + s); + int mail_count = getMailBody(5, s, outlook.getAccessToken()); + logger.info("读取邮件数量: {}" , mail_count); + outlookLogService.addLog(outlook.getGithubId(), "ok", 1, "读取邮件数量:" + mail_count); return true; } catch (Exception e) { e.printStackTrace(); @@ -166,17 +173,35 @@ public class OutlookServiceImpl extends ServiceImpl impl /** * 读取邮件内容 - * @Description: - * @param: count 读取数量,0 则读取当前页所有 - * @return: void - * @Author: 落叶随风 - * @Date: 2020/4/15 - */ - public void getMailBody(int count,String MailBody ,String access_token) throws Exception { - Map head = new HashMap<>(); - head.put("Content-Type", "application/json"); - head.put("Authorization", access_token); - String s = OkHttpRequestUtils.doGet("https://graph.microsoft.com/v1.0/me/messages/", head, null); + * + * @Description: + * @param: count 读取数量,0 则读取当前页所有 + * @return: void + * @Author: 落叶随风 + * @Date: 2020/4/15 + */ + public int getMailBody(int count, String MailBody, String access_token) throws Exception { + Gson gson = new Gson(); + JsonObject jsonObject = gson.fromJson(MailBody, JsonObject.class); + int mial_list_count = jsonObject.get("value").getAsJsonArray().size(); + if (mial_list_count < 1) { + return 0; + } + if (mial_list_count < count) { + count = jsonObject.get("value").getAsJsonArray().size(); + } + JsonArray value = jsonObject.get("value").getAsJsonArray(); + for (int i = 0; i < count - 1; i++) { + JsonObject mail = value.get(i).getAsJsonObject(); + String id = mail.get("id").getAsString(); + + Map head = new HashMap<>(); + head.put("Content-Type", "application/json"); + head.put("Authorization", access_token); + /*不用管邮件内容*/ + OkHttpRequestUtils.doGet("https://graph.microsoft.com/v1.0/me/messages/" + id, head, null); + } + return count; } public String MailList(String access_token) throws Exception { diff --git a/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java b/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java index 65c744e..987858a 100644 --- a/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java +++ b/src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java @@ -2,10 +2,12 @@ package io.qyi.e5.service.rabbitMQ.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.google.gson.Gson; import com.rabbitmq.client.Channel; import io.qyi.e5.outlook.entity.Outlook; import io.qyi.e5.outlook.service.IOutlookService; +import io.qyi.e5.service.task.ITask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; @@ -29,19 +31,26 @@ public class ListenerImpl { @Autowired IOutlookService outlookService; + @Autowired + ITask Task; @RabbitHandler @RabbitListener(queues = "delay_queue2", containerFactory = "rabbitListenerContainerFactory") public void listen(Message message, Channel channel) throws IOException { - String body = new String(message.getBody()); - logger.info("消费者1开始处理消息: {},时间戳:{}" ,body,System.currentTimeMillis()); + logger.info("消费者1开始处理消息: {},时间戳:{}" ,message,System.currentTimeMillis()); + int github_id = Integer.valueOf(new String(message.getBody())); try { - Gson gson = new Gson(); - Outlook outlook = gson.fromJson(body, Outlook.class); - outlookService.getMailList(outlook); + Outlook Outlook = outlookService.getOne(new QueryWrapper().eq("github_id", github_id)); + if (Outlook == null) { + logger.warn("未找到此用户,github_id: {}",github_id); + return; + } + outlookService.getMailList(Outlook); } catch (Exception e) { e.printStackTrace(); } channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); + /*再次进行添加任务*/ + Task.sendTaskOutlookMQ(github_id); } } diff --git a/src/main/java/io/qyi/e5/service/task/ITask.java b/src/main/java/io/qyi/e5/service/task/ITask.java new file mode 100644 index 0000000..78e7570 --- /dev/null +++ b/src/main/java/io/qyi/e5/service/task/ITask.java @@ -0,0 +1,13 @@ +package io.qyi.e5.service.task; + +/** + * @program: e5 + * @description: + * @author: 落叶随风 + * @create: 2020-04-16 16:51 + **/ +public interface ITask { + void sendTaskOutlookMQ(int github_id); + void sendTaskOutlookMQALL(); + +} 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 new file mode 100644 index 0000000..5d4559d --- /dev/null +++ b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java @@ -0,0 +1,98 @@ +package io.qyi.e5.service.task.impl; + +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.amqp.core.MessageProperties; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.util.Iterator; +import java.util.List; +import java.util.Random; +import java.util.UUID; + +/** + * @program: e5 + * @description: + * @author: 落叶随风 + * @create: 2020-04-16 16:53 + **/ +@Service +public class TaskImpl implements ITask { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + IOutlookService outlookService; + @Autowired + RabbitTemplate rabbitTemplate; + + @Override + @Async + public void sendTaskOutlookMQ(int github_id) { + CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); + Outlook Outlook = outlookService.getOne(new QueryWrapper().eq("github_id", github_id)); + if (Outlook == null) { + logger.warn("未找到此用户,github_id: {}",github_id); + return; + } + /*根据用户设置生成随机数*/ + String Expiration = getRandom( Outlook.getCronTimeRandomEnd(),Outlook.getCronTimeRandomEnd()); + send(github_id,Expiration); + + } + + @Override + @Async + public void sendTaskOutlookMQALL() { + List all = outlookService.findAll(); + Iterator iterator = all.iterator(); + while (iterator.hasNext()) { + Outlook next = iterator.next(); + /*根据用户设置生成随机数*/ + String Expiration = getRandom( next.getCronTimeRandomEnd(),next.getCronTimeRandomEnd()); + send(next.getGithubId(), Expiration); + } + } + + /** + * 发送消息到队列 + * @Description: + * @param: * @param msg + * @param Expiration + * @return: void + * @Author: 落叶随风 + * @Date: 2020/4/16 + */ + public void send(Object msg, String Expiration) { + CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); + + rabbitTemplate.convertAndSend("delay", "delay", msg, message -> { + MessageProperties messageProperties = message.getMessageProperties(); + // 设置这条消息的过期时间 + messageProperties.setExpiration(Expiration); + return message; + }, correlationData); + } + + /** + * 生成随机数 + * @Description: + * @param: * @param start + * @param end + * @return: java.lang.String + * @Author: 落叶随风 + * @Date: 2020/4/16 + */ + public String getRandom(int start, int end){ + Random r = new Random(); + String Expiration = String.valueOf((r.nextInt(end - start + 1) + end) * 1000); + return Expiration; + } +} diff --git a/src/test/java/io/qyi/e5/string/dome01.java b/src/test/java/io/qyi/e5/string/dome01.java index b4d9f08..e9e4cdc 100644 --- a/src/test/java/io/qyi/e5/string/dome01.java +++ b/src/test/java/io/qyi/e5/string/dome01.java @@ -7,6 +7,7 @@ import com.google.gson.JsonObject; import org.junit.jupiter.api.Test; import java.util.Iterator; +import java.util.Random; /** * @program: e5 @@ -37,4 +38,10 @@ public class dome01 { JsonObject.get("pid").getAsInt(); } } + + @Test + public void r(){ + Random r=new Random(); + System.out.println(r.nextInt(200-100+1)+100); + } }