深入探索Grails消息传递:从轻量级到企业级
1. 轻量级消息传递与Platform Core
Platform Core提供了轻量级的消息传递功能,允许开发者监听特定事件。例如,我们可以监听GORM的onSaveOrUpdate事件,并记录账户的所有更改:
@grails.events.Listener(namespace = 'gorm', topic = 'onSaveOrUpdate') void logAllAccountChanges(User user) { log.info "Changes made to account- ${user.name} by ${springSecurityService.currentUser}" }此外,还可以使用通配符进行更激进的监听。比如,监听所有GORM事件:
@Listener(topic = '*', namespace = 'gorm') void beforeEachGormEvent(EventMessage message) { log.info "gorm event $message.event on domain $message.data.class" }若只想监听特定事件,可以收紧通配符,如topic="before*"仅监听以before开头的事件。
在集成Sp