往期精选
同事埋了个坑:Insert into select语句把生产服务器“炸”了
Java中如何优雅的实现时间控制,解决Token失效及表单重复提交问题
消息队列应用场景及ActiveMQ、RabbitMQ、RocketMQ、Kafka对比
图解 Spring 循环依赖,近两年流行的 Java 面试题!
Java 8 中 Map 骚操作之 merge() 的用法分析
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
<exclusions>
<exclusion>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-tomcat</artifactid>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-undertow</artifactid>
</dependency>
server:
undertow:
direct-buffers: true
io-threads: 4
worker-threads: 160
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
@EnableAsync
@SpringBootApplication
public class AppApplication
{
public static void main(String[] args)
{
SpringApplication.run(AppApplication.class, args);
}
}
@Async
public Future<string> doReturn(int i){
try {
// 这个方法需要调用500毫秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 消息汇总
return new AsyncResult<>("异步调用");
}
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskDecorator;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.Map;
import java.util.concurrent.Executor;
/**
* @Description:
*/
@EnableAsync
@Configuration
public class AsyncConfig extends AsyncConfigurerSupport {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setTaskDecorator(new MdcTaskDecorator());
executor.initialize();
return executor;
}
}
class MdcTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
Map<string, string> contextMap = MDC.getCopyOfContextMap();
return () -> {
try {
MDC.setContextMap(contextMap);
runnable.run();
} finally {
MDC.clear();
}
};
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@Slf4j
public class TaskExecutorConfig {
@Bean("localDbThreadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(200);
taskExecutor.setQueueCapacity(200);
taskExecutor.setKeepAliveSeconds(100);
taskExecutor.setThreadNamePrefix("LocalDbTaskThreadPool");
taskExecutor.setRejectedExecutionHandler((Runnable r, ThreadPoolExecutor executor) -> {
if (!executor.isShutdown()) {
try {
Thread.sleep(300);
executor.getQueue().put(r);
} catch (InterruptedException e) {
log.error(e.toString(), e);
Thread.currentThread().interrupt();
}
}
}
);
taskExecutor.initialize();
return taskExecutor;
}
}
作者:lipengxs
my.oschina.net/lipengxs/blog/3162343
往期精选
同事埋了个坑:Insert into select语句把生产服务器“炸”了
Java中如何优雅的实现时间控制,解决Token失效及表单重复提交问题
消息队列应用场景及ActiveMQ、RabbitMQ、RocketMQ、Kafka对比
图解 Spring 循环依赖,近两年流行的 Java 面试题!
Java 8 中 Map 骚操作之 merge() 的用法分析