하는 데 의의를 둬春
타이머로 5분 뒤 이메일 전송 구현하기 (ScheduledExecutorService) 본문
@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로 해주어야 에러가 나지 않는다.
'Java' 카테고리의 다른 글
LazyInitializationException @Transactional도 먹히지 않을 때 (0) | 2024.02.08 |
---|---|
Dropzone을 이용한 파일 업로드 및 AWS S3 스토리지 저장 (작성중) (1) | 2023.02.20 |
[Java] Map을 VO로, VO를 Map으로 변환하기 (0) | 2023.01.17 |
[Java] Maven profile로 실행환경 분리하기 (0) | 2022.07.14 |
[Eclipse] File Search에서 열었던 파일 고정하기 (0) | 2022.02.10 |
Comments