事件处理器采用观察者模式,监听聚合根上发布事件的,并执行相应逻辑。借助事件处理器可以实现:
可以通过在方法上添加@EventHandler来创建事件处理器。方法的入参有且仅有一个,是所需处理的事件类型(也可以是集合)。
@Component
@Slf4j
public class AccountEventHandler {
@Autowired
private AccountRepository accountRepository;
@EventHandler(asynchronous = true, postAfterTransaction = true, isTransactionMessage = true,
asyncConfig = @AsyncConfig(queueFullPolicy = QueueFullPolicy.DISCARD),
transactionCheck = @TransactionCheck(checkTransactionStatusMethod = "checkAccountCreateEvent"))
@Transactional(rollbackFor = Throwable.class)
public void handleAccountCreateEvent(AccountCreateEvent accountCreateEvent) {
log.info("exec handleAccountCreateEvent,{}", accountCreateEvent);
//业务逻辑
}
@EventHandler(asynchronous = true, postAfterTransaction = true, isTransactionMessage = true,
asyncConfig = @AsyncConfig(queueFullPolicy = QueueFullPolicy.DISCARD),
transactionCheck = @TransactionCheck(checkTransactionStatusMethod = "batchCheckAccountCreateEvent"))
@Transactional(rollbackFor = Throwable.class)
public void batchHandleAccountCreateEvent(List<AccountCreateEvent> accountCreateEvents) {
log.info("exec batchHandleAccountCreateEvent,{}", accountCreateEvents);
//业务逻辑
}
public boolean checkAccountCreateEvent(AccountCreateEvent accountCreateEvent) {
return accountRepository.findByAccountId(accountCreateEvent.getAccountId()) != null;
}
public boolean batchCheckAccountCreateEvent(List<AccountCreateEvent> accountCreateEvents) {
return !accountRepository.findByAccountIds(accountCreateEvents.stream().map(AccountCreateEvent::getAccountId).collect(Collectors.toList())).isEmpty();
}
}