欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot使用自定义配置的线程池执行Async异步任务

程序员文章站 2023-12-16 10:23:10
在前面的博客中, 我们使用了spring boot的异步操作,当时,我们使用的是默认的线程池,但是,如果我们想根据项目来定制自己的线程池了,下面就来说说,如何定制线程池!...

在前面的博客中, 我们使用了spring boot的异步操作,当时,我们使用的是默认的线程池,但是,如果我们想根据项目来定制自己的线程池了,下面就来说说,如何定制线程池!

一、增加配置属性类

package com.chhliu.springboot.async.configuration; 
import org.springframework.boot.context.properties.configurationproperties; 
 
@configurationproperties(prefix = "spring.task.pool") // 该注解的locations已经被启用,现在只要是在环境中,都会优先加载 
public class taskthreadpoolconfig { 
 private int corepoolsize; 
 
 private int maxpoolsize; 
 
 private int keepaliveseconds; 
 
 private int queuecapacity; 
  
 …………省略getter,setter方法………… 
} 

二、创建线程池

package com.chhliu.springboot.async.pool; 
import java.util.concurrent.executor; 
import java.util.concurrent.threadpoolexecutor; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.context.annotation.bean; 
import org.springframework.context.annotation.configuration; 
import org.springframework.scheduling.annotation.enableasync; 
import org.springframework.scheduling.concurrent.threadpooltaskexecutor; 
 
import com.chhliu.springboot.async.configuration.taskthreadpoolconfig; 
 
@configuration 
@enableasync 
public class taskexecutepool { 
 
 @autowired 
 private taskthreadpoolconfig config; 
 
 @bean 
 public executor mytaskasyncpool() { 
  threadpooltaskexecutor executor = new threadpooltaskexecutor(); 
  executor.setcorepoolsize(config.getcorepoolsize()); 
  executor.setmaxpoolsize(config.getmaxpoolsize()); 
  executor.setqueuecapacity(config.getqueuecapacity()); 
  executor.setkeepaliveseconds(config.getkeepaliveseconds()); 
  executor.setthreadnameprefix("myexecutor-"); 
 
  // rejection-policy:当pool已经达到max size的时候,如何处理新任务 
  // caller_runs:不在新线程中执行任务,而是由调用者所在的线程来执行 
  executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy()); 
  executor.initialize(); 
  return executor; 
 } 
} 

三、在主类中开启配置支持

package com.chhliu.springboot.async; 
import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.springbootapplication; 
import org.springframework.boot.context.properties.enableconfigurationproperties; 
import org.springframework.scheduling.annotation.enableasync; 
 
import com.chhliu.springboot.async.configuration.taskthreadpoolconfig; 
 
@springbootapplication 
@enableasync 
@enableconfigurationproperties({taskthreadpoolconfig.class} ) // 开启配置属性支持 
public class springbootasyncapplication { 
 
 public static void main(string[] args) { 
  springapplication.run(springbootasyncapplication.class, args); 
 } 
} 

四、测试类

package com.chhliu.springboot.async.pool; 
 
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 
import org.springframework.scheduling.annotation.async; 
import org.springframework.stereotype.component; 
 
@component 
public class asynctask { 
 protected final logger logger = loggerfactory.getlogger(this.getclass()); 
  
 @async("mytaskasyncpool") //mytaskasynpool即配置线程池的方法名,此处如果不写自定义线程池的方法名,会使用默认的线程池 
 public void dotask1(int i) throws interruptedexception{ 
  logger.info("task"+i+" started."); 
 } 
} 

五、测试

package com.chhliu.springboot.async; 
import java.util.concurrent.executionexception; 
import org.junit.test; 
import org.junit.runner.runwith; 
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.boot.test.context.springboottest; 
import org.springframework.test.context.junit4.springrunner; 
 
import com.chhliu.springboot.async.pool.asynctask; 
 
@runwith(springrunner.class) 
@springboottest 
public class springbootasyncapplicationtests { 
 protected final logger logger = loggerfactory.getlogger(this.getclass()); 
 @autowired 
 private asynctask asynctask; 
 
