Add transaction listener to delay sending the event right time.

This commit is contained in:
jyjang
2020-04-23 21:47:58 +09:00
parent 78db387eb0
commit c8273ddb84
2 changed files with 18 additions and 7 deletions

View File

@@ -19,12 +19,12 @@ public class PolicyHandler{
if(배달시작됨.isMe() && 배달시작됨.getOrderId()!=null){
try {
// 원래 데이터가 트랜잭션 커밋되기도 전에 이벤트가 너무 빨리 도달하는 경우를 막기 위함
Thread.currentThread().sleep(3000); // no good.
} catch (InterruptedException e) {
e.printStackTrace();
}
// try {
// // 원래 데이터가 트랜잭션 커밋되기도 전에 이벤트가 너무 빨리 도달하는 경우를 막기 위함
// Thread.currentThread().sleep(3000); // no good. --> pay 가 TX 를 마친 후에만 실행되도록 수정함
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println("배달시작됨 orderId = " + 배달시작됨.getOrderId());

View File

@@ -2,6 +2,9 @@ package fooddelivery;
import javax.persistence.*;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.util.List;
@Entity
@@ -26,7 +29,15 @@ public class 결제이력 {
}else{
결제승인됨 결제승인됨 = new 결제승인됨();
BeanUtils.copyProperties(this, 결제승인됨);
결제승인됨.publish();
//바로 이벤트를 보내버리면 주문정보가 커밋되기도 전에 배송발송됨 이벤트가 발송되어 주문테이블의 상태가 바뀌지 않을 수 있다.
// TX 리스너는 커밋이 완료된 후에 이벤트를 발생하도록 만들어준다.
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void beforeCommit(boolean readOnly) {
결제승인됨.publish();
}
});
try {