浏览代码

chore[litemall-core]: 采用默认异步调度器。添加测试类。

Junling Bu 7 年之前
父节点
当前提交
c28573c8eb

+ 9 - 0
litemall-core/src/main/java/org/linlinjava/litemall/core/config/AsyncConfig.java

@@ -0,0 +1,9 @@
+package org.linlinjava.litemall.core.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+
+@Configuration
+@EnableAsync
+public class AsyncConfig {
+}

+ 0 - 40
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/ExecutorConfig.java

@@ -1,40 +0,0 @@
-package org.linlinjava.litemall.core.notify;
-
-
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.scheduling.annotation.EnableAsync;
-import org.springframework.scheduling.annotation.EnableScheduling;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
-
-import java.util.concurrent.Executor;
-import java.util.concurrent.ThreadPoolExecutor;
-
-/**
- * 异步线程池,用于异步发送通知
- */
-@Configuration
-@EnableScheduling
-@EnableAsync
-class ExecutorConfig {
-
-    @Value("${NotifyPoolConfig.corePoolSize}")
-    private int corePoolSize;
-    @Value("${NotifyPoolConfig.maxPoolSize}")
-    private int maxPoolSize;
-    @Value("${NotifyPoolConfig.queueCapacity}")
-    private int queueCapacity;
-
-    @Bean(name = "notifyAsync")
-    public Executor notifyAsync() {
-        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
-        executor.setCorePoolSize(corePoolSize);
-        executor.setMaxPoolSize(maxPoolSize);
-        executor.setQueueCapacity(queueCapacity);
-        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
-        executor.setThreadNamePrefix("NotifyExecutor-");
-        executor.initialize();
-        return executor;
-    }
-}

+ 5 - 5
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/LitemallNotifyService.java

@@ -17,7 +17,7 @@ public class LitemallNotifyService {
     @Autowired
     private WXTemplateSendService wxTemplateSendService;
 
-    @Async("notifyAsync")
+    @Async
     public void notifySMSMessage(String phoneNumber, String message) {
         if (!smsSendService.config.isEnable())
             return;
@@ -33,7 +33,7 @@ public class LitemallNotifyService {
      * @param notifyType    通知类别,通过该枚举值在配置文件中获取相应的模版ID
      * @param params        通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
      */
-    @Async("notifyAsync")
+    @Async
     public void notifyWXTemplate(String token,String touser, String formId, ConfigUtil.NotifyType notifyType, String[] params) {
         if (!wxTemplateSendService.config.isEnable())
             return;
@@ -51,7 +51,7 @@ public class LitemallNotifyService {
      * @param notifyType  通知类别,通过该枚举值在配置文件中获取相应的模版ID
      * @param params      通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
      */
-    @Async("notifyAsync")
+    @Async
     public void notifySMSTemplate(String phoneNumber, ConfigUtil.NotifyType notifyType, String[] params) {
         if (!smsSendService.config.isEnable())
             return;
@@ -69,7 +69,7 @@ public class LitemallNotifyService {
      * @param templateId  模板ID
      * @param params      通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
      */
-    @Async("notifyAsync")
+    @Async
     public void notifySMSTemplate(String phoneNumber, int templateId, String[] params) {
         if (!smsSendService.config.isEnable())
             return;
@@ -83,7 +83,7 @@ public class LitemallNotifyService {
      * @param setSubject 邮件标题
      * @param setText    邮件内容
      */
-    @Async("notifyAsync")
+    @Async
     public void notifyMailMessage(String setSubject, String setText) {
         if (!mailSendService.config.isEnable())
             return;

+ 0 - 6
litemall-core/src/main/resources/application.yaml

@@ -34,12 +34,6 @@ WXNotifyConfig:
     - name: captcha
       templateId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
-# 发送线程池配置
-NotifyPoolConfig:
-  corePoolSize: 5
-  maxPoolSize: 100
-  queueCapacity: 50
-
 #快鸟物流查询配置
 express:
   appId: "XXXXXXXXX"

+ 18 - 0
litemall-core/src/test/java/org/linlinjava/litemall/core/AsyncTask.java

@@ -0,0 +1,18 @@
+package org.linlinjava.litemall.core;
+
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+@Service
+public class AsyncTask {
+    @Async
+    public void asyncMethod() {
+        System.out.println("Execute method asynchronously. "
+                + Thread.currentThread().getName());
+    }
+
+    public void nonasyncMethod() {
+        System.out.println("Execute method nonasynchronously. "
+                + Thread.currentThread().getName());
+    }
+}

+ 26 - 0
litemall-core/src/test/java/org/linlinjava/litemall/core/AsyncTest.java

@@ -0,0 +1,26 @@
+package org.linlinjava.litemall.core;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+/**
+ * 异步测试
+ *
+ */
+@WebAppConfiguration
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+public class AsyncTest {
+
+    @Autowired AsyncTask task;
+
+    @Test
+    public void test() {
+        task.asyncMethod();
+        task.nonasyncMethod();
+    }
+}