前言
Activiti7默认将流程缓存到内存里,默认实现为DefaultDeploymentCache,如下
public class DefaultDeploymentCache<T> implements DeploymentCache<T> {
protected Map<String, T> cache;
/** Cache with no limit */
public DefaultDeploymentCache() {
this.cache = synchronizedMap(new HashMap<String, T>());
}
/**
* Cache which has a hard limit: no more elements will be cached than the limit.
*/
public DefaultDeploymentCache(final int limit) {
this.cache = synchronizedMap(new LinkedHashMap<String, T>(limit + 1, 0.75f, true) { // +1 is needed, because the entry is inserted first, before it is removed
// 0.75 is the default (see javadocs)
// true will keep the 'access-order', which is needed to have a real LRU cache
private static final long serialVersionUID = 1L;
protected boolean removeEldestEntry(Map.Entry<String, T> eldest) {
boolean removeEldest = size() > limit;
if (removeEldest && logger.isTraceEnabled()) {
logger.trace("Cache limit is reached, {} will be evicted", eldest.getKey());
}
return removeEldest;
}
});
}
}
可以看到该缓存用HashMap存储在内存中,且无法设置超时等属性,导致内存越来越大,最终溢出,现将缓存替换为Redis。
一、描述
本案例采用Activiti7+Jedis,大致流程如下:
- 自定义ActivitiDeploymentCache类并实现 DeploymentCache类,重写相关方法。
- 序列化ProcessDefinitionCacheEntry,并保存到redis。
- 注入ActivitiDeploymentCache。
二、操作步骤
1.自定义ActivitiDeploymentCache
@Slf4j
public class ActivitiDeploymentCache implements DeploymentCache<ProcessDefinitionCacheEntry> {
public static final String DEFAULT_ACTIVITI_DEF_CACHE_BEAN_NAME = "activitiDeploymentCache";
private static final String PROCESS_DEFINITION_NAME = "processDefinition";
private static final String BPMN_MODEL_NAME = "BpmnModel";
@Autowired
private JedisHelper jedisHelper;
@Override
public ProcessDefinitionCacheEntry get(String id) {
log.info("ActivitiCacheGet --- id:" + id);
ProcessDefinitionCacheEntry entry = getProcessDefinitionCacheEntry(
jedisHelper.get(getProcessDefinitionId(id).getBytes()), jedisHelper.get(getBpmnId(id).getBytes()));
return entry;
}
@Override
public boolean contains(String id) {
return jedisHelper.hasKey(getProcessDefinitionId(id).getBytes())
&& jedisHelper.hasKey(getBpmnId(id).getBytes());
}
@SneakyThrows
@Override
public void add(String id, ProcessDefinitionCacheEntry entry) {
log.info("ActivitiCacheAdd --- id:" + id);
/**
* // activiti自带Converter,缺失event等属性
*
* BpmnJsonConverter util = new BpmnJsonConverter(); ObjectNode node =
* util.convertToJson(object.getBpmnModel()); CustomBp

本文介绍如何将Activiti7的流程定义缓存从内存迁移到Redis,包括自定义缓存类、序列化处理及配置注入等关键步骤。
最低0.47元/天 解锁文章
1456





