工作流引擎使用Redis

本文重点讲述工作流引擎中使用redis,本文的集成方式适合Activiti/Camunda/Flowable和盘古BPM

1、首先引入Redis包


<dependencies> 

           <groupId>com.pangubpmgroupId>

     <artifactId>bpm-model-cache<artifactId>
    <dependency>

2、流程引擎中配置Redis.

  processEngineConfiguration.setProcessDefinitionCache(getRedisDeploymentCache());

@Bean

public  RedisDeploymentCache getRedisDeploymentCache(){
    RedisDeploymentCache redisDeploymentCache=new RedisDeploymentCache();
    return redisDeploymentCache;
}
3、流程引擎中Redis的具体实现如下:
@Service
public class RedisDeploymentCache<ProcessDefinitionCacheEntry> implements DeploymentCache {
    public static final String PREFIX = "*";
    public static final String PREFIXKEY = "pangu_";
    @Autowired
    private RedisTemplate redisTemplate;
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDeploymentCache.class);
    @Override
    public Object get(String id) {
        Object obj=null;
        if (StringUtils.isNotEmpty(id)){
            obj = redisTemplate.opsForHash().get(PREFIXKEY+id,PREFIXKEY+ id);
        }
        return obj;
    }
    @Override
    public boolean contains(String id) {
        LOGGER.info("contains..........."+id);
        Object obj = redisTemplate.opsForHash().get(PREFIXKEY+ id,PREFIXKEY+ id);
        return obj==null;
    }
    @Override
    public boolean containsProcessInstance(String processInstanceId) {
        LOGGER.info("containsProcessInstance..........."+processInstanceId);
        return false;
    }
    @Override
    public void add(String id, Object object) {
        redisTemplate.opsForHash().put(PREFIXKEY+ id,PREFIXKEY+ id,object);
        LOGGER.info("add..........."+object);
    }
    @Override
    public void addProcessInstanceId(String id, Object object) {
        LOGGER.info("addProcessInstanceId..........."+id+",:"+object);

        redisTemplate.opsForHash().put(PREFIXKEY+ id,PREFIXKEY+ id,object);

    }
    @Override
    public void remove(String id) {
        LOGGER.info("remove..........."+id);
        this.removeById(PREFIXKEY+ id);
    }
    @Override
    public void removeProcessInstance(String processInstanceId) {
        LOGGER.info("containsProcessInstance..........."+processInstanceId);
        this.removeById(PREFIXKEY+ processInstanceId);

    }
    private void removeById(String id){
        redisTemplate.opsForHash().delete(PREFIXKEY+ id,PREFIXKEY+ id);
    }
    @Override
    public void clear() {
        LOGGER.info("clear...........");

    }
    @Override
    public void clearProcessInstance() {

    }
    @Override
    public Object get(String processDefinitionId, String processInstanceId) {
        LOGGER.info("get..........."+processDefinitionId+":"+processInstanceId);
        Object obj=null;
        obj = redisTemplate.opsForHash()
            .get(PREFIXKEY+ processInstanceId,PREFIXKEY+ processInstanceId);
        if (obj==null){
            obj =  redisTemplate.opsForHash()
                .get(PREFIXKEY+ processDefinitionId,PREFIXKEY+ processDefinitionId);
        }

        return obj;
    }
    @Override
    public Collection getAll() {
        LOGGER.info("getAll...........");
        Set keys = redisTemplate.keys(PREFIXKEY+ PREFIX);
        List result=new ArrayList<>(keys.size());
        for (String key : keys) {
            HashMap map = new HashMap<>();
            if(redisTemplate.type(key).code()=="hash"){
                Object value=redisTemplate.opsForHash().get(key,key);
                result.add(value);
            }
        }
        return result;
    }
}

 技术支持:盘古BPM工作流平台

相关教程