spring 上下文异步事件驱动模型

倖福魔咒の 提交于 2020-08-05 11:22:25


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方法,触发事件;
 
            
            

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!