示例如下
package com.zz.flow.utils;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ByteArrayEntity;
import java.io.UnsupportedEncodingException;
/**
* 动态更新流程定义
* @author lqx
* @date 2021/3/2 14:51
*/
public class UpdateByteArrayCmd implements Command<Object> {
public UpdateByteArrayCmd() {
}
@Override
public Object execute(CommandContext commandContext) {
String h="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:activiti=\"http://activiti.org/bpmn\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:omgdc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:omgdi=\"http://www.omg.org/spec/DD/20100524/DI\" typeLanguage=\"http://www.w3.org/2001/XMLSchema\" expressionLanguage=\"http://www.w3.org/1999/XPath\" targetNamespace=\"http://www.activiti.org/processdef\">" +
" <process id=\"obj1356775660033163265\" isExecutable=\"true\">" +
" <startEvent id=\"sid1612315779740\"/>" +
/*"<userTask id=\"sid1612315819963\" name=\"a\" activiti:assignee=\"${user_sid1612315819963}\">" +
" <multiInstanceLoopCharacteristics isSequential=\"false\" activiti:collection=\"${list_sid1612315819963}\" activiti:elementVariable=\"user_sid1612315819963\"/>" +
" </userTask>"+*/
"<userTask id=\"sid1612315819963\" name=\"a\" activiti:assignee=\"${createUser}\">" +
" </userTask>"+
" <sequenceFlow id=\"sid1612315823245\" sourceRef=\"sid1612315779740\" targetRef=\"sid1612315819963\"/>" +
" <parallelGateway id=\"sid1612315819303\"/>" +
" <sequenceFlow id=\"sid1612315851310\" sourceRef=\"sid1612315819963\" targetRef=\"sid1612315819303\"/>" +
" <userTask id=\"sid1612315794587\" name=\"b\" activiti:assignee=\"${user_sid1612315794587}\">" +
" <multiInstanceLoopCharacteristics isSequential=\"false\" activiti:collection=\"${list_sid1612315794587}\" activiti:elementVariable=\"user_sid1612315794587\"/>" +
" </userTask>" +
" <sequenceFlow id=\"sid1612315797380\" sourceRef=\"sid1612315819303\" targetRef=\"sid1612315794587\"/>" +
" <userTask id=\"sid1612315784629\" name=\"c\" activiti:assignee=\"${user_sid1612315784629}\"/>" +
" <sequenceFlow id=\"sid1612315837016\" sourceRef=\"sid1612315819303\" targetRef=\"sid1612315784629\"/>" +
" <parallelGateway id=\"sid1612315856360\"/>" +
" <userTask id=\"sid1612315785641\" name=\"e\" activiti:assignee=\"${user_sid1612315785641}\"/>" +
" <sequenceFlow id=\"sid1612315808969\" sourceRef=\"sid1612315856360\" targetRef=\"sid1612315785641\"/>" +
" <userTask id=\"sid1612315774748\" name=\"d\" activiti:assignee=\"${user_sid1612315774748}\"/>" +
" <sequenceFlow id=\"sid1612315834465\" sourceRef=\"sid1612315856360\" targetRef=\"sid1612315774748\"/>" +
" <sequenceFlow id=\"sid1612315801477\" sourceRef=\"sid1612315794587\" targetRef=\"sid1612315856360\"/>" +
" <parallelGateway id=\"sid1612315797069\"/>" +
" <sequenceFlow id=\"sid1612315802306\" sourceRef=\"sid1612315785641\" targetRef=\"sid1612315797069\"/>" +
" <sequenceFlow id=\"sid1612315759794\" sourceRef=\"sid1612315774748\" targetRef=\"sid1612315797069\"/>" +
" <userTask id=\"sid1612315811783\" name=\"f\" activiti:assignee=\"${user_sid1612315811783}\"/>" +
" <sequenceFlow id=\"sid1612315788209\" sourceRef=\"sid1612315797069\" targetRef=\"sid1612315811783\"/>" +
" <parallelGateway id=\"sid1612315765072\"/>" +
" <sequenceFlow id=\"sid1612315794022\" sourceRef=\"sid1612315784629\" targetRef=\"sid1612315765072\"/>" +
" <sequenceFlow id=\"sid1612315790937\" sourceRef=\"sid1612315811783\" targetRef=\"sid1612315765072\"/>" +
" <userTask id=\"sid1612315851207\" name=\"g\" activiti:assignee=\"${user_sid1612315851207}\"/>" +
" <sequenceFlow id=\"sid1612315839669\" sourceRef=\"sid1612315765072\" targetRef=\"sid1612315851207\"/>" +
" <endEvent id=\"sid1612315822608\"/>" +
" <sequenceFlow id=\"sid1612315801380\" sourceRef=\"sid1612315851207\" targetRef=\"sid1612315822608\"/>" +
" </process>" +
" <bpmndi:BPMNDiagram id=\"BPMNDiagram_obj1356775660033163265\">" +
" <bpmndi:BPMNPlane bpmnElement=\"obj1356775660033163265\" id=\"BPMNPlane_obj1356775660033163265\">" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315779740\" id=\"BPMNShape_sid1612315779740\">" +
" <omgdc:Bounds height=\"30.0\" width=\"30.0\" x=\"162.06668090820312\" y=\"101.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315819963\" id=\"BPMNShape_sid1612315819963\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"237.06668090820312\" y=\"76.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315819303\" id=\"BPMNShape_sid1612315819303\">" +
" <omgdc:Bounds height=\"40.0\" width=\"40.0\" x=\"398.0666809082031\" y=\"94.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315794587\" id=\"BPMNShape_sid1612315794587\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"480.0\" y=\"15.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315784629\" id=\"BPMNShape_sid1612315784629\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"483.0666809082031\" y=\"195.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315856360\" id=\"BPMNShape_sid1612315856360\">" +
" <omgdc:Bounds height=\"40.0\" width=\"40.0\" x=\"675.0\" y=\"40.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315785641\" id=\"BPMNShape_sid1612315785641\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"825.0\" y=\"0.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315774748\" id=\"BPMNShape_sid1612315774748\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"735.0\" y=\"105.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315797069\" id=\"BPMNShape_sid1612315797069\">" +
" <omgdc:Bounds height=\"40.0\" width=\"40.0\" x=\"1011.5666667222977\" y=\"109.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315811783\" id=\"BPMNShape_sid1612315811783\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"1020.0\" y=\"180.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315765072\" id=\"BPMNShape_sid1612315765072\">" +
" <omgdc:Bounds height=\"40.0\" width=\"40.0\" x=\"782.4833335876465\" y=\"228.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315851207\" id=\"BPMNShape_sid1612315851207\">" +
" <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"810.0\" y=\"285.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNShape bpmnElement=\"sid1612315822608\" id=\"BPMNShape_sid1612315822608\">" +
" <omgdc:Bounds height=\"28.0\" width=\"28.0\" x=\"955.0\" y=\"311.0\"/>" +
" </bpmndi:BPMNShape>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315759794\" id=\"BPMNEdge_sid1612315759794\">" +
" <omgdi:waypoint x=\"835.0\" y=\"141.75544139589226\"/>" +
" <omgdi:waypoint x=\"1012.7854048203002\" y=\"130.21873809800255\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315788209\" id=\"BPMNEdge_sid1612315788209\">" +
" <omgdi:waypoint x=\"1051.0666667222977\" y=\"129.5\"/>" +
" <omgdi:waypoint x=\"1070.0\" y=\"129.5\"/>" +
" <omgdi:waypoint x=\"1070.0\" y=\"180.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315802306\" id=\"BPMNEdge_sid1612315802306\">" +
" <omgdi:waypoint x=\"925.0\" y=\"68.4223972647573\"/>" +
" <omgdi:waypoint x=\"1018.8152075106651\" y=\"121.75145921163259\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315797380\" id=\"BPMNEdge_sid1612315797380\">" +
" <omgdi:waypoint x=\"418.5666809082031\" y=\"94.5\"/>" +
" <omgdi:waypoint x=\"418.5666809082031\" y=\"55.0\"/>" +
" <omgdi:waypoint x=\"480.0\" y=\"55.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315851310\" id=\"BPMNEdge_sid1612315851310\">" +
" <omgdi:waypoint x=\"337.0666809082031\" y=\"115.23664122137404\"/>" +
" <omgdi:waypoint x=\"398.3674327879024\" y=\"114.30075187969925\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315839669\" id=\"BPMNEdge_sid1612315839669\">" +
" <omgdi:waypoint x=\"821.9833335876465\" y=\"248.5\"/>" +
" <omgdi:waypoint x=\"860.0\" y=\"248.5\"/>" +
" <omgdi:waypoint x=\"860.0\" y=\"285.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315837016\" id=\"BPMNEdge_sid1612315837016\">" +
" <omgdi:waypoint x=\"437.5666809082031\" y=\"114.5\"/>" +
" <omgdi:waypoint x=\"533.0666809082031\" y=\"114.5\"/>" +
" <omgdi:waypoint x=\"533.0666809082031\" y=\"195.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315794022\" id=\"BPMNEdge_sid1612315794022\">" +
" <omgdi:waypoint x=\"583.0666809082031\" y=\"237.41261998297256\"/>" +
" <omgdi:waypoint x=\"783.4039591865028\" y=\"247.07937440114372\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315801380\" id=\"BPMNEdge_sid1612315801380\">" +
" <omgdi:waypoint x=\"910.0\" y=\"325.0\"/>" +
" <omgdi:waypoint x=\"955.0\" y=\"325.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315834465\" id=\"BPMNEdge_sid1612315834465\">" +
" <omgdi:waypoint x=\"714.5\" y=\"60.5\"/>" +
" <omgdi:waypoint x=\"785.0\" y=\"60.5\"/>" +
" <omgdi:waypoint x=\"785.0\" y=\"105.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315808969\" id=\"BPMNEdge_sid1612315808969\">" +
" <omgdi:waypoint x=\"714.5\" y=\"60.5\"/>" +
" <omgdi:waypoint x=\"770.0\" y=\"60.5\"/>" +
" <omgdi:waypoint x=\"770.0\" y=\"40.0\"/>" +
" <omgdi:waypoint x=\"825.0\" y=\"40.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315801477\" id=\"BPMNEdge_sid1612315801477\">" +
" <omgdi:waypoint x=\"580.0\" y=\"55.0\"/>" +
" <omgdi:waypoint x=\"680.0\" y=\"55.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315823245\" id=\"BPMNEdge_sid1612315823245\">" +
" <omgdi:waypoint x=\"192.06668090820312\" y=\"116.0\"/>" +
" <omgdi:waypoint x=\"237.06668090820312\" y=\"116.0\"/>" +
" </bpmndi:BPMNEdge>" +
" <bpmndi:BPMNEdge bpmnElement=\"sid1612315790937\" id=\"BPMNEdge_sid1612315790937\">" +
" <omgdi:waypoint x=\"1020.0\" y=\"225.2333188013117\"/>" +
" <omgdi:waypoint x=\"820.5883474036233\" y=\"246.1050138159768\"/>" +
" </bpmndi:BPMNEdge>" +
" </bpmndi:BPMNPlane>" +
" </bpmndi:BPMNDiagram>" +
"</definitions>";
try {
ByteArrayEntity resourceEntity = new ByteArrayEntity(h.getBytes("UTF-8"));
resourceEntity.setId("obj1356889910785433603");
resourceEntity.setRevision(6);
commandContext.getDbSqlSession().update(resourceEntity);
//清除缓存
commandContext.getProcessEngineConfiguration().getProcessDefinitionCache().clear();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
调用如下
@Resource
private ManagementService managementService;
//controller中如下
managementService.executeCommand(new UpdateByteArrayCmd())
经进一步测试 修改那个流程定义的二进制数据 只清除这个流程定义的缓存即可 代码如下
commandContext.getDbSqlSession().update(updateXmlByteArr);
commandContext.getDbSqlSession().update(updateImgByteArr);
commandContext.getProcessEngineConfiguration().getProcessDefinitionCache().remove(processDefId);
commandContext.getProcessEngineConfiguration().getDeploymentManager().getBpmnModelCache().remove(processDefId);