Spring Boot(十)使用@Async实现异步调用


项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot(十)使用@Async实现异步调用 ,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.10



Spring Boot启动类,增加@EnableAsync注解配置:

src/main/java/com/training/SpringBootServlet.java

package com.training;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class SpringBootServlet extends SpringBootServletInitializer {

	// jar启动
	public static void main(String[] args) {
        SpringApplication.run(SpringBootServlet.class, args);
    }

	// tomcat war启动
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootServlet.class);
    }

}



测试:


增加异步方法Service,线程休眠:

package com.training.async.service.impl;

import java.util.Random;
import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import com.training.async.service.DemoAsyncService;

@Service
public class DemoAsyncServiceImpl implements DemoAsyncService {

	public static Random random =new Random();

    @Async
    public Future<String> doTaskOne() throws Exception {
        System.out.println("开始做任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>("任务一完成");
    }

    @Async
    public Future<String> doTaskTwo() throws Exception {
        System.out.println("开始做任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>("任务二完成");
    }

    @Async
    public Future<String> doTaskThree() throws Exception {
        System.out.println("开始做任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>("任务三完成");
    }
	
}


调用异步测试测试,查看控制台输出执行顺序:

package com.training.async.controller;

import io.swagger.annotations.ApiOperation;

import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.training.async.service.DemoAsyncService;
import com.training.core.dto.ResultDataDto;

@RestController
@RequestMapping(value="/async") 
public class DemoAsyncController {

	@Resource
	private DemoAsyncService demoAsyncService;

	/**
	 * 测试异步方法调用顺序
	 */
	@ApiOperation(value="测试异步方法调用顺序", notes="getEntityById")
    @RequestMapping(value = "/getTestDemoAsync", method = RequestMethod.GET)
	public @ResponseBody ResultDataDto getEntityById() throws Exception {
    	
		long start = System.currentTimeMillis();

		Future<String> task1 = demoAsyncService.doTaskOne();
		Future<String> task2 = demoAsyncService.doTaskTwo();
		Future<String> task3 = demoAsyncService.doTaskThree();

		while(true) {
			if(task1.isDone() && task2.isDone() && task3.isDone()) {
				// 三个任务都调用完成,退出循环等待
				break;
			}
			Thread.sleep(1000);
		}

		long end = System.currentTimeMillis();

		System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒");
		return ResultDataDto.addSuccess();
	}
}


















  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值