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

mvn 创建project

mvn archetype:generate -DgroupId=com.denk -DartifactId=task-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

导入IDEA后的项目结构

c256f51c3021

项目结构图

pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

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...");

}

}

c256f51c3021

打印结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值