From 32bf0e18411ebcd3ef2a7e6112ed4dffcaf7631b Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Thu, 13 Jul 2023 09:05:13 +0800 Subject: [PATCH] =?UTF-8?q?#=20=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=99=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hzims/message/config/ThreadPoolConfig.java | 6 ++- .../message/controller/MessageController.java | 20 +++++++-- .../service/impl/PushMessageServiceImpl.java | 47 ++++++---------------- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/hzims-service/message/src/main/java/com/hnac/hzims/message/config/ThreadPoolConfig.java b/hzims-service/message/src/main/java/com/hnac/hzims/message/config/ThreadPoolConfig.java index d8feb4f..b3e8b1a 100644 --- a/hzims-service/message/src/main/java/com/hnac/hzims/message/config/ThreadPoolConfig.java +++ b/hzims-service/message/src/main/java/com/hnac/hzims/message/config/ThreadPoolConfig.java @@ -18,9 +18,13 @@ public class ThreadPoolConfig { @Bean public ExecutorService logExecutorService() { - return new ThreadPoolExecutor(1, 1, + return new ThreadPoolExecutor(2, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); } + @Bean + public ExecutorService appMessagePushExecutor() { + return new ThreadPoolExecutor(2, 5, 5L, TimeUnit.SECONDS,new LinkedBlockingQueue()); + } } diff --git a/hzims-service/message/src/main/java/com/hnac/hzims/message/controller/MessageController.java b/hzims-service/message/src/main/java/com/hnac/hzims/message/controller/MessageController.java index 4b84621..89a171d 100644 --- a/hzims-service/message/src/main/java/com/hnac/hzims/message/controller/MessageController.java +++ b/hzims-service/message/src/main/java/com/hnac/hzims/message/controller/MessageController.java @@ -7,6 +7,7 @@ import com.hnac.hzims.message.dto.SmsPushDto; import com.hnac.hzims.message.dto.WsPushDto; import com.hnac.hzims.message.fegin.IPushMsgClient; import com.hnac.hzims.message.service.IMessageService; +import com.hnac.hzims.message.service.IPushMessageService; import com.hnac.hzims.message.service.impl.MailMessageServiceImpl; import com.hnac.hzims.message.service.impl.PushMessageServiceImpl; import com.hnac.hzims.message.service.impl.SmsMessageServiceImpl; @@ -14,16 +15,16 @@ import com.hnac.hzims.message.service.impl.WebsocketServiceImpl; import com.hnac.hzims.message.utils.HtmlModule; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.boot.ctrl.BladeController; import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.SpringUtil; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import java.util.List; import java.util.Map; /** @@ -79,4 +80,15 @@ public class MessageController extends BladeController { IMessageService messageService = SpringUtil.getBean(WebsocketServiceImpl.class); return messageService.send(wsPushDto); } + + @GetMapping("/sendByUsers") + @ApiOperation("发送APP消息(多人)") + @ApiOperationSupport(order=5) + public R sendByUsers(@ApiParam("消息主题") String subject, + @ApiParam("消息内容") String content, + @ApiParam("用户ID列表") String userIds, + @ApiParam("租户ID") String tenantId) throws Exception { + PushMessageServiceImpl messageService = SpringUtil.getBean(PushMessageServiceImpl.class); + return R.status(messageService.sendByUsers(subject,content, Func.toStrList(",",userIds),tenantId)); + } } diff --git a/hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java b/hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java index 51a512a..9c1d6be 100644 --- a/hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java +++ b/hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java @@ -30,6 +30,7 @@ import org.springframework.util.Assert; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutorService; import java.util.concurrent.FutureTask; import java.util.concurrent.atomic.AtomicReference; @@ -42,6 +43,7 @@ public class PushMessageServiceImpl implements IMessageService { private final IPushClient pushClient; private final BladeLogger logger; + private final ExecutorService appMessagePushExecutor; @Override @SaveLog(type = MessageConstants.APP_NAME) @@ -87,37 +89,14 @@ public class PushMessageServiceImpl implements IMessageService { } @Override - public boolean send(MessagePushRecordEntity request) { - PushInfoVO pushInfoVO = new PushInfoVO(); - pushInfoVO.setTenantId(Func.isNotEmpty(AuthUtil.getTenantId()) ? AuthUtil.getTenantId() : request.getTenantId()); - ArrayList platformTypes = Lists.newArrayList(PlatformType.Android, PlatformType.IOS); - platformTypes.forEach(platformType -> { - PushPlatform platform = PushPlatform.newBuilder().addPlatformType(platformType).build(); - R response; - // 安卓推送 - if(PlatformType.Android.equals(platformType)) { - PushAudience pushAudience = PushAudience.newBuilder().addPushAudienceType(PushAudienceType.TAG, Lists.newArrayList(request.getPusher())).build(); - PushInfo pushInfo = new PushInfo("ops-push-android", request.getSubject(), request.getContent(), - "", null, platform, pushAudience); - pushInfoVO.setPushInfo(pushInfo); - response = pushClient.tenantPush(pushInfoVO); - } - // IOS推送 - else if(PlatformType.IOS.equals(platformType)) { - PushAudience pushAudience = PushAudience.newBuilder().addPushAudienceType(PushAudienceType.TAG, Lists.newArrayList(request.getPusher())).build(); - PushInfo pushInfo = new PushInfo("ops-push-ios", request.getSubject(), request.getContent(), - "", null, platform, pushAudience); - pushInfoVO.setPushInfo(pushInfo); - response = pushClient.tenantPush(pushInfoVO); - } else { - response = null; - } - Assert.isTrue(ObjectUtil.isNotEmpty(response) && response.isSuccess(),() -> { - logger.error("hzims-message:PushMessageServiceImpl:send", "请求参数为:"+JSON.toJSONString(pushInfoVO)+"错误信息为:"+response.getMsg()); - throw new ServiceException(response.getMsg()); - }); - }); - return true; + public boolean send(MessagePushRecordEntity request) { + String tenantId = Func.isNotEmpty(AuthUtil.getTenantId()) ? AuthUtil.getTenantId() : request.getTenantId(); + try { + return this.sendByUsers(request.getSubject(), request.getContent(), Func.toStrList(",",request.getPusher()),tenantId); + } + catch (Exception e) { + throw new ServiceException(e.getMessage()); + } } /** @@ -125,7 +104,7 @@ public class PushMessageServiceImpl implements IMessageService { * @param subject 消息主题 * @param content 消息内容 * @param userIds 用户ID列表 - * @param tenantId 推送成功标识 + * @param tenantId 租户ID * @return */ public boolean sendByUsers(String subject, String content, List userIds,String tenantId) throws Exception { @@ -140,7 +119,7 @@ public class PushMessageServiceImpl implements IMessageService { R pushResult = pushClient.tenantPush(pushInfoVO); return pushResult.isSuccess(); }); - new Thread(androidPush,"安卓推送").start(); + appMessagePushExecutor.submit(new Thread(androidPush,"安卓推送")); // IOS推送 FutureTask iosPush = new FutureTask<>(() -> { @@ -153,7 +132,7 @@ public class PushMessageServiceImpl implements IMessageService { R pushResult = pushClient.tenantPush(pushInfoVO); return pushResult.isSuccess(); }); - new Thread(iosPush,"ios推送").start(); + appMessagePushExecutor.submit(new Thread(iosPush,"ios推送")); return androidPush.get() && iosPush.get(); }