文章目录

  • 【java实战】异步任务使用
  • 一、线程配置类
  • 1.1 使用Executor
  • 1.2 使用ScheduledThreadPoolExecutor
  • 二、异步任务的使用


【java实战】异步任务使用

一、线程配置类

进行异步任务线程池的配置

下边两种方法定义线程池

1.1 使用Executor
package com.awifi.cloudnative.container.manage.provider.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ExecutorConfig {

   private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorConfig.class);

   private int corePoolSize = 2;
   private int maxPoolSize = 4;
   private int queueCapacity = 1000;


   @Bean("asyncJobExecutor")
   public Executor asyncJobExecutor() {
       LOGGER.info("start asyncKJobExecutor");
       ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
       //配置核心线程数
       executor.setCorePoolSize(corePoolSize);
       //配置最大线程数
       executor.setMaxPoolSize(maxPoolSize);
       //配置队列大小
       executor.setQueueCapacity(queueCapacity);
       //配置线程池中的线程的名称前缀
       executor.setThreadNamePrefix("upload-async-job");

       // rejection-policy:当pool已经达到max size的时候,如何处理新任务
       // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
       executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
       //执行初始化
       executor.initialize();
       return executor;
   }
}
1.2 使用ScheduledThreadPoolExecutor
package com.awifi.cloudnative.container.manage.provider.config;

import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @ClassName ThredConfig
* @Description: 定时任务线程池配置,解决线程阻塞问题导致的定时任务无法按时执行的问题
* @Author tangzy
* @Date 2020/9/2
* @Version V1.0
**/
@Configuration
public class ThreadConfig {

   /**
    * 初始化定时线程池
    * @return
    */
   @Bean
   public ScheduledThreadPoolExecutor scheduledExecutorService() {
       return new ScheduledThreadPoolExecutor(30);
   }
}

二、异步任务的使用

定义异步任务类

在类的方法上使用@Async(“xxxx”)开启线程

xxxx:

与线程配置类内@Bean内定义的名称一样

package com.awifi.cloudnative.container.manage.provider.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
* @Author 张鑫  集群扩容异步任务
* @Date 2022/3/10
* @Param
* @return
**/
@Component
@Slf4j
public class ClusterExpandAsyncJob {

   @Autowired
   private ResourceVMMapper vmMapper;


   @Async("asyncJobExecutor")
   public void addNode(String s, ClusterExpandRecord expandRecord, ClusterExpandDTO clusterExpandDTO, ClusterExpandVO clusterExpandVO) {

       String vmList = feignOpenStackService.creatVM(s);
       JSONObject vmJson = JSONObject.parseObject(vmList);
       
   }

}