• SpringBoot 使用异步方法


    SpringBoot 使用异步方法

    在pom文件引入相关依赖:

    <dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starterartifactId>
    		dependency>
    
    		<dependency>
    			<groupId>org.springframeworkgroupId>
    			<artifactId>spring-webartifactId>
    		dependency>
    		<dependency>
    			<groupId>com.fasterxml.jackson.coregroupId>
    			<artifactId>jackson-databindartifactId>
    		dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建一个接收数据的实体:

    @JsonIgnoreProperties(ignoreUnknown=true)
    public class User {
    
        private String name;
        private String blog;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getBlog() {
            return blog;
        }
    
        public void setBlog(String blog) {
            this.blog = blog;
        }
    
        @Override
        public String toString() {
            return "User [name=" + name + ", blog=" + blog + "]";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    创建一个请求的 githib的service:

    @Service
    public class GitHubLookupService {
    
        private static final Logger logger = LoggerFactory.getLogger(GitHubLookupService.class);
    
        private final RestTemplate restTemplate;
    
        public GitHubLookupService(RestTemplateBuilder restTemplateBuilder) {
            this.restTemplate = restTemplateBuilder.build();
        }
    
        @Async
        public Future<User> findUser(String user) throws InterruptedException {
            logger.info("Looking up " + user);
            String url = String.format("https://api.github.com/users/%s", user);
            User results = restTemplate.getForObject(url, User.class);
            // Artificial delay of 1s for demonstration purposes
            Thread.sleep(1000L);
            return new AsyncResult<>(results);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    通过,RestTemplate去请求,另外加上类@Async 表明是一个异步任务。

    开启异步任务:

    @SpringBootApplication
    @EnableAsync
    public class Application extends AsyncConfigurerSupport {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(2);
            executor.setMaxPoolSize(2);
            executor.setQueueCapacity(500);
            executor.setThreadNamePrefix("GithubLookup-");
            executor.initialize();
            return executor;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    通过@EnableAsync开启异步任务;并且配置AsyncConfigurerSupport,比如最大的线程池为2.

    测试

    测试代码如下:

    @Component
    public class AppRunner implements CommandLineRunner {
    
        private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);
    
        private final GitHubLookupService gitHubLookupService;
    
        public AppRunner(GitHubLookupService gitHubLookupService) {
            this.gitHubLookupService = gitHubLookupService;
        }
    
        @Override
        public void run(String... args) throws Exception {
            // Start the clock
            long start = System.currentTimeMillis();
    
            // Kick of multiple, asynchronous lookups
            Future page1 = gitHubLookupService.findUser("PivotalSoftware");
            Future page2 = gitHubLookupService.findUser("CloudFoundry");
            Future page3 = gitHubLookupService.findUser("Spring-Projects");
    
            // Wait until they are all done
            while (!(page1.isDone() && page2.isDone() && page3.isDone())) {
                Thread.sleep(10); //10-millisecond pause between each check
            }
    
            // Print results, including elapsed time
            logger.info("Elapsed time: " + (System.currentTimeMillis() - start));
            logger.info("--> " + page1.get());
            logger.info("--> " + page2.get());
            logger.info("--> " + page3.get());
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    启动程序,控制台会打印:

    2017-04-30 13:11:10.351 INFO 1511 — [ GithubLookup-1] com.forezp.service.GitHubLookupService : Looking up PivotalSoftware 2017-04-30 13:11:10.351 INFO 1511 — [ GithubLookup-2] com.forezp.service.GitHubLookupService : Looking up CloudFoundry 2017-04-30 13:11:13.144 INFO 1511 — [ GithubLookup-2] com.forezp.service.GitHubLookupService : Looking up Spring-Projects

    耗时:3908

    分析:可以卡的前面2个方法分别在GithubLookup-1 和GithubLookup-2执行,第三个在GithubLookup-2执行,注意因为在配置线程池的时候最大线程为2.如果你把线程池的个数为3的时候,耗时减少。

    如果去掉@Async,你会发现,执行这三个方法都在main线程中执行。耗时总结,如下:

    2017-04-30 13:13:00.934 INFO 1527 — [ main] com.forezp.service.GitHubLookupService : Looking up PivotalSoftware 2017-04-30 13:13:03.571 INFO 1527 — [ main] com.forezp.service.GitHubLookupService : Looking up CloudFoundry 2017-04-30 13:13:04.865 INFO 1527 — [ main] com.forezp.service.GitHubLookupService : Looking up Spring-Projects

    耗时:5261

    通过这一个小的栗子,你应该对异步任务有了一定的了解。

  • 相关阅读:
    python-opencv 图像处理基础 (十)图像膨胀腐蚀+开闭操作
    LAMMPS实操系列(二): 大量FCC-CoCrCuFeNi高熵合金建模与最稳定结构筛选
    【Qt】简单聊聊信号与槽
    Spring源码级笔记(一)
    上周热点回顾(9.12-9.18)
    Go 微服务开发框架 DMicro 的设计思路
    【Git】Git下载安装&环境配置 下载速度慢的解决方案
    mybatis字段映射的容错性
    第三十五章 Objects - 集合类
    深度学习实战基础案例——卷积神经网络(CNN)基于Xception的猫狗识别|第2例
  • 原文地址:https://blog.csdn.net/qq_45525848/article/details/132922745