1:定义一个事件对象继承ApplicationEvent抽象类
public class TableChangeEvent extends ApplicationEvent {
private Long Id;
private String tableName;
private Integer opType;
private String createTime;
private Long orgId;
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public TableChangeEvent(Object source) {
super(source);
}
public Long getOrgId() {
return orgId;
}
public void setOrgId(Long orgId) {
this.orgId = orgId;
}
public Integer getOpType() {
return opType;
}
public void setOpType(Integer opType) {
this.opType = opType;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
2.定义事件监听处理器
@Component
public class TableChangeEventHandler {
private Logger logger = LoggerFactory.getLogger(TableChangeEventHandler.class);
private static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),new ThreadException());
@Autowired
private MedicalService medicalService;
@Autowired
private DeviceService deviceService;
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT,fallbackExecution = true)
public void listenerTableDataChange(TableChangeEvent event){
Thread t=new Thread(new Runnable() {
@Override
public void run() {
TableEvent tableEvent=new TableEvent();
tableEvent.setOpType(event.getOpType());
tableEvent.setTableId(event.getId().toString());
tableEvent.setTableName(event.getTableName());
tableEvent.setCreateTime(DateUtils.string2Date(event.getCreateTime(),DateUtils.DATE_TIME_PATTERN));
medicalService.updateUpdateTimeByCondtion(tableEvent);
Long orgId = event.getOrgId();
Long endOrgId = OrgUtil.getMaxLeafTheoreticalValue(orgId);
LLwExample lLwExample=new LLwExample();
lLwExample.andGreaterThanOrEqualTo("orgId",orgId);
lLwExample.andLessThanOrEqualTo("orgId",endOrgId);
List<DeviceDto> deviceDtos = deviceService.selectByExample(lLwExample);
List<String> deviceCodes=new ArrayList<>();
deviceDtos.forEach(e->{
deviceCodes.add(e.getDeviceCode());
});
if(CommonUtils.isNotNullList(deviceCodes)){
Map<String,Object> map=new HashMap<>();
map.put(CmdTypeConstants.updata.getCode(),CmdTypeConstants.updata.getName());
NettyMsgUtils.sendMsg(map,deviceCodes, CmdTypeConstants.updata.getCode());
}
}
});
executorService.execute(t);
}
}
@TransactionalEventListener注解:事务监听注解
参考:https://blog.csdn.net/sinat_34341162/article/details/84226437
参考:https://blog.csdn.net/y_index/article/details/98505520
TransactionPhase.AFTER_COMMIT 事务提交后在进行操作
3.事件发送
TableChangeEvent tableChangeEvent = new TableChangeEvent(source);
// String redisTopic = PropertiesUtils.getString(ParamUtil.SYS_PARAM_REDIS_TOPIC);
tableChangeEvent.setId(id);
tableChangeEvent.setTableName(tableName);
tableChangeEvent.setOpType(optType);
tableChangeEvent.setCreateTime(DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));
LoginUser user = LoginUtil.getCurrentUser();
if(user!=null){
tableChangeEvent.setOrgId(user.getOrgId());
}
AppContextHolder.getContext().publishEvent(tableChangeEvent);
自定义spring的工具类获取spring的上下文,通过spring的上下文的publishEvent方法,触发事件;
来源:oschina
链接:https://my.oschina.net/u/3971821/blog/4470381