一、编写事件
1.编写事件类并集成spring boot 事件接口,提供访问事件参数属性
public class PeriodicityRuleChangeEvent extends ApplicationEvent {private final JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO;public PeriodicityRuleChangeEvent(Object source, JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {super(source);this.jwpDeployWorkOrderRuleDTO = jwpDeployWorkOrderRuleDTO;}public JwpDeployWorkOrderRuleDTO getJwpDeployWorkOrderRuleDTO() {return jwpDeployWorkOrderRuleDTO;}}
二、编写监听类(必须写明监听事件类型,重写监听到事件后,处理方法)
@Component
public class PeriodicityRuleListener implements ApplicationListener<PeriodicityRuleChangeEvent> {@Autowiredprivate PeriodicityCreateProcessServiceImpl periodicityCreateProcessServiceImpl;@Overridepublic void onApplicationEvent(PeriodicityRuleChangeEvent periodicityRuleChangeEvent) {periodicityCreateProcessServiceImpl.addTask(periodicityRuleChangeEvent.getJwpDeployWorkOrderRuleDTO());}}
三、发布事件
@compnent
public class PeriodicityStartProcessService {@Autowiredprivate ApplicationEventPublisher publisher;private void triggerEvent(JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {PeriodicityRuleChangeEvent periodicityRuleChangeEvent = new PeriodicityRuleChangeEvent(this, jwpDeployWorkOrderRuleDTO);publisher.publishEvent(periodicityRuleChangeEvent);}}