1、基于Spring-Event增加事件处理机制
import org.bson.Document; import org.springframework.context.ApplicationEvent;/*** 基于Spring-Event增加事件处理机制* @create: 2024/4/1-13:33*/ public class SysProductConfigEvent extends ApplicationEvent {// 数据配置private Document configDoc;public SysProductConfigEvent(Document source) {super(source);this.configDoc = source;}public Document getConfigDoc() {return configDoc;}public void setConfigDoc(Document configDoc) {this.configDoc = configDoc;} }
2、定时任务
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.HashMultiset; import com.google.common.collect.Lists; import com.google.common.collect.Multiset; import com.mongodb.client.model.Filters; import com.mongodb.client.model.UpdateOneModel; import com.skybird.iot.addons.oemSystem.backend.jdo.Procedure; import com.skybird.iot.addons.sysConfig.product.backend.event.SysProductConfigEvent; import com.skybird.iot.addons.sysConfig.product.backend.jdo.QProductConfig; import com.skybird.iot.addons.sysConfig.product.backend.listener.SysProductConfigListener; import com.skybird.iot.base.utils.DBUtils; import com.skybird.iot.base.utils.DocuLib; import com.skybird.iot.base.utils.StringLib; import org.apache.commons.collections4.CollectionUtils; import org.bson.Document; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.stream.Collectors;/*** 产品调度器*/ @Component public class SysProductConfigScheduler implements ApplicationListener<SysProductConfigEvent> {// Logger打印日志对象private static final Logger LOGGER = LoggerFactory.getLogger(SysProductConfigListener.class.getName());@Override public void onApplicationEvent(SysProductConfigEvent event) {Document configDoc = event.getConfigDoc();taskExecute();LOGGER.info("configDoc:{}", configDoc);}/* 定时任务 */@Scheduled(fixedRate = 500000L)public void taskExecute(){// 数据同步datasync();}private synchronized void datasync() {System.out.println("数据同步成功");}
3、数据保存接口触发案例
@PostMapping("/save") public CheckMessage save(@RequestParam("data") String data) {Document entity = DocuLib.parseDecode(data);String id = DocuLib.getID(entity);Document filter = DocuLib.newDoc();filter.put("code", DocuLib.getCode(entity));if (StrUtil.isNotEmpty(id)) {filter.put("id", DocuLib.newDoc("$ne", id));}long count = DBUtils.count(QProductConfig.collectionName, filter);if (count > 0) {return CheckMessage.newInstance().setSuccess(false).addMessage("产品编码已存在");}if (StrUtil.isEmpty(id)) {entity.put("create_time", DateUtil.date());DBUtils.insert(QProductConfig.collectionName, entity);} else {Document query = DocuLib.newDoc("id", id);DBUtils.updateOne(QProductConfig.collectionName, query, DocuLib.new$Set(entity));}// 增加事件推送,推进后直接触发定时任务进行数据同步SpringUtil.publishEvent(new SysProductConfigEvent(new Document()));return CheckMessage.newInstance(); }