Mr.qian 2 veckor sedan
förälder
incheckning
1a505fcdf0

+ 9 - 9
mall-service/src/main/java/com/txz/mall/MallApplication.java

@@ -1,6 +1,5 @@
 package com.txz.mall;
 
-import com.spring4all.swagger.EnableSwagger2Doc;
 import com.txz.mall.configurer.shutdown.GracefulShutdownUndertowWrapper;
 import io.undertow.UndertowOptions;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -11,24 +10,26 @@ import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFa
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 import org.springframework.context.annotation.Bean;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.web.client.RestTemplate;
 
 @EnableDiscoveryClient
 @SpringBootApplication
+@EnableAsync
 // @EnableSwagger2Doc
 //@RibbonClients(value = {
 //        @RibbonClient(name = "operating",configuration = RibbonWeightedResponseTimeRuleConfig.class)
 //})
 //@EnableFeignClients(basePackages = {"com.txz.operating.service"})
 public class MallApplication {
-
+    
     public static void main(String[] args) throws Exception {
         SpringApplication.run(MallApplication.class, args);
     }
-
+    
     @Autowired
     private GracefulShutdownUndertowWrapper gracefulShutdownUndertowWrapper;
-
+    
     @Bean
     public UndertowServletWebServerFactory servletWebServerFactory() {
         UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
@@ -36,16 +37,15 @@ public class MallApplication {
         factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_STATISTICS, true));
         return factory;
     }
-
+    
     /**
      * 负载均衡
      * 默认的负载均衡机制采用的类似轮询的这种均衡机制
      */
     @Bean
     @LoadBalanced
-    public RestTemplate restTemplate(RestTemplateBuilder builder){
-        //spring官网建议RestTemplateBuilder构造器来进行构造,这里体现了构造者设计模式
+    public RestTemplate restTemplate(RestTemplateBuilder builder) {
+        // spring官网建议RestTemplateBuilder构造器来进行构造,这里体现了构造者设计模式
         return builder.build();
     }
-}
-
+}

+ 30 - 0
mall-service/src/main/java/com/txz/mall/configurer/AsyncConfig.java

@@ -0,0 +1,30 @@
+package com.txz.mall.configurer;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+@Configuration
+@EnableAsync
+public class AsyncConfig {
+
+    /**
+     * 线程池
+     */
+    @Bean("taskExecutor")
+    public Executor taskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(10);
+        executor.setMaxPoolSize(30);
+        executor.setQueueCapacity(200);
+        executor.setThreadNamePrefix("async-task-");
+        executor.setKeepAliveSeconds(30);
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+}

+ 1 - 1
mall-service/src/main/java/com/txz/mall/configurer/jpush/JPushConf.java

