1Khi khởi động, Spring đăng ký FraudCheckListener (@EventListener) và OrderEmailListener (@TransactionalEventListener AFTER_COMMIT) theo kiểu tham số OrderPlacedEvent, nên bộ điều phối biết nơi nhận.At startup, Spring registers FraudCheckListener (@EventListener) and OrderEmailListener (@TransactionalEventListener AFTER_COMMIT) by their OrderPlacedEvent parameter type, so the dispatcher knows the recipients.
2OrderService.place(cmd) chạy trong @Transactional: orders.save lưu đơn 1024 rồi tạo new OrderPlacedEvent(order.id(), order.customerId(), order.total()). Transaction chưa commit.OrderService.place(cmd) runs inside @Transactional: orders.save stores order 1024, then it creates new OrderPlacedEvent(order.id(), order.customerId(), order.total()). The transaction has not committed yet.
3Service gọi events.publishEvent(...) đúng một lần, không gọi từng tính năng phụ. Bộ điều phối chọn listener theo kiểu sự kiện, không theo tên method.The service calls events.publishEvent(...) exactly once instead of calling each side feature. The dispatcher selects listeners by event type, not by method name.
4FraudCheckListener là @EventListener thường: chạy đồng bộ, cùng thread và cùng transaction. total = 1.250.000 > 0 nên không ném lỗi và place() đi tiếp.FraudCheckListener is a plain @EventListener: synchronous, same thread and same transaction. total = 1,250,000 > 0, so it throws nothing and place() continues.
5Transaction commit, lúc đó OrderEmailListener mới được gọi; nhờ @Async nó chạy trên executor khác và gọi email.send(e.customerId(), ...), nên luồng đặt hàng không chờ máy chủ mail.The transaction commits and only then is OrderEmailListener invoked; thanks to @Async it runs on another executor thread and calls email.send(e.customerId(), ...), so the order thread never waits on the mail server.
6Nhánh khác: nếu total <= 0, FraudCheckListener ném IllegalStateException("Invalid total"), lỗi lan ra khỏi place() và transaction rollback. Không có commit nên listener AFTER_COMMIT không chạy — không có email xác nhận cho đơn bị hủy.Alternative branch: if total <= 0, FraudCheckListener throws IllegalStateException("Invalid total"), which escapes place() and rolls the transaction back. With no commit, the AFTER_COMMIT listener never runs — no confirmation email for a rejected order.
7Bản Go: khi khởi tạo, placed.Subscribe(EmailObserver(ctx, send)). EmailObserver tạo channel đệm 100, khởi chạy goroutine gửi mail và trả về một Listener[OrderPlaced] là closure.In Go: at wiring time, placed.Subscribe(EmailObserver(ctx, send)). EmailObserver creates a 100-slot buffered channel, starts a mailer goroutine and returns a Listener[OrderPlaced] closure.
8Service.Place gọi s.repo.Save rồi s.placed.Publish(ctx, OrderPlaced{...}). Topic sao chép danh sách listener dưới RLock, nhả khóa rồi gọi từng l(ctx, event) theo thứ tự đăng ký.Service.Place calls s.repo.Save, then s.placed.Publish(ctx, OrderPlaced{...}). Topic clones the listener slice under RLock, releases the lock, then calls each l(ctx, event) in subscription order.
9Closure của EmailObserver chỉ đẩy sự kiện vào ch (không chặn); goroutine nền gọi send(e). Gửi lỗi thì slog.Error, hàng đợi đầy thì slog.Warn và bỏ sự kiện. Thêm observer cộng điểm sau này chỉ cần một Subscribe mới, không sửa Service.EmailObserver's closure only pushes the event onto ch (non-blocking); the background goroutine calls send(e). A send error logs slog.Error, a full queue logs slog.Warn and drops the event. A loyalty observer later needs just one more Subscribe, with no change to Service.