Skip to content

Commit

Permalink
perf($Starter): enable Spring Boot async task
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jan 29, 2021
1 parent 07dfbff commit 418fc43
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;
Expand All @@ -56,7 +54,8 @@
Swagger2Configuration.class,
SftpConfiguration.class,
WebSecurityConfiguration.class,
RestTemplateConfiguration.class
RestTemplateConfiguration.class,
AsyncConfiguration.class
})
public class MafAutoConfiguration {
@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.jmsoftware.maf.springbootstarter.configuration;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
* Description: AsyncConfiguration, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 1/29/2021 4:39 PM
**/
@Slf4j
@EnableAsync
@Configuration
@RequiredArgsConstructor
public class AsyncConfiguration implements AsyncConfigurer {
@Bean
@Override
public AsyncTaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
executor.setMaxPoolSize(100);
executor.setBeanName("spring-boot-starter-thread-pool-task-executor");
// Specify the RejectedExecutionHandler to use for the ExecutorService.
// Default is the ExecutorService's default abort policy.
executor.setRejectedExecutionHandler((runnable, executor1) -> {
log.error("Captured rejected execution. {}", runnable);
// TODO: runnable persistence
});
executor.initialize();
log.warn("Initial bean: '{}'", AsyncTaskExecutor.class.getSimpleName());
return executor;
}
}

0 comments on commit 418fc43

Please sign in to comment.