• @EnableAsync和@Async开始异步任务支持


    Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程。使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.在开发中实现异步任务,我们可以在配置类中添加@EnableAsync开始对异步任务的支持,并在相应的方法中使用@Async注解来声明一个异步任务。
    配置类

    package com.xingguo.logistics.controller;
    
    import java.util.concurrent.Executor;
    
    import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.AsyncConfigurer;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    
    @Configuration
    @ComponentScan({"com.xingguo.logistics.service.aspect","com.xingguo.logistics.service.event"})
    //开始异步支持
    @EnableAsync
    public class AopConfig implements AsyncConfigurer{
    
        @Override
        public Executor getAsyncExecutor() {
             //线程池
            ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
            taskExecutor.setCorePoolSize(5);
            taskExecutor.setMaxPoolSize(10);
            taskExecutor.setQueueCapacity(25);
            taskExecutor.initialize();
            return taskExecutor;
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
             return null;
        }
    
    }

    任务执行service类

    package com.xingguo.logistics.service.aspect;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @Service
    public class TestService2 {
    
        //声明异步任务
        @Async
        public void executeAsyncTask(Integer i){
             System.out.println("执行异步任务:"+i);
        }
    
        @Async
        public void executeAsyncTask2(Integer i){
             System.out.println("执行异步任务2:"+i);
        }
    
    }<

    测试类

    package com.xingguo.logistics.controller;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import com.xingguo.logistics.service.aspect.TestService2;
    
    public class TestController {
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
    
        TestService2 testService2 = context.getBean(TestService2.class);
        for(int i = 0; i<10; i++){
            testService2.executeAsyncTask(i);
            testService2.executeAsyncTask2(i);
        }
        context.close();
    
        }
    }

    测试结果如下:
    这里写图片描述
    执行结果可以看出,并没有按照顺序来执行。

  • 相关阅读:
    vue2.0 + vux (六)NewsList 资讯页 及 NewsDetail 资讯详情页
    vue2.0 + vux (五)api接口封装 及 首页 轮播图制作
    vue2.0 + vux (四)Home页
    vue2.0 + vux (三)MySettings 页
    vue2.0 + vux (二)Footer组件
    vue2.0 + vux (一)Header 组件
    vue2 + typescript2 自定义过滤器
    flexible.js + makegrid.js 自适应布局
    vue-router钩子beforeRouteEnter函数获取到this实例
    weex 项目搭建
  • 原文地址:https://www.cnblogs.com/jpfss/p/9429001.html
Copyright © 2020-2023  润新知