线程池异步日志代码实现

144 阅读1分钟
package com.aosiding;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync //开启异步请求
public class ThreadPoolConfig {

    @Resource
    private Environment env;

    //创建线程池
    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setThreadNamePrefix("--------------全局线程池-----------------");
        System.out.println(("--------------全局线程池-----------------"));
        pool.setCorePoolSize(Integer.parseInt(env.getProperty("threadpool.corePoolSize")));
        pool.setMaxPoolSize(Integer.parseInt(env.getProperty("threadpool.maxPoolSize")));
        pool.setKeepAliveSeconds(Integer.parseInt(env.getProperty("threadpool.queueCapacity")));
        pool.setQueueCapacity(Integer.parseInt(env.getProperty("threadpool.keepAliveSeconds")));
        // 直接在execute方法的调用线程中运行
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        pool.initialize();
        return pool;
    }
}
package com.aosiding.controller;

import com.aosiding.AsynchronousTask;
import com.aosiding.pojo.Product;
import com.aosiding.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author AO SIDING
 * Created on 2022/9/16 23:48
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Resource
    private AsynchronousTask asynchronousTask;

    @GetMapping("list")
    public List<Product> selectProduuctList(){
        return productService.selectProductList();
    }

    @GetMapping("login")
    public String login(){
        //登录处理
        Long userId=10001L;
        boolean isSucc=true;
        if(isSucc){
            asynchronousTask.recordLoginLog(userId);
        }
        System.out.println("=====登录成功====");
        return "登录成功";
    }

}
package com.aosiding;

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

@Component
public class AsynchronousTask {

    @Async("taskExecutor")
    public void recordLoginLog(Long userId){
        System.out.println("======== 登录日志记录=====start=======");
        try {
            // TODO: 2022/4/14 业务处理
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("========登录日志记录------end=======");
    }
}