Skip to content

Commit 5f1f990

Browse files
Add option to set variables async for bpmn and cmmn
1 parent a586e9e commit 5f1f990

File tree

21 files changed

+1131
-3
lines changed

21 files changed

+1131
-3
lines changed

modules/flowable-cmmn-api/src/main/java/org/flowable/cmmn/api/CmmnRuntimeService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ public interface CmmnRuntimeService {
216216

217217
void setLocalVariable(String planItemInstanceId, String variableName, Object variableValue);
218218

219+
void setVariablesAsync(String caseInstanceId, Map<String, Object> variables);
220+
221+
void setVariableAsync(String caseInstanceId, String variableName, Object variableValue);
222+
223+
void setLocalVariablesAsync(String planItemInstanceId, Map<String, Object> variables);
224+
225+
void setLocalVariableAsync(String planItemInstanceId, String variableName, Object variableValue);
226+
219227
void removeVariable(String caseInstanceId, String variableName);
220228

221229
void removeVariables(String caseInstanceId, Collection<String> variableNames);

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/CmmnEngineConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.flowable.cmmn.engine.impl.job.CmmnHistoryCleanupJobHandler;
106106
import org.flowable.cmmn.engine.impl.job.ExternalWorkerTaskCompleteJobHandler;
107107
import org.flowable.cmmn.engine.impl.job.HistoricCaseInstanceMigrationJobHandler;
108+
import org.flowable.cmmn.engine.impl.job.SetAsyncVariablesJobHandler;
108109
import org.flowable.cmmn.engine.impl.job.TriggerTimerEventJobHandler;
109110
import org.flowable.cmmn.engine.impl.listener.CmmnListenerFactory;
110111
import org.flowable.cmmn.engine.impl.listener.CmmnListenerNotificationHelper;
@@ -1302,6 +1303,7 @@ public void initDependentScopeTypes() {
13021303
this.dependentScopeTypes.add(ScopeTypes.CMMN);
13031304
this.dependentScopeTypes.add(ScopeTypes.CMMN_VARIABLE_AGGREGATION);
13041305
this.dependentScopeTypes.add(ScopeTypes.CMMN_EXTERNAL_WORKER);
1306+
this.dependentScopeTypes.add(ScopeTypes.CMMN_ASYNC_VARIABLES);
13051307
}
13061308

13071309
public void initHistoryConfigurationSettings() {
@@ -1658,6 +1660,7 @@ public void initJobHandlers() {
16581660
jobHandlers.put(AsyncActivatePlanItemInstanceJobHandler.TYPE, new AsyncActivatePlanItemInstanceJobHandler());
16591661
jobHandlers.put(AsyncLeaveActivePlanItemInstanceJobHandler.TYPE, new AsyncLeaveActivePlanItemInstanceJobHandler());
16601662
jobHandlers.put(AsyncInitializePlanModelJobHandler.TYPE, new AsyncInitializePlanModelJobHandler());
1663+
jobHandlers.put(SetAsyncVariablesJobHandler.TYPE, new SetAsyncVariablesJobHandler(this));
16611664
jobHandlers.put(CmmnHistoryCleanupJobHandler.TYPE, new CmmnHistoryCleanupJobHandler());
16621665
jobHandlers.put(ExternalWorkerTaskCompleteJobHandler.TYPE, new ExternalWorkerTaskCompleteJobHandler(this));
16631666
addJobHandler(new CaseInstanceMigrationJobHandler());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.cmmn.engine.impl.cmd;
14+
15+
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
16+
import org.flowable.cmmn.engine.impl.job.SetAsyncVariablesJobHandler;
17+
import org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntity;
18+
import org.flowable.cmmn.engine.impl.persistence.entity.PlanItemInstanceEntity;
19+
import org.flowable.common.engine.api.scope.ScopeTypes;
20+
import org.flowable.job.service.JobService;
21+
import org.flowable.job.service.JobServiceConfiguration;
22+
import org.flowable.job.service.impl.persistence.entity.JobEntity;
23+
import org.flowable.variable.service.VariableService;
24+
import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntity;
25+
26+
public abstract class AbstractSetVariableAsyncCmd {
27+
28+
protected void addVariable(boolean isLocal, String scopeId, String subScopeId, String varName, Object varValue,
29+
String tenantId, VariableService variableService) {
30+
31+
VariableInstanceEntity variableInstance = variableService.createVariableInstance(varName);
32+
variableInstance.setScopeId(scopeId);
33+
variableInstance.setSubScopeId(subScopeId);
34+
variableInstance.setScopeType(ScopeTypes.CMMN_ASYNC_VARIABLES);
35+
variableInstance.setMetaInfo(String.valueOf(isLocal));
36+
37+
variableService.insertVariableInstanceWithValue(variableInstance, varValue, tenantId);
38+
}
39+
40+
protected void createSetAsyncVariablesJob(CaseInstanceEntity caseInstanceEntity, CmmnEngineConfiguration cmmnEngineConfiguration) {
41+
JobServiceConfiguration jobServiceConfiguration = cmmnEngineConfiguration.getJobServiceConfiguration();
42+
JobService jobService = jobServiceConfiguration.getJobService();
43+
44+
JobEntity job = jobService.createJob();
45+
job.setScopeId(caseInstanceEntity.getId());
46+
job.setScopeDefinitionId(caseInstanceEntity.getCaseDefinitionId());
47+
job.setScopeType(ScopeTypes.CMMN);
48+
job.setJobHandlerType(SetAsyncVariablesJobHandler.TYPE);
49+
50+
// Inherit tenant id (if applicable)
51+
if (caseInstanceEntity.getTenantId() != null) {
52+
job.setTenantId(caseInstanceEntity.getTenantId());
53+
}
54+
55+
jobService.createAsyncJob(job, true);
56+
jobService.scheduleAsyncJob(job);
57+
}
58+
59+
protected void createSetAsyncVariablesJob(PlanItemInstanceEntity planItemInstanceEntity, CmmnEngineConfiguration cmmnEngineConfiguration) {
60+
JobServiceConfiguration jobServiceConfiguration = cmmnEngineConfiguration.getJobServiceConfiguration();
61+
JobService jobService = jobServiceConfiguration.getJobService();
62+
63+
JobEntity job = jobService.createJob();
64+
job.setScopeId(planItemInstanceEntity.getCaseInstanceId());
65+
job.setSubScopeId(planItemInstanceEntity.getId());
66+
job.setScopeDefinitionId(planItemInstanceEntity.getCaseDefinitionId());
67+
job.setScopeType(ScopeTypes.CMMN);
68+
job.setJobHandlerType(SetAsyncVariablesJobHandler.TYPE);
69+
70+
// Inherit tenant id (if applicable)
71+
if (planItemInstanceEntity.getTenantId() != null) {
72+
job.setTenantId(planItemInstanceEntity.getTenantId());
73+
}
74+
75+
jobService.createAsyncJob(job, true);
76+
jobService.scheduleAsyncJob(job);
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.cmmn.engine.impl.cmd;
14+
15+
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
16+
import org.flowable.cmmn.engine.impl.persistence.entity.PlanItemInstanceEntity;
17+
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
18+
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
19+
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
20+
import org.flowable.common.engine.impl.interceptor.Command;
21+
import org.flowable.common.engine.impl.interceptor.CommandContext;
22+
23+
public class SetLocalVariableAsyncCmd extends AbstractSetVariableAsyncCmd implements Command<Void> {
24+
25+
protected String planItemInstanceId;
26+
protected String variableName;
27+
protected Object variableValue;
28+
29+
public SetLocalVariableAsyncCmd(String planItemInstanceId, String variableName, Object variableValue) {
30+
this.planItemInstanceId = planItemInstanceId;
31+
this.variableName = variableName;
32+
this.variableValue = variableValue;
33+
}
34+
35+
@Override
36+
public Void execute(CommandContext commandContext) {
37+
if (planItemInstanceId == null) {
38+
throw new FlowableIllegalArgumentException("planItemInstanceId is null");
39+
}
40+
if (variableName == null) {
41+
throw new FlowableIllegalArgumentException("variable name is null");
42+
}
43+
44+
CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
45+
PlanItemInstanceEntity planItemInstanceEntity = cmmnEngineConfiguration.getPlanItemInstanceEntityManager().findById(planItemInstanceId);
46+
if (planItemInstanceEntity == null) {
47+
throw new FlowableObjectNotFoundException("No plan item instance found for id " + planItemInstanceId, PlanItemInstanceEntity.class);
48+
}
49+
50+
addVariable(true, planItemInstanceEntity.getCaseInstanceId(), planItemInstanceEntity.getId(), variableName, variableValue, planItemInstanceEntity.getTenantId(),
51+
cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService());
52+
createSetAsyncVariablesJob(planItemInstanceEntity, cmmnEngineConfiguration);
53+
54+
return null;
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.cmmn.engine.impl.cmd;
14+
15+
import java.util.Map;
16+
17+
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
18+
import org.flowable.cmmn.engine.impl.persistence.entity.PlanItemInstanceEntity;
19+
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
20+
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
21+
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
22+
import org.flowable.common.engine.impl.interceptor.Command;
23+
import org.flowable.common.engine.impl.interceptor.CommandContext;
24+
25+
public class SetLocalVariablesAsyncCmd extends AbstractSetVariableAsyncCmd implements Command<Void> {
26+
27+
protected String planItemInstanceId;
28+
protected Map<String, Object> variables;
29+
30+
public SetLocalVariablesAsyncCmd(String planItemInstanceId, Map<String, Object> variables) {
31+
this.planItemInstanceId = planItemInstanceId;
32+
this.variables = variables;
33+
}
34+
35+
@Override
36+
public Void execute(CommandContext commandContext) {
37+
if (planItemInstanceId == null) {
38+
throw new FlowableIllegalArgumentException("planItemInstanceId is null");
39+
}
40+
if (variables == null) {
41+
throw new FlowableIllegalArgumentException("variables is null");
42+
}
43+
if (variables.isEmpty()) {
44+
throw new FlowableIllegalArgumentException("variables is empty");
45+
}
46+
47+
CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
48+
PlanItemInstanceEntity planItemInstanceEntity = cmmnEngineConfiguration.getPlanItemInstanceEntityManager().findById(planItemInstanceId);
49+
if (planItemInstanceEntity == null) {
50+
throw new FlowableObjectNotFoundException("No plan item instance found for id " + planItemInstanceId, PlanItemInstanceEntity.class);
51+
}
52+
53+
for (String variableName : variables.keySet()) {
54+
addVariable(true, planItemInstanceEntity.getCaseInstanceId(), planItemInstanceEntity.getId(), variableName, variables.get(variableName),
55+
planItemInstanceEntity.getTenantId(), cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService());
56+
}
57+
58+
createSetAsyncVariablesJob(planItemInstanceEntity, cmmnEngineConfiguration);
59+
60+
return null;
61+
}
62+
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.cmmn.engine.impl.cmd;
14+
15+
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
16+
import org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntity;
17+
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
18+
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
19+
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
20+
import org.flowable.common.engine.impl.interceptor.Command;
21+
import org.flowable.common.engine.impl.interceptor.CommandContext;
22+
23+
public class SetVariableAsyncCmd extends AbstractSetVariableAsyncCmd implements Command<Void> {
24+
25+
protected String caseInstanceId;
26+
protected String variableName;
27+
protected Object variableValue;
28+
29+
public SetVariableAsyncCmd(String caseInstanceId, String variableName, Object variableValue) {
30+
this.caseInstanceId = caseInstanceId;
31+
this.variableName = variableName;
32+
this.variableValue = variableValue;
33+
}
34+
35+
@Override
36+
public Void execute(CommandContext commandContext) {
37+
if (caseInstanceId == null) {
38+
throw new FlowableIllegalArgumentException("caseInstanceId is null");
39+
}
40+
if (variableName == null) {
41+
throw new FlowableIllegalArgumentException("variable name is null");
42+
}
43+
44+
CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
45+
CaseInstanceEntity caseInstanceEntity = cmmnEngineConfiguration.getCaseInstanceEntityManager().findById(caseInstanceId);
46+
if (caseInstanceEntity == null) {
47+
throw new FlowableObjectNotFoundException("No case instance found for id " + caseInstanceId, CaseInstanceEntity.class);
48+
}
49+
50+
addVariable(false, caseInstanceId, null, variableName, variableValue, caseInstanceEntity.getTenantId(),
51+
cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService());
52+
createSetAsyncVariablesJob(caseInstanceEntity, cmmnEngineConfiguration);
53+
54+
return null;
55+
}
56+
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.cmmn.engine.impl.cmd;
14+
15+
import java.util.Map;
16+
17+
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
18+
import org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntity;
19+
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
20+
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
21+
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
22+
import org.flowable.common.engine.impl.interceptor.Command;
23+
import org.flowable.common.engine.impl.interceptor.CommandContext;
24+
25+
public class SetVariablesAsyncCmd extends AbstractSetVariableAsyncCmd implements Command<Void> {
26+
27+
protected String caseInstanceId;
28+
protected Map<String, Object> variables;
29+
30+
public SetVariablesAsyncCmd(String caseInstanceId, Map<String, Object> variables) {
31+
this.caseInstanceId = caseInstanceId;
32+
this.variables = variables;
33+
}
34+
35+
@Override
36+
public Void execute(CommandContext commandContext) {
37+
if (caseInstanceId == null) {
38+
throw new FlowableIllegalArgumentException("caseInstanceId is null");
39+
}
40+
if (variables == null) {
41+
throw new FlowableIllegalArgumentException("variables is null");
42+
}
43+
if (variables.isEmpty()) {
44+
throw new FlowableIllegalArgumentException("variables is empty");
45+
}
46+
47+
CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
48+
CaseInstanceEntity caseInstanceEntity = cmmnEngineConfiguration.getCaseInstanceEntityManager().findById(caseInstanceId);
49+
if (caseInstanceEntity == null) {
50+
throw new FlowableObjectNotFoundException("No case instance found for id " + caseInstanceId, CaseInstanceEntity.class);
51+
}
52+
53+
for (String variableName : variables.keySet()) {
54+
addVariable(false, caseInstanceId, null, variableName, variables.get(variableName), caseInstanceEntity.getTenantId(),
55+
cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService());
56+
}
57+
58+
createSetAsyncVariablesJob(caseInstanceEntity, cmmnEngineConfiguration);
59+
60+
return null;
61+
}
62+
63+
}

0 commit comments

Comments
 (0)