Skip to content

Commit

Permalink
add taskExecutorConf, add async annotation, add code comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 22, 2023
1 parent ee54837 commit fa16b55
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.EnableTransactionManagement;

//@Async // enable async, let Spring auto do op with async
@ComponentScan(basePackages = "com.yen.springWarehouse")
@org.mybatis.spring.annotation.MapperScan("com.yen.springWarehouse.mapper")
@EnableTransactionManagement // TODO : recheck this
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.yen.springWarehouse.config;

// https://github.com/timebusker/spring-boot/blob/master/spring-boot-5-Async/src/main/java/cn/timebusker/config/TaskExecutorConfig.java

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer{

/**
* Set the ThreadPoolExecutor's core pool size.
*/
private static final int CORE_POOL_SIZE = 5;

/**
* Set the ThreadPoolExecutor's maximum pool size.
*/
private static final int MAX_POOL_SIZE = 20;

/**
* Set the capacity for the ThreadPoolExecutor's BlockingQueue.
*/
private static final int QUEUE_CAPACITY = 10;

/**
* 通过重写getAsyncExecutor方法,制定默认的任务执行由该方法产生
*
* 配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor
* 这样我们就获得了一个基于线程池的TaskExecutor
*/
@Override
public Executor getAsyncExecutor() {

System.out.println(">>> getAsyncExecutor ...");

ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(CORE_POOL_SIZE);// 线程池维护线程的最少数量
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);// 线程池维护线程的最大数量
taskExecutor.setQueueCapacity(QUEUE_CAPACITY);// 线程池所使用的缓冲队列
taskExecutor.initialize();

return taskExecutor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}

/**
* 自定义任务执行器:在定义了多个任务执行器的情况下,可以使用@Async("getMineAsync")来设定
*
* @return
*/
@Bean
public Executor getMineAsync() {

System.out.println(">>> getMineAsync ... ");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(CORE_POOL_SIZE - 4);
executor.setMaxPoolSize(MAX_POOL_SIZE - 10);
executor.setQueueCapacity(QUEUE_CAPACITY - 5);
executor.setThreadNamePrefix("mineAsync-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ public String list(Map<String, Object> map, @RequestParam(value="pageNo", requir
}

@GetMapping("/create_report")
public String createDownload() {
public String createDownload() throws InterruptedException {

DateTimeUtils dateTimeUtils = new DateTimeUtils();
String timestamp = dateTimeUtils.getCurrentDateYYYYMMDDHHMMSS();
FileUtil fileUtil = new FileUtil();

// log.info("--------------------------------------------\n");
// log.info(">>>>>>>>> SLEEP 10 SECONDS >>>>>>>>>\n");
// Thread.sleep(10000);
// log.info("--------------------------------------------\n");

// create report
String fileName = timestamp + "_report.json";
Map<String, Object> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.yen.springWarehouse.bean.DownloadStatus;
import com.yen.springWarehouse.mapper.DownloadStatusMapper;
import com.yen.springWarehouse.service.DownloadStatusService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import com.yen.springWarehouse.mapper.ProductTypeMapper;
import com.yen.springWarehouse.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// NOTE below !!!
// -> we don't implement every abstract method in ProductTypeService, but we extend ServiceImpl first
// then implement rest of the abstract methods
@Service
//@Async("getMineAsync")
@Transactional(rollbackFor = Exception.class)
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

Expand Down

0 comments on commit fa16b55

Please sign in to comment.