
278
2
부
이벤트 기반 아키텍처
13.6
실행 도중 핸들러가 제공된 메시지 버스
이제 메시지 버스는 더 이상 정적인 존재가 아니다.
DI
가 끝난 핸들러가 메시지 버스로 전달되
어야 한다. 따라서 모듈에서 설정 가능한 클래스로 메시지 버스를 변경해야 한다.
클래스로 정의한 메시지 버스(
src
/
allocation
/
service
_
layer
/
messagebus
.
py
)
class MessageBus: # ①
def __init__(
self,
uow: unit_of_work.AbstractUnitOfWork,
event_handlers: Dict[Type[events.Event], List[Callable]], # ②
command_handlers: Dict[Type[commands.Command], Callable], # ②
):
self.uow = uow
self.event_handlers = event_handlers
self.command_handlers = command_handlers
def handle(self, message: Message): # ③
self.queue = [message] # ④
while self.queue:
message = self.queue.pop(0)
if isinstance(message, events.Event): ...