mvn 创建project
mvn archetype:generate -DgroupId=com.denk -DartifactId=task-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
导入IDEA后的项目结构
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.denk</groupId>
<artifactId>task-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>task-demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Spring Boot-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
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...");
}
}