Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

하는 데 의의를 둬春

타이머로 5분 뒤 이메일 전송 구현하기 (ScheduledExecutorService) 본문

Java

타이머로 5분 뒤 이메일 전송 구현하기 (ScheduledExecutorService)

Shonir00ng 2024. 2. 7. 10:33

 

@Controller
@Slf4j
public class EmailController {

	@Autowired
	EmailService emailService;

	@RequestMapping("/emp/sendEmail")
	@ResponseBody
	public Map<String, Object> sendEmail(@RequestParam(value = "empNo") String empNo
		, @RequestParam(value = "contents") String contents) {	
		
		EmployeeInfo empInfo = new EmployeeInfo();
		Map<String, Object> resMap = new HashMap<String, Object>();
		String html = "";
		
		try {

			final EmployeeInfo finalEmpInfo = empInfo;
			final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
			scheduler.schedule(() -> empService.sendAutoReplyEmail(finalEmpInfo), 5, TimeUnit.MINUTES);
			scheduler.shutdown();
            
			resMap.put("result", true);
				
			} catch (Exception e) {			
				resMap.put("result", false);
				resMap.put("message", "예외 발생");
			}		
		
		return resMap;
	}
	
}

 

비동기로 실행해야 해서 찾다가 발견했다.

내부 함수에서 값이 바뀌면 안되므로 변수 선언을 final로 해주어야 에러가 나지 않는다.

 

Comments