概述
一 概述
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);
/**
* Returns a representation of the object, as would be stored in the database.
* Used when deciding if updates have occurred to the object or not since it was last loaded.
*/
Object getPersistentState();
}
2 事件分发管理
持久层对象统一交给org.activiti.engine.impl.persistence.entity.EntityManager实现类 来管理,比如 一个重要的抽象类org.activiti.engine.impl.persistence.entity.AbstractEntityManager
public abstract class AbstractEntityManager<EntityImpl extends Entity> extends AbstractManager implements EntityManager<EntityImpl> {
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<EntityImpl extends Entity> extends AbstractManager implements DataManager<EntityImpl> {
@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<Class<? extends Entity>, Map<String, Entity>> insertedObjects
= new HashMap<Class<? extends Entity>, Map<String, Entity>>();//换存插入对象
protected Map<Class<? extends Entity>, Map<String, Entity>> deletedObjects
= new HashMap<Class<? extends Entity>, Map<String, Entity>>();//缓存删除对像
protected Map<Class<? extends Entity>, List<BulkDeleteOperation>> bulkDeleteOperations
= new HashMap<Class<? extends Entity>, List<BulkDeleteOperation>>();
protected List<Entity> updatedObjects = new ArrayList<Entity>();//缓存更新对象
public void insert(Entity entity) {
if (entity.getId() == null) {//实体是否有id, 没有则生成一个
String id = dbSqlSessionFactory.getIdGenerator().getNextId();
entity.setId(id);
}
Class<? extends Entity> clazz = entity.getClass();
if (!insertedObjects.containsKey(clazz)) {//判断该类型对象是否存在,不存在则缓存新增
insertedObjects.put(clazz, new LinkedHashMap<String, Entity>()); // 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
方法。
最后
以上就是大气台灯为你收集整理的Activiti源码——持久化模型的全部内容,希望文章能够帮你解决Activiti源码——持久化模型所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复