消息发送
parent
2601a95150
commit
aa3746705d
@ -1,7 +1,7 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"method": "WS_RECORD_SUBMIT",
|
"method": "WS_RECORD_SUBMIT",
|
||||||
"urls": ["http://127.0.0.1:9312/message/receive1/wsRecordSubmit/do"],
|
"urls": ["http://127.0.0.1:9312/message/receive/wsRecordSubmit/do"],
|
||||||
"errorResult": "<Response><RetInfo><RetCode>${{system:STANDARD_NO_CONNECT}} ${{xpath:/A/B/C/@val}}</RetCode><RetCon>系统错误!</RetCon></RetInfo></Response>",
|
"errorResult": "<Response><RetInfo><RetCode>${{system:STANDARD_NO_CONNECT}} ${{xpath:/A/B/C/@val}}</RetCode><RetCon>系统错误!</RetCon></RetInfo></Response>",
|
||||||
"messageDataType": 1
|
"messageDataType": 1
|
||||||
}
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.docus.server.message.mapper;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.docus.server.message.entity.UnifyMessage;
|
||||||
|
|
||||||
|
public interface UnifyMessageMapper extends BaseMapper<UnifyMessage> {
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.docus.server.message.service;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import com.docus.core.util.Func;
|
||||||
|
import com.docus.server.message.config.MessageForwardConfig;
|
||||||
|
import com.docus.server.message.dto.Message;
|
||||||
|
import com.docus.server.message.dto.MessageResponse;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class MessageCarrier {
|
||||||
|
public List<MessageResponse> send(Message message, MessageForwardConfig.MessageForward messageForward) {
|
||||||
|
List<MessageResponse> responseLis = new ArrayList<>();
|
||||||
|
for (String url : messageForward.getUrls()) {
|
||||||
|
String resp = HttpUtil.post(url, Func.toJson(message));
|
||||||
|
responseLis.add(Func.readJson(resp, MessageResponse.class));
|
||||||
|
}
|
||||||
|
return responseLis;
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
package com.docus.server.message.service.impl;
|
|
||||||
|
|
||||||
import com.docus.server.message.consts.InteractiveMethod;
|
|
||||||
import com.docus.server.message.service.UnifyMessageService;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author wyb
|
|
||||||
*/
|
|
||||||
@Service(InteractiveMethod.HTTP)
|
|
||||||
public class HttpUnifyMessageServiceImpl implements UnifyMessageService {
|
|
||||||
@Override
|
|
||||||
public String pushInformation(String method,String message) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,103 @@
|
|||||||
|
package com.docus.server.message.service.impl;
|
||||||
|
|
||||||
|
import com.docus.core.util.Func;
|
||||||
|
import com.docus.infrastructure.redis.service.IdService;
|
||||||
|
import com.docus.infrastructure.web.api.ResultCode;
|
||||||
|
import com.docus.server.message.config.MessageForwardConfig;
|
||||||
|
import com.docus.server.message.dto.Message;
|
||||||
|
import com.docus.server.message.dto.MessageResponse;
|
||||||
|
import com.docus.server.message.entity.UnifyMessage;
|
||||||
|
import com.docus.server.message.enums.DataFormatEnum;
|
||||||
|
import com.docus.server.message.mapper.UnifyMessageMapper;
|
||||||
|
import com.docus.server.message.result.MessageInfo;
|
||||||
|
import com.docus.server.message.result.ResultUtil;
|
||||||
|
import com.docus.server.message.service.MessageCarrier;
|
||||||
|
import com.docus.server.message.service.UnifyMessageService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author WYBDEV
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class UnifyMessageServiceImpl implements UnifyMessageService {
|
||||||
|
private UnifyMessageMapper unifyMessageMapper;
|
||||||
|
private IdService idService;
|
||||||
|
private MessageCarrier messageCarrier;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setUnifyMessageMapper(UnifyMessageMapper unifyMessageMapper) {
|
||||||
|
this.unifyMessageMapper = unifyMessageMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setIdService(IdService idService) {
|
||||||
|
this.idService = idService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setMessageCarrier(MessageCarrier messageCarrier) {
|
||||||
|
this.messageCarrier = messageCarrier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String pushInformation(String method, String message) {
|
||||||
|
MessageForwardConfig.MessageForward messageForward = MessageForwardConfig.wsMessageForward(method);
|
||||||
|
if (messageForward == null || Func.isEmpty(messageForward.getUrls())) {
|
||||||
|
return "方法匹配错误!";
|
||||||
|
}
|
||||||
|
DataFormatEnum dataFormatEnum = DataFormatEnum.fromValue(messageForward.getMessageDataType());
|
||||||
|
if (dataFormatEnum == null) {
|
||||||
|
return "格式匹配错误!";
|
||||||
|
}
|
||||||
|
UnifyMessage unifyMessage = saveMessage(method, message);
|
||||||
|
try {
|
||||||
|
List<MessageResponse> responseLis = messageCarrier.send(new Message(method, message), messageForward);
|
||||||
|
return verifyMessageResponseAndGetReturnMessage(responseLis);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error(ex.getMessage(), ex);
|
||||||
|
messageFailed(unifyMessage);
|
||||||
|
MessageInfo messageInfo = new MessageInfo(dataFormatEnum, message);
|
||||||
|
return ResultUtil.dynamicParameterReplace(messageInfo, messageForward.getErrorResult());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void messageFailed(UnifyMessage unifyMessage) {
|
||||||
|
UnifyMessage updateMessage = new UnifyMessage();
|
||||||
|
updateMessage.setId(unifyMessage.getId());
|
||||||
|
updateMessage.setState(0);
|
||||||
|
unifyMessageMapper.updateById(updateMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String verifyMessageResponseAndGetReturnMessage(List<MessageResponse> responseLis) {
|
||||||
|
responseLis = responseLis.stream().filter(item -> Func.isNotEmpty(item.getRespCode())).collect(Collectors.toList());
|
||||||
|
if (Func.isEmpty(responseLis)) {
|
||||||
|
throw new RuntimeException("未收到成功和失败的返回结果!");
|
||||||
|
}
|
||||||
|
for (MessageResponse response : responseLis) {
|
||||||
|
if (ResultCode.FAILED.getCode().equals(response.getRespCode())) {
|
||||||
|
return response.getRespMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return responseLis.get(0).getRespMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
private UnifyMessage saveMessage(String method, String message) {
|
||||||
|
UnifyMessage unifyMessage = new UnifyMessage();
|
||||||
|
unifyMessage.setId(idService.getDateSeq());
|
||||||
|
unifyMessage.setMessage(message);
|
||||||
|
unifyMessage.setMethod(method);
|
||||||
|
unifyMessage.setState(1);
|
||||||
|
unifyMessage.setCreateTime(new Date());
|
||||||
|
unifyMessageMapper.insert(unifyMessage);
|
||||||
|
return unifyMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,61 +0,0 @@
|
|||||||
package com.docus.server.message.service.impl;
|
|
||||||
|
|
||||||
import cn.hutool.http.HttpUtil;
|
|
||||||
import com.docus.core.util.Func;
|
|
||||||
import com.docus.infrastructure.web.api.ResultCode;
|
|
||||||
import com.docus.server.message.config.WsMessageForwardConfig;
|
|
||||||
import com.docus.server.message.consts.InteractiveMethod;
|
|
||||||
import com.docus.server.message.dto.Message;
|
|
||||||
import com.docus.server.message.dto.MessageResponse;
|
|
||||||
import com.docus.server.message.enums.DataFormatEnum;
|
|
||||||
import com.docus.server.message.result.MessageInfo;
|
|
||||||
import com.docus.server.message.result.ResultUtil;
|
|
||||||
import com.docus.server.message.service.UnifyMessageService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author WYBDEV
|
|
||||||
*/
|
|
||||||
@Service(InteractiveMethod.WS)
|
|
||||||
@Slf4j
|
|
||||||
public class WebServiceUnifyMessageServiceImpl implements UnifyMessageService {
|
|
||||||
@Override
|
|
||||||
public String pushInformation(String method, String message) {
|
|
||||||
Message messageObj = new Message(method, message);
|
|
||||||
WsMessageForwardConfig.WsMessageForward wsMessageForward = WsMessageForwardConfig.wsMessageForward(method);
|
|
||||||
if (wsMessageForward == null || Func.isEmpty(wsMessageForward.getUrls())) {
|
|
||||||
return "方法匹配错误!";
|
|
||||||
}
|
|
||||||
DataFormatEnum dataFormatEnum = DataFormatEnum.fromValue(wsMessageForward.getMessageDataType());
|
|
||||||
if (dataFormatEnum == null) {
|
|
||||||
return "格式匹配错误!";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
List<MessageResponse> responseLis = new ArrayList<>();
|
|
||||||
for (String url : wsMessageForward.getUrls()) {
|
|
||||||
String resp = HttpUtil.post(url, Func.toJson(messageObj));
|
|
||||||
responseLis.add(Func.readJson(resp, MessageResponse.class));
|
|
||||||
}
|
|
||||||
for (MessageResponse response : responseLis) {
|
|
||||||
if (ResultCode.FAILED.getCode().equals(response.getRespCode())) {
|
|
||||||
return response.getRespMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
responseLis = responseLis.stream().filter(item -> Func.isNotEmpty(item.getRespCode())).collect(Collectors.toList());
|
|
||||||
if (Func.isEmpty(responseLis)) {
|
|
||||||
throw new RuntimeException("未收到成功和失败的返回结果!");
|
|
||||||
}
|
|
||||||
return responseLis.get(0).getRespMessage();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
// save(method,message);
|
|
||||||
log.error(ex.getMessage(), ex);
|
|
||||||
MessageInfo messageInfo = new MessageInfo(dataFormatEnum, message);
|
|
||||||
return ResultUtil.dynamicParameterReplace(messageInfo, wsMessageForward.getErrorResult());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue