|
|
|
@ -1,18 +1,20 @@
|
|
|
|
|
package me.zhengjie.task;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import me.zhengjie.modules.taskrecord.domain.TaskRecord;
|
|
|
|
|
import me.zhengjie.modules.taskrecord.rest.dto.PushDBJsonContent;
|
|
|
|
|
import me.zhengjie.modules.taskrecord.service.TaskRecordService;
|
|
|
|
|
import me.zhengjie.modules.taskrecord.service.dto.TaskRecordDto;
|
|
|
|
|
import me.zhengjie.utils.HttpUtil;
|
|
|
|
|
import me.zhengjie.utils.StringUtils;
|
|
|
|
|
import org.apache.poi.ss.formula.functions.T;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
@ -29,6 +31,7 @@ import java.util.stream.Collectors;
|
|
|
|
|
* 发送记录异步执行任务类
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@Scope("prototype")
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class SendRecordTask {
|
|
|
|
|
|
|
|
|
@ -48,11 +51,22 @@ public class SendRecordTask {
|
|
|
|
|
private static final int FINISH_SEND_TAG = 1;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下游发送地址
|
|
|
|
|
* 用于随机选的环境代替字符, 如果添加环境就进行自动添加
|
|
|
|
|
*/
|
|
|
|
|
public static final String BASE_URL_CHAR_NUMBER = "12345";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下游发送url
|
|
|
|
|
*/
|
|
|
|
|
@Value(value = "${req.db.url}")
|
|
|
|
|
private String url;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下游推送ip:端口
|
|
|
|
|
*/
|
|
|
|
|
@Value(value = "${req.db.host}")
|
|
|
|
|
private String host;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TaskRecordService taskRecordService;
|
|
|
|
|
|
|
|
|
@ -95,7 +109,7 @@ public class SendRecordTask {
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 分批进行发送
|
|
|
|
|
batchSend(collect);
|
|
|
|
|
batchSend(collect, taskRecordDto);
|
|
|
|
|
// 对发送后的记录进行更新
|
|
|
|
|
TaskRecord taskRecord = new TaskRecord();
|
|
|
|
|
BeanUtils.copyProperties(taskRecordDto, taskRecord);
|
|
|
|
@ -109,21 +123,40 @@ public class SendRecordTask {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void batchSend(List<String> collect) {
|
|
|
|
|
private void batchSend(List<String> collect, TaskRecordDto taskRecordDto) {
|
|
|
|
|
List<List<String>> partition = Lists.partition(collect, SEND_LIMIT);
|
|
|
|
|
partition.forEach(
|
|
|
|
|
list->{
|
|
|
|
|
// TODO: 2020/9/10 0010 调用推送地址进行推送
|
|
|
|
|
String jsonStr = JSON.toJSONString(list);
|
|
|
|
|
System.out.println(jsonStr);
|
|
|
|
|
PushDBJsonContent pushDBJsonContent = new PushDBJsonContent();
|
|
|
|
|
pushDBJsonContent.setActId(taskRecordDto.getTaskPushId());
|
|
|
|
|
pushDBJsonContent.setActName(taskRecordDto.getTaskName());
|
|
|
|
|
// 加入每一个号码对应接口字段
|
|
|
|
|
List<PushDBJsonContent.Client> clientList = new ArrayList<>();
|
|
|
|
|
list.forEach(
|
|
|
|
|
each->{
|
|
|
|
|
PushDBJsonContent.Client client = new PushDBJsonContent.Client();
|
|
|
|
|
client.setCellphone(each);
|
|
|
|
|
clientList.add(client);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
pushDBJsonContent.setClientList(clientList);
|
|
|
|
|
// 进行Json解析
|
|
|
|
|
String jsonStr = JSON.toJSONString(pushDBJsonContent);
|
|
|
|
|
if (StringUtils.isNotBlank(jsonStr)){
|
|
|
|
|
log.info("============ [ Pre send Json is : {} ] ============", jsonStr);
|
|
|
|
|
int count = 1;
|
|
|
|
|
// 失败重发请求3次
|
|
|
|
|
while (count <= 3){
|
|
|
|
|
// 对发送请求地址进行准备
|
|
|
|
|
String description = taskRecordDto.getDescription();
|
|
|
|
|
String address ="";
|
|
|
|
|
if (StringUtils.isNotBlank(description)){
|
|
|
|
|
address = preSendReqAddress(description);
|
|
|
|
|
}
|
|
|
|
|
// 调用HTTP请求发送数据
|
|
|
|
|
HttpResponse httpResponse = HttpUtil.sendPostReq(url, jsonStr);
|
|
|
|
|
// todo 这个判别发送正常的码要进行判别 ok
|
|
|
|
|
if (httpResponse.isOk() && httpResponse.body().contains("ok")){
|
|
|
|
|
HttpResponse httpResponse = HttpUtil.sendPostReq(address, jsonStr);
|
|
|
|
|
if (httpResponse.isOk() && httpResponse.body().contains("success")){
|
|
|
|
|
log.info("========== [DB request success, response is {} ] ==========", httpResponse.body());
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
@ -139,4 +172,17 @@ public class SendRecordTask {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String preSendReqAddress(String desc) {
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
// 处理环境选择问题, 只接受单个 a, b, c 中的任意一个输入,如果不输入就进行随机返回
|
|
|
|
|
desc = (StringUtils.isNotBlank(desc) && StringUtils.countMatches(BASE_URL_CHAR_NUMBER, desc.trim()) == 1)?
|
|
|
|
|
desc.trim(): String.valueOf(RandomUtil.randomChar(BASE_URL_CHAR_NUMBER));
|
|
|
|
|
// 拼接成需要的URL进行返回
|
|
|
|
|
builder.append(host)
|
|
|
|
|
.append(desc)
|
|
|
|
|
.append(url);
|
|
|
|
|
// 返回拼接结果
|
|
|
|
|
return builder.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|