笔记:Spring Boot-Java Config 异步任务(自定义线程池和异常处理)

mvn 创建project
mvn archetype:generate -DgroupId=com.denk -DartifactId=task-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
导入IDEA后的项目结构
笔记:Spring Boot-Java Config 异步任务(自定义线程池和异常处理)_第1张图片
项目结构图
pom.xml

    4.0.0
    com.denk
    task-demo
    jar
    1.0-SNAPSHOT
    task-demo
    http://maven.apache.org
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                1.5.8.RELEASE
                pom
                import
            
        
    

Spring Boot 启动类
package com.denk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}
Controller.java
package com.denk.controller;

import com.denk.service.AsyncStatisticsService;
import com.denk.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private AsyncStatisticsService asyncStatisticsService;

    @RequestMapping("/demo")
    public String demo() {
        commonService.dealCommonService_1();
        asyncStatisticsService.asyncStatistics();
        commonService.dealCommonService_2();
        return "SUCCESS";
    }
}
异步统计类 AsyncStatisticsService.java
package com.denk.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Async
public class AsyncStatisticsService {

    public void asyncStatistics() {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("异步统计业务...");
        throw new IllegalArgumentException("异步统计业务异常...");
    }
}
普通业务类 CommonService.java
package com.denk.service;

import org.springframework.stereotype.Service;

@Service
public class CommonService {
    public void dealCommonService_1(){
        System.out.println("同步业务处理1111...");
    }

    public void dealCommonService_2(){
        System.out.println("同步业务处理2222...");
    }
}
异步配置类 TaskExecutorConfig.java 线程池和异步异常处理
package com.denk.config;

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;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class TaskExecutorConfig {

    @Bean
    public AsyncConfigurer getAsyncConfigurer() {
        AsyncConfigurer asyncConfigurer = new AsyncConfigurer() {
            public Executor getAsyncExecutor() {
                ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
                taskExecutor.setCorePoolSize(5);
                taskExecutor.setMaxPoolSize(20);
                taskExecutor.setQueueCapacity(100);
                taskExecutor.initialize();
                return taskExecutor;
            }

            public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
                return new MyAsyncExceptionHandler();
            }
        };
        return asyncConfigurer;
    }
}

class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
        System.out.print("handleUncaughtException...");
    }
}
访问http://localhost:8080/demo,控制台打印结果
打印结果

你可能感兴趣的:(笔记:Spring Boot-Java Config 异步任务(自定义线程池和异常处理))