0512 spring AOP 중 발생 오류(스케쥴러 동작) java.lang.IllegalStateException: No thread-bound request found:
오류내용:
java.lang.IllegalStateException: No thread bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
try {
//HttpServletRequest 얻어오기
//단, 스프링 스케줄러 동작 시 예외 발생(스케줄러는 요청객체가 존재하지 않는다)
HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
Member loginMember = (Member)req.getSession().getAttribute("loginMember");
str += "ip : " + getRemoteAddr(req);
if(loginMember != null) {
str += "(email : " + loginMember.getMemberEmail() + " ) ";
}
} catch (Exception e) {
str += "스케줄러 동작";
e.printStackTrice();
}
logger.info(str);
}
원인:
java.lang.IllegalStateException에러가 발생하면 여러가지 이유가 있겠지만
1.해당 Controller.java 파일의 Mapping에 문제가 발생한 것일 수 있다
2. 나같은 경우에는 1번의 문제는 아니고 위 구문에 HttpServletRequest와 스케쥴러 관련된 문제였다
catch (Exception e) {
str += "스케줄러 동작";
e.printStackTrice();
에서 e.printStackTrice(); 적어줘서 표시되는 오류다.
HttpServletRequest 얻어오면
스프링 스케줄러 동작 시 예외 발생시킨다
(스케줄러는 요청객체가 존재하지 않는다)
결론:
스케쥴러 동작이 있어서 예외를 발생시킨것이니 문제될거 없다
e.printStackTrice(); 지워주면 문제될것도 없고 에러도 뜨지 않는다