1、增加读取邮件

2、发送消息队列的消息换为github_id
3、根据用户设定的随机值设置队列时间
4、修改队列接收参数outlook对象为github_id
This commit is contained in:
LuoYe_MyWork 2020-04-16 17:33:34 +08:00
parent 6aa704f5c0
commit 9bcc0d83f7
6 changed files with 177 additions and 29 deletions

View File

@ -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<Outlook>().eq("github_id", 19658189));
rabbitTemplate.convertAndSend("delay", "delay", Outlook, message -> {
MessageProperties messageProperties = message.getMessageProperties();
// 设置这条消息的过期时间
messageProperties.setExpiration("5000");
return message;
}, correlationData);
Task.sendTaskOutlookMQ(19658189);
}
}

View File

@ -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<OutlookMapper, Outlook> 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<OutlookMapper, Outlook> 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<OutlookMapper, Outlook> 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<String, Object> 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<String, Object> 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 {

View File

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

View File

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

View File

@ -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<Outlook>().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<Outlook> all = outlookService.findAll();
Iterator<Outlook> 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;
}
}

View File

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