 @test 
 public void asynctasktest() throws interruptedexception, executionexception { 
 
  for (int i = 0; i < 100; i++) { 
   asynctask.dotask1(i); 
  } 
 
  logger.info("all tasks finished."); 
 } 
} 

测试结果如下:

2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-10] c.c.springboot.async.pool.asynctask  : task60 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-25] c.c.springboot.async.pool.asynctask  : task61 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-6] c.c.springboot.async.pool.asynctask  : task62 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-23] c.c.springboot.async.pool.asynctask  : task63 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-20] c.c.springboot.async.pool.asynctask  : task64 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-19] c.c.springboot.async.pool.asynctask  : task65 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-16] c.c.springboot.async.pool.asynctask  : task66 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-15] c.c.springboot.async.pool.asynctask  : task67 started. 
2017-03-20 20:15:15.208 info 4068 --- [ myexecutor-12] c.c.springboot.async.pool.asynctask  : task68 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-1] c.c.springboot.async.pool.asynctask  : task69 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-11] c.c.springboot.async.pool.asynctask  : task81 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-8] c.c.springboot.async.pool.asynctask  : task82 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-7] c.c.springboot.async.pool.asynctask  : task83 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-4] c.c.springboot.async.pool.asynctask  : task84 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-29] c.c.springboot.async.pool.asynctask  : task85 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-21] c.c.springboot.async.pool.asynctask  : task86 started. 
2017-03-20 20:15:15.209 info 4068 --- [ myexecutor-17] c.c.springboot.async.pool.asynctask  : task88 started. 

测试结果ok!

六、配置默认的线程池

如果我们想使用默认的线程池,但是只是想修改默认线程池的配置,那怎么做了,此时我们需要实现asyncconfigurer类,示例代码如下:

import java.lang.reflect.method; 
import java.util.concurrent.executor; 
import java.util.concurrent.threadpoolexecutor; 
import org.springframework.aop.interceptor.asyncuncaughtexceptionhandler; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.context.annotation.configuration; 
import org.springframework.scheduling.annotation.asyncconfigurer; 
import org.springframework.scheduling.concurrent.threadpooltaskexecutor; 
import com.chhliu.cq.emailservice.threadconfiguration.taskthreadpoolconfig; 
import lombok.extern.slf4j.slf4j; 
 
/** 
 * 注意:该线程池被所有的异步任务共享,而不属于某一个异步任务 
 * 描述:配置异步任务的线程池 
 * @author chhliu 
 * 创建时间:2017年5月22日 上午10:20:56 
 * @version 1.2.0 
 */ 
@slf4j 
@configuration 
public class asynctaskexecutepool implements asyncconfigurer{ 
 
 @autowired 
 private taskthreadpoolconfig config; // 配置属性类,见上面的代码 
 
 @override 
 public executor getasyncexecutor() { 
  threadpooltaskexecutor executor = new threadpooltaskexecutor(); 
  executor.setcorepoolsize(config.getcorepoolsize()); 
  executor.setmaxpoolsize(config.getmaxpoolsize()); 
  executor.setqueuecapacity(config.getqueuecapacity()); 
  executor.setkeepaliveseconds(config.getkeepaliveseconds()); 
  executor.setthreadnameprefix("taskexecutor-"); 
 
  // rejection-policy:当pool已经达到max size的时候,如何处理新任务 
  // caller_runs:不在新线程中执行任务,而是由调用者所在的线程来执行 
  executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy()); 
  executor.initialize(); 
  return executor; 
 } 
 
 @override 
 public asyncuncaughtexceptionhandler getasyncuncaughtexceptionhandler() {// 异步任务中异常处理 
  return new asyncuncaughtexceptionhandler() { 
    
   @override 
   public void handleuncaughtexception(throwable arg0, method arg1, object... arg2) { 
    log.error("=========================="+arg0.getmessage()+"=======================", arg0); 
    log.error("exception method:"+arg1.getname()); 
   } 
  }; 
 } 
} 

使用的时候,只需在方法上加上@async即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: