文章目录
1、代码
抽象类
package com.sdecloud.efpx.pipeline.listener;
import com.sdecloud.efpx.pipeline.vo.JrmkVo;
/**
*
* * @className: Observer
* * @description:TODO
* * @param:
* * @return:
* * @throws:
* * @author: lizz
* * @date: 2020/06/09 15:06
*
*/
public abstract class Observer {
protected JrmkVo jrmkVo;
public abstract void update() throws Exception;
public abstract void del();
public abstract void insert();
}
观察者
package com.sdecloud.efpx.pipeline.vo;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.sdecloud.efpx.common.consts.BizConsts;
import com.sdecloud.efpx.pipeline.listener.Observer;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
*
* * @className: Jrmk
* * @description:TODO
* * @param:
* * @return:
* * @throws:
* * @author: lizz
* * @date: 2020/06/11 10:09
*
*/
@Data
@ApiModel(value = "实体类集合")
public class JrmkVo implements BizConsts {
private List<Observer> observers
= new ArrayList<Observer>();
private JrmkApplyVo jrmkApplyVo;
private JrmkAuditVo jrmkAuditVo;
private JrmkProductVo jrmkProductVo;
private JrmkWhitelistVo jrmkWhitelistVo;
public com.sdecloud.efpx.pipeline.vo.JrmkApplyVo getJrmkApplyVo() {
return jrmkApplyVo;
}
public com.sdecloud.efpx.pipeline.vo.JrmkAuditVo getJrmkAuditVo() {
return jrmkAuditVo;
}
public com.sdecloud.efpx.pipeline.vo.JrmkProductVo getJrmkProductVo() {
return jrmkProductVo;
}
public com.sdecloud.efpx.pipeline.vo.JrmkWhitelistVo getJrmkWhitelistVo() {
return jrmkWhitelistVo;
}
public void setJrmkApplyVo(JrmkApplyVo jrmkApplyVo,CanalEntry.EventType type) throws Exception {
this.jrmkApplyVo = jrmkApplyVo;
notifyAllObservers(type);
}
public void setJrmkAuditVo(JrmkAuditVo jrmkAuditVo,CanalEntry.EventType type) throws Exception {
this.jrmkAuditVo = jrmkAuditVo;
notifyAllObservers(type);
}
public void setJrmkProductVo(JrmkProductVo jrmkProductVo,CanalEntry.EventType type) throws Exception {
this.jrmkProductVo = jrmkProductVo;
notifyAllObservers(type);
}
public void setJrmkWhitelistVo(JrmkWhitelistVo jrmkWhitelistVo,CanalEntry.EventType type) throws Exception {
this.jrmkWhitelistVo = jrmkWhitelistVo;
notifyAllObservers(type);
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(CanalEntry.EventType type) throws Exception {
for (Observer observer : observers) {
if(type ==CanalEntry.EventType.UPDATE){
observer.update();
}else if(type ==CanalEntry.EventType.DELETE){
observer.del();
}else if(type ==CanalEntry.EventType.INSERT){
observer.insert();
}
}
}
}
操作
package com.sdecloud.efpx.pipeline.listener;
import com.sdecloud.efpx.common.elsticsearch.support.ElasticsearchTemplate;
import com.sdecloud.efpx.common.elsticsearch.support.ElasticsearchTemplateImpl;
import com.sdecloud.efpx.pipeline.spring.ComponentFactory;
import com.sdecloud.efpx.pipeline.vo.JrmkVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
*
* * @className: TableJrmkApplyObserver
* * @description:TODO
* * @param:
* * @return:
* * @throws:
* * @author: lizz
* * @date: 2020/06/09 15:07
*
*/
@Slf4j
//@Component
public class TableJrmkApplyObserver extends Observer{
// private static ElasticsearchTemplate elasticsearchTemplate = (ElasticsearchTemplate) ComponentFactory.getBean("elasticsearchTemplate");
public TableJrmkApplyObserver(com.sdecloud.efpx.pipeline.vo.JrmkVo jrmkVo){
this.jrmkVo = jrmkVo;
this.jrmkVo.attach(this);
}
@Override
public void update() throws Exception {
//ComponentFactory.getBean(ElasticsearchTemplate.class).save(jrmkVo.getJrmkApplyVo());
// elasticsearchTemplate.save(jrmkVo.getJrmkApplyVo());
//ElasticsearchTemplateImpl elasticsearchTemplateImpl = (ElasticsearchTemplateImpl)ServiceLocator.getService("elasticsearchTemplateImpl");
//elasticsearchTemplateImpl.update(jrmkVo.getJrmkApplyVo());
log.info("TableJrmkApplyObserver更新");
}
@Override
public void del() {
log.info("TableJrmkApplyObserver删除 ");
}
@Override
public void insert() {
log.info("TableJrmkApplyObserver新增 ");
}
}