Commit c28573c8 authored by Junling Bu's avatar Junling Bu
Browse files

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

parent a64988a8
package org.linlinjava.litemall.core.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
public class AsyncConfig {
}
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;
}
}
\ No newline at end of file
......@@ -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;
......
......@@ -34,12 +34,6 @@ WXNotifyConfig:
- name: captcha
templateId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 发送线程池配置
NotifyPoolConfig:
corePoolSize: 5
maxPoolSize: 100
queueCapacity: 50
#快鸟物流查询配置
express:
appId: "XXXXXXXXX"
......
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());
}
}
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();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment