Browse Source

fix:解决因用户手机号为空定时任务轮询执行导致resource服务熔断问题

zhongwei
haungxing 6 months ago
parent
commit
943c1d3b4d
  1. 5
      hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java
  2. 4
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/IMessageService.java
  3. 8
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MailMessageServiceImpl.java
  4. 29
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/SmsMessageServiceImpl.java

5
hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java

@ -179,9 +179,6 @@ public class MessageClient extends BladeController implements IMessageClient{
try { try {
for (Long pushId : Func.toLongList(",", smsImmediatelyPushDTO.getPusher())) { for (Long pushId : Func.toLongList(",", smsImmediatelyPushDTO.getPusher())) {
User user = UserCache.getUser(pushId); User user = UserCache.getUser(pushId);
if (ObjectUtil.isEmpty(user)||ObjectUtil.isEmpty(user.getPhone())){
continue;
}
MessagePushRecordEntity record = BeanUtil.copy(smsImmediatelyPushDTO, MessagePushRecordEntity.class); MessagePushRecordEntity record = BeanUtil.copy(smsImmediatelyPushDTO, MessagePushRecordEntity.class);
record.setMessageId(IdWorker.getId()); record.setMessageId(IdWorker.getId());
record.setPusherName(user.getRealName()); record.setPusherName(user.getRealName());
@ -199,7 +196,7 @@ public class MessageClient extends BladeController implements IMessageClient{
record.setSmsParam(JSON.toJSONString(params)); record.setSmsParam(JSON.toJSONString(params));
Boolean flag = recordService.sendSmsImmediatelyMsg(record); Boolean flag = recordService.sendSmsImmediatelyMsg(record);
if (!flag) { if (!flag) {
log.error("短信推送失败,参数:" + record.toString()); log.error("短信推送失败,参数:{}", JSON.toJSONString(record));
} }
} }
return R.data(true); return R.data(true);

4
hzims-service/message/src/main/java/com/hnac/hzims/message/service/IMessageService.java

@ -35,9 +35,9 @@ public interface IMessageService {
if(ObjectUtil.isNotEmpty(user)) { if(ObjectUtil.isNotEmpty(user)) {
switch(type) { switch(type) {
case MAIL_PUSH: case MAIL_PUSH:
return Func.isNotEmpty(user.getEmail()) ? user.getEmail() : Long.toString(pusher); return user.getEmail();
case SMS_PUSH: case SMS_PUSH:
return Func.isNotEmpty(user.getPhone()) ? user.getPhone() : Long.toString(pusher); return user.getPhone();
default: default:
break; break;
} }

8
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MailMessageServiceImpl.java

@ -13,12 +13,14 @@ import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil; import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil; import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.mail.MessagingException; import javax.mail.MessagingException;
@ -79,6 +81,10 @@ public class MailMessageServiceImpl implements IMessageService {
@Override @Override
public boolean send(MessagePushRecordEntity request) { public boolean send(MessagePushRecordEntity request) {
String account = getAccountByPusher(Long.parseLong(request.getPusher()), request.getType());
Assert.isTrue(Func.isNotEmpty(account) && StringUtil.isNotBlank(account) , () -> {
throw new ServiceException("未查询到相关用户,邮件推送失败!");
});
// 创建复杂有限发送对象 // 创建复杂有限发送对象
MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper; MimeMessageHelper messageHelper;
@ -87,7 +93,7 @@ public class MailMessageServiceImpl implements IMessageService {
// 设置发件人邮箱(若配置默认邮箱则不用再设置) // 设置发件人邮箱(若配置默认邮箱则不用再设置)
messageHelper.setFrom(fromAccount); messageHelper.setFrom(fromAccount);
// 设置收件人邮箱 // 设置收件人邮箱
messageHelper.setTo(getAccountByPusher(Long.parseLong(request.getPusher()),request.getType())); messageHelper.setTo(account);
// 设置抄报人邮箱(可以不填写) // 设置抄报人邮箱(可以不填写)
if(StringUtil.isNotBlank(request.getCcAccount())) { if(StringUtil.isNotBlank(request.getCcAccount())) {
messageHelper.setCc(request.getCcAccount()); messageHelper.setCc(request.getCcAccount());

29
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/SmsMessageServiceImpl.java

@ -15,6 +15,7 @@ import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.sms.model.SmsResponse; import org.springblade.core.sms.model.SmsResponse;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.resource.feign.ISmsClient; import org.springblade.resource.feign.ISmsClient;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -48,11 +49,15 @@ public class SmsMessageServiceImpl implements IMessageService {
@Override @Override
public boolean send(MessagePushRecordEntity request) { public boolean send(MessagePushRecordEntity request) {
String account = this.getAccountByPusher(Long.parseLong(request.getPusher()), request.getType());
Assert.isTrue(Func.isNotEmpty(account) && StringUtil.isNotBlank(account) ,() -> {
throw new ServiceException("未查询到相关用户,短信推送失败!");
});
R<SmsResponse> response = smsClient.sendMessage( R<SmsResponse> response = smsClient.sendMessage(
Func.isNotEmpty(AuthUtil.getTenantId()) ? AuthUtil.getTenantId() : request.getTenantId(), Func.isNotEmpty(AuthUtil.getTenantId()) ? AuthUtil.getTenantId() : request.getTenantId(),
request.getResourceCode(), request.getResourceCode(),
request.getSmsParam(), request.getSmsParam(),
this.getAccountByPusher(Long.parseLong(request.getPusher()),request.getType()) account
); );
if(ObjectUtils.isEmpty(response.getData())) { if(ObjectUtils.isEmpty(response.getData())) {
Assert.isTrue(response.isSuccess(), () -> { Assert.isTrue(response.isSuccess(), () -> {
@ -68,11 +73,15 @@ public class SmsMessageServiceImpl implements IMessageService {
} }
public R sendSmsByRecord(MessagePushRecordEntity request) { public R sendSmsByRecord(MessagePushRecordEntity request) {
return smsClient.sendMessage( String account = this.getAccountByPusher(Long.parseLong(request.getPusher()), request.getType());
Func.isNotEmpty(AuthUtil.getTenantId()) ? AuthUtil.getTenantId() : request.getTenantId(), if(Func.isNotEmpty(account) && StringUtil.isNotBlank(account)) {
request.getResourceCode(), return smsClient.sendMessage(
request.getSmsParam(), Func.isNotEmpty(AuthUtil.getTenantId()) ? AuthUtil.getTenantId() : request.getTenantId(),
this.getAccountByPusher(Long.parseLong(request.getPusher()),request.getType()) request.getResourceCode(),
); request.getSmsParam(),
account
);
}
return R.fail("未查询到相关用户,短信推送失败!");
} }
} }

Loading…
Cancel
Save