栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Activiti源码——持久化模型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Activiti源码——持久化模型

一 概述     

        Activiti持久层是基于MyBatis框架定制了一套自己的使用方式, 处理逻辑的几个核心组件有:

  •  Entity:  数据对象需实现的接口
  •  EntityManager:  获取DataManager 和 处理事件的发布
  •  DataManager:  操作数据库的入口
  • Session:  获取sqlSteatment 缓存或执行sql的接口
二 源码详解      1  持久层对象的封装

        在activiti中持久层所有的操作模型都需要实现org.activiti.engine.impl.persistence.entity.Entity接口

public interface Entity {

  String getId();

  void setId(String id);
  
  boolean isInserted();
  
  void setInserted(boolean inserted);
  
  boolean isUpdated();
  
  void setUpdated(boolean updated);
  
  boolean isDeleted();
  
  void setDeleted(boolean deleted);
  
  
  Object getPersistentState();
}
      2   事件分发管理

  持久层对象统一交给org.activiti.engine.impl.persistence.entity.EntityManager实现类 来管理,比如 一个重要的抽象类org.activiti.engine.impl.persistence.entity.AbstractEntityManager

public abstract class AbstractEntityManager extends AbstractManager implements EntityManager {
    public AbstractEntityManager(ProcessEngineConfigurationImpl processEngineConfiguration)     {
        super(processEngineConfiguration);
  }

 @Override
  public void insert(EntityImpl entity) {
    insert(entity, true);
  }

      @Override
  public void insert(EntityImpl entity, boolean fireCreateEvent) {
    getDataManager().insert(entity);

    ActivitiEventDispatcher eventDispatcher = getEventDispatcher();
    if (fireCreateEvent && eventDispatcher.isEnabled()) {
      eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, entity));
      eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_INITIALIZED, entity));
    }
  }
}
      3  数据操作封装            

org.activiti.engine.impl.persistence.entity.data.DataManager接口的实现类是操作数据库的入口

public abstract class AbstractDataManager extends AbstractManager implements DataManager {
    @Override
  public void insert(EntityImpl entity) {
    getDbSqlSession().insert(entity);
  }
  
  public EntityImpl update(EntityImpl entity) {
    getDbSqlSession().update(entity);
    return entity;
  }
  
  @Override
  public void delete(String id) {
    EntityImpl entity = findById(id);
    delete(entity);
  }
  
  @Override
  public void delete(EntityImpl entity) {
    getDbSqlSession().delete(entity);
  }

//...
}

        activiti的sqlSession是由SessionFactory创建的, 引擎内部持有各类SessionFactory实现,用户也能够自定义注册本身的SessionFactory实现。

package org.activiti.engine.impl.interceptor;


public interface SessionFactory {

  Class getSessionType();

  Session openSession(CommandContext commandContext);

}
       4  集中提交

                dbSqlSession的实现片段

public class DbSqlSession implements Session {
  protected SqlSession sqlSession;
  protected DbSqlSessionFactory dbSqlSessionFactory;
  protected EntityCache entityCache;
  
  protected Map, Map> insertedObjects 
    = new HashMap, Map>();//换存插入对象
  protected Map, Map> deletedObjects 
    = new HashMap, Map>();//缓存删除对像
  protected Map, List> bulkDeleteOperations
    = new HashMap, List>();
  protected List updatedObjects = new ArrayList();//缓存更新对象

public void insert(Entity entity) {
    if (entity.getId() == null) {//实体是否有id, 没有则生成一个
      String id = dbSqlSessionFactory.getIdGenerator().getNextId();
      entity.setId(id);
    }
    
    Class clazz = entity.getClass();
    if (!insertedObjects.containsKey(clazz)) {//判断该类型对象是否存在,不存在则缓存新增
    	insertedObjects.put(clazz, new LinkedHashMap()); // order of insert is important, hence LinkedHashMap
    }
    //将对像放入缓存map,
    insertedObjects.get(clazz).put(entity.getId(), entity);
    entityCache.put(entity, false); // False -> entity is inserted, so always changed
    entity.setInserted(true);
  }

  public void flush() {
    determineUpdatedObjects(); // Needs to be done before the removeUnnecessaryOperations, as removeUnnecessaryOperations will remove stuff from the cache
    removeUnnecessaryOperations();

    if (log.isDebugEnabled()) {
      debugFlush();
    }

    flushInserts();
    flushUpdates();
    flushDeletes();
  }

}

上面的代码涉及到了集中提交, 增删改查并没有直接操作数据库, 而是将对象缓存在sqlSession中, sqlSession又是从当前线程的CommandContext中获取的,当一个命令执行完毕后,最终命令上下文CommandContext的close方法会被调用。当执行CommandContext.close()方法时,其内部会按照顺序执行flushSessions,closeSessions方法。

        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/986047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号