@@ -23,7 +23,7 @@ public class JPushConf {
     @Bean
     public PushApi pushApi() {
         return new PushApi.Builder()
-                .setHost(DataCenterHost.HK.getUrl())
+                .setHost(DataCenterHost.SG.getUrl())
                 .setAppKey(appKey)
                 .setMasterSecret(masterSecret)
                 .build();

+ 133 - 125
mall-service/src/main/java/com/txz/mall/service/impl/NoticeServiceImpl.java

@@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollectionUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.txz.mall.configurer.AsyncConfig;
 import com.txz.mall.dao.NoticeMapper;
 import com.txz.mall.enums.NoticeEnum;
 import com.txz.mall.model.Notice;
@@ -12,6 +13,7 @@ import com.txz.mall.service.NoticeService;
 import com.txz.mall.service.UserJPushService;
 import com.txz.mall.util.NoticeI18nUtil;
 import dto.NoticeDTO;
+import io.github.engagelab.api.PushApi;
 import io.github.engagelab.bean.push.PushParam;
 import io.github.engagelab.bean.push.PushResult;
 import io.github.engagelab.bean.push.message.notification.NotificationMessage;
@@ -36,10 +38,12 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
     
-    private final io.github.engagelab.api.PushApi pushApi;
+    private final PushApi pushApi;
     
     private final UserJPushService userJPushService;
     
+    private final AsyncConfig asyncConfig;
+    
     @Override
     public void addNotice(NoticeDTO notice, Long... uids) {
         if (CollectionUtil.isEmpty(Arrays.asList(uids))) {
@@ -56,131 +60,135 @@ public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> impleme
         });
         this.saveBatch(saveBatch);
         
-        
-        // 极光推送
-        List<UserJPush> pushList = userJPushService.list(Wrappers.<UserJPush>lambdaQuery()
-                .in(UserJPush::getUserId, Arrays.asList(uids))
-        );
-        
-        PushParam param = new PushParam();
-        PushParam.Body body = new PushParam.Body();
-        
-        Map<String, List<UserJPush>> languages = pushList.stream().collect(Collectors.groupingBy(UserJPush::getUserLanguage));
-        
-        // 单独处理孟加拉区域
-        List<UserJPush> bns = languages.get("bn");
-        if (CollectionUtil.isNotEmpty(bns)) {
-            // android 通知内容
-            NotificationMessage.Android android = new NotificationMessage.Android();
-            // ios 通知内容
-            NotificationMessage.IOS ios = new NotificationMessage.IOS();
-            switch (notice.getNoticeType()) {
-                default:
-                    android.setTitle(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()));
-                    android.setAlert(String.format(NoticeI18nUtil.get("bn", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
-                    ios.setAlert(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("bn", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
-                    break;
-                case OTHER:
-                    android.setTitle(notice.getNoticeTitle());
-                    android.setAlert(notice.getNoticeMessage());
-                    ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
-                    break;
-            }
-            NotificationMessage notificationMessage = new NotificationMessage();
-            notificationMessage.setAlert(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()));
-            notificationMessage.setAndroid(android);
-            notificationMessage.setIos(ios);
-            body.setNotification(notificationMessage);
-            // 目标人群
-            To to = new To();
-            to.setRegistrationIdList(bns.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
-            // 指定目标
-            param.setTo(to);
-            
-            // 指定平台
-            body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
-            
-            // 发送
-            param.setBody(body);
-            PushResult result = pushApi.push(param);
-            log.info("孟加拉极光推送结果:{}", result);
-        }
-        
-        List<UserJPush> ens = languages.get("en");
-        if (CollectionUtil.isNotEmpty(ens)) {
-            // android 通知内容
-            NotificationMessage.Android android = new NotificationMessage.Android();
-            // ios 通知内容
-            NotificationMessage.IOS ios = new NotificationMessage.IOS();
-            switch (notice.getNoticeType()) {
-                default:
-                    android.setTitle(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()));
-                    android.setAlert(String.format(NoticeI18nUtil.get("en", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
-                    ios.setAlert(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("en", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
-                    break;
-                case OTHER:
-                    android.setTitle(notice.getNoticeTitle());
-                    android.setAlert(notice.getNoticeMessage());
-                    ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
-                    break;
+        asyncConfig.taskExecutor().execute(() -> {
+            try {
+                // 极光推送
+                List<UserJPush> pushList = userJPushService.list(Wrappers.<UserJPush>lambdaQuery()
+                        .in(UserJPush::getUserId, Arrays.asList(uids))
+                );
+                
+                PushParam param = new PushParam();
+                PushParam.Body body = new PushParam.Body();
+                
+                Map<String, List<UserJPush>> languages = pushList.stream().collect(Collectors.groupingBy(UserJPush::getUserLanguage));
+                
+                // 单独处理孟加拉区域
+                List<UserJPush> bns = languages.get("bn");
+                if (CollectionUtil.isNotEmpty(bns)) {
+                    // android 通知内容
+                    NotificationMessage.Android android = new NotificationMessage.Android();
+                    // ios 通知内容
+                    NotificationMessage.IOS ios = new NotificationMessage.IOS();
+                    switch (notice.getNoticeType()) {
+                        default:
+                            android.setTitle(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()));
+                            android.setAlert(String.format(NoticeI18nUtil.get("bn", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
+                            ios.setAlert(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("bn", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
+                            break;
+                        case OTHER:
+                            android.setTitle(notice.getNoticeTitle());
+                            android.setAlert(notice.getNoticeMessage());
+                            ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
+                            break;
+                    }
+                    NotificationMessage notificationMessage = new NotificationMessage();
+                    notificationMessage.setAlert(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()));
+                    notificationMessage.setAndroid(android);
+                    notificationMessage.setIos(ios);
+                    body.setNotification(notificationMessage);
+                    // 目标人群
+                    To to = new To();
+                    to.setRegistrationIdList(bns.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
+                    
+                    // 指定平台
+                    body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
+                    
+                    // 发送
+                    param.setBody(body);
+                    PushResult result = pushApi.push(param);
+                    log.info("孟加拉极光推送结果:{}", result);
+                }
+                
+                List<UserJPush> ens = languages.get("en");
+                if (CollectionUtil.isNotEmpty(ens)) {
+                    // android 通知内容
+                    NotificationMessage.Android android = new NotificationMessage.Android();
+                    // ios 通知内容
+                    NotificationMessage.IOS ios = new NotificationMessage.IOS();
+                    switch (notice.getNoticeType()) {
+                        default:
+                            android.setTitle(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()));
+                            android.setAlert(String.format(NoticeI18nUtil.get("en", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
+                            ios.setAlert(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("en", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
+                            break;
+                        case OTHER:
+                            android.setTitle(notice.getNoticeTitle());
+                            android.setAlert(notice.getNoticeMessage());
+                            ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
+                            break;
+                    }
+                    NotificationMessage notificationMessage = new NotificationMessage();
+                    notificationMessage.setAlert(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()));
+                    notificationMessage.setAndroid(android);
+                    notificationMessage.setIos(ios);
+                    body.setNotification(notificationMessage);
+                    // 目标人群
+                    To to = new To();
+                    to.setRegistrationIdList(ens.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
+                    // 指定目标
+                    param.setTo(to);
+                    
+                    // 指定平台
+                    body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
+                    
+                    // 发送
+                    param.setBody(body);
+                    PushResult result = pushApi.push(param);
+                    log.info("英文极光推送结果:{}", result);
+                }
+                
+                List<UserJPush> zhs = languages.get("zh");
+                if (CollectionUtil.isNotEmpty(zhs)) {
+                    // android 通知内容
+                    NotificationMessage.Android android = new NotificationMessage.Android();
+                    // ios 通知内容
+                    NotificationMessage.IOS ios = new NotificationMessage.IOS();
+                    switch (notice.getNoticeType()) {
+                        default:
+                            android.setTitle(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()));
+                            android.setAlert(String.format(NoticeI18nUtil.get("zh", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
+                            ios.setAlert(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("zh", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
+                            break;
+                        case OTHER:
+                            android.setTitle(notice.getNoticeTitle());
+                            android.setAlert(notice.getNoticeMessage());
+                            ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
+                            break;
+                    }
+                    NotificationMessage notificationMessage = new NotificationMessage();
+                    notificationMessage.setAlert(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()));
+                    notificationMessage.setAndroid(android);
+                    notificationMessage.setIos(ios);
+                    body.setNotification(notificationMessage);
+                    // 目标人群
+                    To to = new To();
+                    to.setRegistrationIdList(zhs.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
+                    // 指定目标
+                    param.setTo(to);
+                    
+                    // 指定平台
+                    body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
+                    
+                    // 发送
+                    param.setBody(body);
+                    PushResult result = pushApi.push(param);
+                    log.info("中文极光推送结果:{}", result);
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+                log.error("极光推送失败:{}", e.getMessage());
             }
-            NotificationMessage notificationMessage = new NotificationMessage();
-            notificationMessage.setAlert(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()));
-            notificationMessage.setAndroid(android);
-            notificationMessage.setIos(ios);
-            body.setNotification(notificationMessage);
-            // 目标人群
-            To to = new To();
-            to.setRegistrationIdList(ens.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
-            // 指定目标
-            param.setTo(to);
-            
-            // 指定平台
-            body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
-            
-            // 发送
-            param.setBody(body);
-            PushResult result = pushApi.push(param);
-            log.info("英文极光推送结果:{}", result);
-        }
-        
-        List<UserJPush> zhs = languages.get("zh");
-        if (CollectionUtil.isNotEmpty(zhs)) {
-            // android 通知内容
-            NotificationMessage.Android android = new NotificationMessage.Android();
-            // ios 通知内容
-            NotificationMessage.IOS ios = new NotificationMessage.IOS();
-            switch (notice.getNoticeType()) {
-                default:
-                    android.setTitle(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()));
-                    android.setAlert(String.format(NoticeI18nUtil.get("zh", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
-                    ios.setAlert(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("zh", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
-                    break;
-                case OTHER:
-                    android.setTitle(notice.getNoticeTitle());
-                    android.setAlert(notice.getNoticeMessage());
-                    ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
-                    break;
-            }
-            NotificationMessage notificationMessage = new NotificationMessage();
-            notificationMessage.setAlert(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()));
-            notificationMessage.setAndroid(android);
-            notificationMessage.setIos(ios);
-            body.setNotification(notificationMessage);
-            // 目标人群
-            To to = new To();
-            to.setRegistrationIdList(zhs.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
-            // 指定目标
-            param.setTo(to);
-            
-            // 指定平台
-            body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
-            
-            // 发送
-            param.setBody(body);
-            PushResult result = pushApi.push(param);
-            log.info("中文极光推送结果:{}", result);
-        }
+        });
         
     }