SpringBoot异步任务@EnableAsync

1、启动类加注解@EnableAsync开启异步任务,自动扫描:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync //开启异步
public class Application {

    public static void main(String[] args) {
        final SpringApplication application = new SpringApplication(com.xxx.xxx.Application.class);
        application.run(args);
    }
}

2、定义异步任务并使用@Component标记组件被容器扫描,异步方法加上@Async

注意:

1)要把异步任务封装到类里面,不能直接写到Controller;

2)增加Future<String>返回结果AsyncResult<String>("task执行完成")

3)如果需要拿到结果,需要判断全部的task.isDone();

4)通过注入方式,注入到Controller里面,如果改为同步任务需要把@Async注释掉;

4)异步任务无返回结果。

异步任务业务类:

// 异步任务业务类
@Component
@Async
public class AsyncTask {

	public void task1() throws InterruptedException {
		long begin = System.currentTimeMillis();
		TimeUnit.SECONDS.sleep(1);
		long end = System.currentTimeMillis();
		System.out.println("任务1耗时=" + (end - begin));
	}

	public void task2() throws InterruptedException {
		long begin = System.currentTimeMillis();
		TimeUnit.SECONDS.sleep(2);
		long end = System.currentTimeMillis();
		System.out.println("任务2耗时=" + (end - begin));
	}

	public void task3() throws InterruptedException {
		long begin = System.currentTimeMillis();
		TimeUnit.SECONDS.sleep(3);
		long end = System.currentTimeMillis();
		System.out.println("任务3耗时=" + (end - begin));
	}

}

测试类:

@RestController
@RequestMapping("/api/v1")
public class UserController {
	@Autowired
	private AsyncTask asyncTask;

	@RequestMapping("async_task")
	public String exeTask() throws InterruptedException {
		long begin = System.currentTimeMillis();
		asyncTask.task1();
		asyncTask.task2();
		asyncTask.task3();
		long end = System.currentTimeMillis();
		System.out.println("执行总耗时=" + (end - begin));
		return "success";
	}
}

运行结果:

执行总耗时=0
任务1耗时=1001
任务2耗时=2000
任务3耗时=3000

异步任务存在返回结果:

// 异步任务业务类
@Component
@Async
public class AsyncTask {

	public Future<String> task1() throws InterruptedException {
		long begin = System.currentTimeMillis();
		TimeUnit.SECONDS.sleep(1);
		long end = System.currentTimeMillis();
		System.out.println("任务1耗时=" + (end - begin));
		return new AsyncResult<String>("任务1");
	}

	public Future<String> task2() throws InterruptedException {
		long begin = System.currentTimeMillis();
		TimeUnit.SECONDS.sleep(2);
		long end = System.currentTimeMillis();
		System.out.println("任务2耗时=" + (end - begin));
		return new AsyncResult<String>("任务2");
	}

	public Future<String> task3() throws InterruptedException {
		long begin = System.currentTimeMillis();
		TimeUnit.SECONDS.sleep(3);
		long end = System.currentTimeMillis();
		System.out.println("任务3耗时=" + (end - begin));
		return new AsyncResult<String>("任务3");
	}

}

测试类:

@RestController
@RequestMapping("/api/v1")
public class UserController {
	@Autowired
	private AsyncTask asyncTask;

	@RequestMapping("async_task")
	public String exeTask() throws InterruptedException {
		long begin = System.currentTimeMillis();
		Future<String> task1 = asyncTask.task1();
		Future<String> task2 = asyncTask.task2();
		Future<String> task3 = asyncTask.task3();
		for (;;) {
			if (task1.isDone() && task2.isDone() && task3.isDone()) {
				break;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("执行总耗时=" + (end - begin));
		return "success";
	}
}

运行结果:

任务1耗时=1000
任务2耗时=2001
任务3耗时=3001
执行总耗时=3001

PS:需要异步执行的方法,不能和调用它的方法在一个类中,否则异步不生效。

猜你喜欢

转载自blog.csdn.net/Kevin_Gu6/article/details/88399131