[Java/Spring] 비동기(Async)

Hyeri Park·2022년 8월 4일
0

JAVA/Spring 기초

목록 보기
16/22
post-thumbnail

AsyncApplication.java

package com.example.async;

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

@SpringBootApplication
@EnableAsync
public class AsyncApplication {

	public static void main(String[] args) {
		SpringApplication.run(AsyncApplication.class, args);
	}

}

1. 예제 (1)

ApiController.java

package com.example.async.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.async.service.AsyncService;



@RestController
@RequestMapping("/api")
public class ApiController {
	
	
	private AsyncService asyncService;
	
	
	// 자동 생성자 
	public ApiController(AsyncService asyncService) {
		this.asyncService = asyncService;
	}


	@GetMapping("hello")
	public String hello() {
		asyncService.hello();
		System.out.println("method end");
		
		return "hello";
	}
}

AsyncService.java

package com.example.async.service;

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



@Service
public class AsyncService {
	
	//비동기동작
	@Async
	public void hello() {
		
		for(int i = 0; i < 10; i++) {
			try {
				Thread.sleep(2000);
				System.out.println("thread sleep~!");
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		
		
	}
 }




2. 예제 (2) - Lombok 사용


build.gradle 에 아래 내용 추가

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}


ApiController.java

package com.example.async.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.async.service.AsyncService;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/api")
public class ApiController {
	
	
	private AsyncService asyncService;
	
	
	// 자동 생성자 
	public ApiController(AsyncService asyncService) {
		this.asyncService = asyncService;
	}


	@GetMapping("hello")
	public String hello() {
		asyncService.hello();
		//System.out.println("method end");
		log.info("method end");
		return "hello";
	}
}

AsyncService.java

package com.example.async.service;

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

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class AsyncService {
	
	//비동기동작
	@Async
	public void hello() {
		
		for(int i = 0; i < 10; i++) {
			try {
				Thread.sleep(2000);
				//System.out.println("thread sleep~!");
				log.info("thread sleep");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		
		
	}

}

AsyncService.java

package com.example.async.service;

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

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class AsyncService {
	
	
	
	//비동기동작
	@Async("aync-thread")
	public void hello() {
		
		for(int i = 0; i < 10; i++) {
			try {
				Thread.sleep(2000);
				//System.out.println("thread sleep~!");
				log.info("thread sleep");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		
		
	}

}

AppConfig.java

package com.example.async.config;

import java.util.concurrent.Executor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class AppConfig {
	@Bean("aync-thread")
	public Executor asyncThread() {
		ThreadPoolTaskExecutor threadPoolTaskExcutor = new ThreadPoolTaskExecutor();
		threadPoolTaskExcutor.setMaxPoolSize(100);
		threadPoolTaskExcutor.setCorePoolSize(10);
		threadPoolTaskExcutor.setQueueCapacity(10);
		threadPoolTaskExcutor.setThreadNamePrefix("Async-");
		return threadPoolTaskExcutor;
	}
}

profile
Backend Developer

0개의 댓글