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