1 配置
1.0 application.yml
task:
pool:
corePoolSize: 3
maxPoolSize: 5
keepAliveSeconds: 300
queueCapacity: 30
1.2 ThreadPoolConfig.java
package com.company.ddd.infrastructure.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Thread pool configuration.
* @author xindaqi
* @since 2020-10-17
*/
@ConfigurationProperties(prefix = "task.pool")
public class ThreadPoolConfig {
private Integer corePoolSize;
private Integer maxPoolSize;
private Integer keepAliveSeconds;
private Integer queueCapacity;
public void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
public Integer getCorePoolSize() {
return corePoolSize;
}
public void setMaxPoolSize(Integer maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public Integer getMaxPoolSize() {
return maxPoolSize;
}
public void setKeepAliveSeconds(Integer keepAliveSeconds) {
this.keepAliveSeconds = keepAliveSeconds;
}
public Integer getKeepAliveSeconds() {
return keepAliveSeconds;
}
public void setQueueCapacity(Integer queueCapacity) {
this.queueCapacity = queueCapacity;
}
public Integer getQueueCapacity() {
return queueCapacity;
}
}
1.3 ThreadPoolExecutorUtil.java
package com.company.ddd.infrastructure.util;
import com.company.ddd.infrastructure.config.ThreadPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* Thread pool execute.
* @author xindaqi
* @since 2020-10-17
*/
@Configuration
@EnableAsync
public class ThreadPoolExecutorUtil {
@Autowired
private ThreadPoolConfig threadPoolConfig;
@Bean
public Executor threadPoolExecutorExe() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
executor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds());
executor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
executor.setThreadNamePrefix("MyThreadPool-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
2 服务层
2.1 IThreadPoolService.java
package com.company.ddd.application.service;
import java.util.List;
/**
* Threadpool service.
* @author xindaqi
* @since 2020-10-17
*/
public interface IThreadPoolService {
public void dataProcessBatch(List<Integer> params);
}
2.2 ThreadPoolServiceImpl.java
package com.company.ddd.domain.common.service;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Async;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.company.ddd.application.service.*;
/**
* Thread pool service implements.
* @author xindaqi
* @since 2020-10-17
*/
@Service
public class ThreadPoolServiceImpl implements IThreadPoolService{
static Logger logger = LoggerFactory.getLogger(ThreadPoolServiceImpl.class);
@Override
@Async
public void dataProcessBatch(List<Integer> params) {
logger.info("Start asynchronized task");
try {
List<Integer> processData = new ArrayList<>();
params.forEach(data -> {
Integer process = data + 1;
processData.add(process);
});
TimeUnit.SECONDS.sleep(8);
System.out.println("Data:" + processData);
}catch(Exception e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
}
logger.info("End asynchronized task");
}
}
3 启动层
package com.company.ddd;
import org.springframework.boot.SpringApplication;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.company.ddd.infrastructure.config.ThreadPoolConfig;
/**
* @author xindaqi
* @since 2020-10-06
*/
@SpringBootApplication
@MapperScan("com.company.ddd.infrastructure.dao.*")
@EnableConfigurationProperties({ThreadPoolConfig.class})
public class DddApplication {
public static Logger logger = LoggerFactory.getLogger(DddApplication.class);
public static void main(String[] args) {
SpringApplication.run(DddApplication.class, args);
logger.info("启动--ddd projeect");
}
}
3 接口层
package com.company.ddd.interfaces.facade;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.ddd.application.service.*;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
/**
* Thread pool controller.
* @author xindaqi
* @since 2020-10-17
*/
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api/threadpool")
@RestController
public class ThreadPool {
@Autowired
private IThreadPoolService threadPoolService;
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String threadPoolTest() {
for(int i = 0; i < 5; i++){
List<Integer> params = Arrays.asList(1, 2, 3, 4);
threadPoolService.dataProcessBatch(params);
}
return "success";
}
}
- 结果
2020-10-18 00:54:56,499][MyThreadPool-1][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:28)] Start asynchronized task
[2020-10-18 00:54:56,499][MyThreadPool-2][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:28)] Start asynchronized task
[2020-10-18 00:54:56,503][MyThreadPool-3][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:28)] Start asynchronized task
Data:[2, 3, 4, 5]
Data:[2, 3, 4, 5]
[2020-10-18 00:55:04,503][MyThreadPool-1][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:43)] End asynchronized task
[2020-10-18 00:55:04,503][MyThreadPool-2][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:43)] End asynchronized task
[2020-10-18 00:55:04,504][MyThreadPool-2][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:28)] Start asynchronized task
[2020-10-18 00:55:04,504][MyThreadPool-1][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:28)] Start asynchronized task
Data:[2, 3, 4, 5]
[2020-10-18 00:55:04,509][MyThreadPool-3][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:43)] End asynchronized task
Data:[2, 3, 4, 5]
[2020-10-18 00:55:12,507][MyThreadPool-2][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:43)] End asynchronized task
Data:[2, 3, 4, 5]
[2020-10-18 00:55:12,507][MyThreadPool-1][INFO][com.company.ddd.domain.common.service.ThreadPoolServiceImpl.dataProcessBatch(ThreadPoolServiceImpl.java:43)] End asynchronized task
【参考文献】
[1]https://blog.csdn.net/blacktulip_feng/article/details/99698189
[2]https://blog.csdn.net/Design407/article/details/97036465
[3]https://blog.csdn.net/u011523796/article/details/79537055
[4]https://blog.csdn.net/weixin_43778179/article/details/93750558