SpringBoot 비동기 작업 사용 방법 상세 설명

절차,그림 참조:

1.비동기 작업 업무 클래스 추가

package top.ytheng.demo.task;

import java.util.concurrent.Future;

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

//       
@Component
//        ,        
//  ,            
@Async
public class AsyncTask {

  public void task1() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(1000);
    long end = System.currentTimeMillis();
    System.out.println("  1  :" + (end - begin));
  }
  
  public void task2() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    System.out.println("  2  :" + (end - begin));
  }
  
  public void task3() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(3000);
    long end = System.currentTimeMillis();
    System.out.println("  3  :" + (end - begin));
  }
  
  //        
  public Future<String> task4() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(1000);
    long end = System.currentTimeMillis();
    System.out.println("  4  :" + (end - begin));
    return new AsyncResult<String>("  4");
  }
  
  public Future<String> task5() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    System.out.println("  5  :" + (end - begin));
    return new AsyncResult<String>("  5");
  }
  
  public Future<String> task6() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(3000);
    long end = System.currentTimeMillis();
    System.out.println("  6  :" + (end - begin));
    return new AsyncResult<String>("  6");
  }
}
2.테스트 컨트롤 러 추가

package top.ytheng.demo.controller;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

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 top.ytheng.demo.task.AsyncTask;

@RestController
@RequestMapping("api/v1/async")
public class TaskController {

  @Autowired
  private AsyncTask asyncTask;
  
  @GetMapping("/test")
  public Object test() throws InterruptedException, ExecutionException {
    long begin = System.currentTimeMillis();
    //asyncTask.task1();
    //asyncTask.task2();
    //asyncTask.task3();
    Future<String> result1 = asyncTask.task4();
    Future<String> result2 = asyncTask.task5();
    Future<String> result3 = asyncTask.task6();
    System.out.println("    :" + result1.get() + "," + result2.get() + "," + result3.get());
    for(;;) {
      if(result1.isDone() && result2.isDone() && result3.isDone()) {
        break;
      }
    }
    long end = System.currentTimeMillis();
    long total = end - begin;
    System.out.println("   :" + total);
    return "   :" + total;
  }
}
3.시작 클래스 추가

package top.ytheng.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication //    3 
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//     
@ServletComponentScan
//MyBatis  
@MapperScan("top.ytheng.demo.mapper")
//    (      )
@EnableScheduling
//      
@EnableAsync
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}
4.오른쪽 키 항목 Run As 시작,url 방문

http://localhost:8080/api/v1/async/test
결과:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기