ThreadLocal을 사용하여 메세지 기반의 이벤트 처리를 하고 있었습니다.
Command가 성공하면 ThreadLocal에 Event가 등록되고
Aop를 사용하여 Transaction이 성공될때만 ThreadLocal에 있는 Event를 가져와 처리하는 로직입니다.
하지만 예상치 못한 에러를 발견하였습니다.
public class Events {
private static ThreadLocal<List<Event>> eventThreadLocal = new ThreadLocal<>();
private Events() {
throw new IllegalStateException("Do not Initialize.");
}
public static <T> void register(T event) {
if (event == null) {
return;
}
eventThreadLocal.get().add((Event) event);
}
public static List<Event> getEvents() {
return eventThreadLocal.get();
}
public static void clear() {
eventThreadLocal.get().clear();
}
}
Exception in thread "getDomainEventTaskExecutor-3" java.lang.NullPointerException: Cannot invoke "java.util.List.add(Object)" because the return value of "java.lang.ThreadLocal.get()" is null
말 그대로 ThreadLocal에 값이 없다는것인데요...
분명 저장까지 했지만 에러가 발생했던 이유는 아래와 같습니다.
ArrayList를 초기화 하지 않았기 때문입니다.
private static ThreadLocal<List<Event>> eventThreadLocal = ThreadLocal.withInitial(ArrayList::new);
'Project > 개인프로젝트' 카테고리의 다른 글
[Error] Multi Module 적용 에러 (0) | 2023.10.19 |
---|---|
Spring Integration 적용하기 (0) | 2023.10.07 |
ThreadLocal적용하여 Thread 안에서의 값을 공유하기 (0) | 2023.10.07 |
[Spring Integration] Direct Channel과 Executor Channel 차이 (0) | 2023.10.07 |
TransactionSynchronizationManager를 사용해 Transaction 이후에 Event 등록하기 (0) | 2023.10.06 |
ThreadLocal을 사용하여 메세지 기반의 이벤트 처리를 하고 있었습니다.
Command가 성공하면 ThreadLocal에 Event가 등록되고
Aop를 사용하여 Transaction이 성공될때만 ThreadLocal에 있는 Event를 가져와 처리하는 로직입니다.
하지만 예상치 못한 에러를 발견하였습니다.
public class Events {
private static ThreadLocal<List<Event>> eventThreadLocal = new ThreadLocal<>();
private Events() {
throw new IllegalStateException("Do not Initialize.");
}
public static <T> void register(T event) {
if (event == null) {
return;
}
eventThreadLocal.get().add((Event) event);
}
public static List<Event> getEvents() {
return eventThreadLocal.get();
}
public static void clear() {
eventThreadLocal.get().clear();
}
}
Exception in thread "getDomainEventTaskExecutor-3" java.lang.NullPointerException: Cannot invoke "java.util.List.add(Object)" because the return value of "java.lang.ThreadLocal.get()" is null
말 그대로 ThreadLocal에 값이 없다는것인데요...
분명 저장까지 했지만 에러가 발생했던 이유는 아래와 같습니다.
ArrayList를 초기화 하지 않았기 때문입니다.
private static ThreadLocal<List<Event>> eventThreadLocal = ThreadLocal.withInitial(ArrayList::new);
'Project > 개인프로젝트' 카테고리의 다른 글
[Error] Multi Module 적용 에러 (0) | 2023.10.19 |
---|---|
Spring Integration 적용하기 (0) | 2023.10.07 |
ThreadLocal적용하여 Thread 안에서의 값을 공유하기 (0) | 2023.10.07 |
[Spring Integration] Direct Channel과 Executor Channel 차이 (0) | 2023.10.07 |
TransactionSynchronizationManager를 사용해 Transaction 이후에 Event 등록하기 (0) | 2023.10.06 |