Activiti7将默认缓存替换为Redis

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

 


前言

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,大致流程如下:

  1. 自定义ActivitiDeploymentCache类并实现 DeploymentCache类,重写相关方法。
  2. 序列化ProcessDefinitionCacheEntry,并保存到redis。
  3. 注入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
评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值