
183
8
장
이벤트와 메시지 버스
8.3.4
메시지 버스는 이벤트를 핸들러에 매핑한다
메시지 버스는 기본적으로 “이 이벤트가 발생하면 다음 핸들러 함수를 호출해야 한다”라고 말
한다. 다른 말로, 메시지 버스는 간단한 발행/구독 시스템
publish
-
subscribe
system
이다. 핸들러는 수
신된 이벤트를
구독
subscribed
한다. 수신되는 이벤트는 버스에 시스템이 발행한 것이다. 글로 쓰
면 실제보다 더 어려워 보이지만 보통은 사전을 활용해 메시지 버스를 구현한다.
간단한 메시지 버스(
src
/
allocation
/
service
_
layer
/
messagebus
.
py
)
def handle(event: events.Event):
for handler in HANDLERS[type(event)]:
handler(event)
def send_out_of_stock_notification(event: events.OutOfStock):
email.send_mail(
‘stock@made.com’,
f’Out of stock for {event.sku}’,
)
HANDLERS = {
events.OutOfStock: [send_out_of_stock_notification],
} # type: Dict[Type[events.Event], List[Callable]] ...