今天有一个需求,定时器检测一个功能,然后调用监听器去执行任务,代码如下
// 监听任务
@Component
public class SendCourseAuditMessageEventListener implements ApplicationListener<SendCourseAuditMessageEvent> {
@Autowired
private WxApiService wxApiService;
@Override
public void onApplicationEvent(SendCourseAuditMessageEvent event) {
Map<String,String> map = (Map)event.getSource();
String courseId = map.get("courseId");
String url = map.get("url");
if(StrUtil.isNotEmpty(courseId)){
//发起微信消息推送
wxApiService.sendCourseAuditMessage(courseId,url);
}
}
}
public class SendCourseAuditMessageEvent extends ApplicationEvent {
public SendCourseAuditMessageEvent(Object source) {
super(source);
}
}
public class CourseServiceImpl{
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public R audit(){
// 发布监听任务
applicationEventPublisher.publishEvent(new SendCourseAuditMessageEvent(messageMap));
}
}
// 跑定时器
@Component
public class CourseScheduler {
@Autowired
private CourseService courseService;
@Scheduled(fixedRate = 1000 * 60 * 5)
private void CourseAudit() throws ParseException {
R r = courseService.courseAudit();
}
}