Package org.camunda.bpm.engine.cdi
Class BusinessProcess
- java.lang.Object
-
- org.camunda.bpm.engine.cdi.BusinessProcess
-
- All Implemented Interfaces:
java.io.Serializable
@Named public class BusinessProcess extends java.lang.Object implements java.io.SerializableBean supporting contextual business process management. This allows us to implement a unit of work, in which a particular CDI scope (Conversation / Request / Thread) is associated with a particular Execution / ProcessInstance or Task. The protocol is that we associate theBusinessProcessbean with a particular Execution / Task, then perform some changes (retrieve / set process variables) and then end the unit of work. This bean makes sure that our changes are only "flushed" to the process engine when we successfully complete the unit of work. A typical usage scenario might look like this:
1st unit of work ("process instantiation"):conversation.begin(); ... businessProcess.setVariable("billingId", "1"); // setting variables before starting the process businessProcess.startProcessByKey("billingProcess"); conversation.end();2nd unit of work ("perform a user task"):conversation.begin(); businessProcess.startTask(id); // now we have associated a task with the current conversation ... // this allows us to retrieve and change process variables // and @BusinessProcessScoped beans businessProcess.setVariable("billingDetails", "someValue"); // these changes are cached in the conversation ... businessProcess.completeTask(); // now all changed process variables are flushed conversation.end();NOTE: in the absence of a conversation, (non faces request, i.e. when processing a JAX-RS, JAX-WS, JMS, remote EJB or plain Servlet requests), theBusinessProcessbean associates with the current Request (see@RequestScoped). NOTE: in the absence of a request, ie. when the JobExecutor accesses@BusinessProcessScopedbeans, the execution is associated with the current thread.- Author:
- Daniel Meyer, Falko Menge
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description BusinessProcess()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidassertCommandContextNotActive()protected voidassertExecutionAssociated()protected voidassertTaskAssociated()voidassociateExecutionById(java.lang.String executionId)Associate with the provided execution.voidcompleteTask()Completes the current UserTask, seeTaskService.complete(String)voidcompleteTask(boolean endConversation)voidflushVariableCache()This method allows to flush the cached variables to the Task or Execution.VariableMapgetAndClearCachedLocalVariableMap()Get theVariableMapof local cached variables and clear the internal variable cache.VariableMapgetAndClearCachedVariableMap()Get theVariableMapof cached variables and clear the internal variable cache.java.util.Map<java.lang.String,java.lang.Object>getAndClearVariableCache()Deprecated.usegetAndClearCachedVariableMap()insteadjava.util.Map<java.lang.String,java.lang.Object>getAndClearVariableLocalCache()Deprecated.usegetAndClearCachedLocalVariableMap()insteadVariableMapgetCachedLocalVariableMap()Get a copy of theVariableMapof local cached variables.VariableMapgetCachedVariableMap()Get a copy of theVariableMapof cached variables.ExecutiongetExecution()Returns the currently associated execution or 'null'java.lang.StringgetExecutionId()ProcessInstancegetProcessInstance()Returns theProcessInstancecurrently associated or 'null'java.lang.StringgetProcessInstanceId()Returns the id of the currently associated process instance or 'null'TaskgetTask()Returns the currently associatedTaskor 'null'java.lang.StringgetTaskId()Returns the id of the task associated with the current conversation or 'null'.<T> TgetVariable(java.lang.String variableName)java.util.Map<java.lang.String,java.lang.Object>getVariableCache()Deprecated.usegetCachedVariableMap()instead<T> TgetVariableLocal(java.lang.String variableName)java.util.Map<java.lang.String,java.lang.Object>getVariableLocalCache()Deprecated.usegetCachedLocalVariableMap()instead<T extends TypedValue>
TgetVariableLocalTyped(java.lang.String variableName)<T extends TypedValue>
TgetVariableTyped(java.lang.String variableName)booleanisAssociated()returns true if anExecutionis associated.booleanisTaskAssociated()voidsaveTask()Save the currently associated task.voidsetExecution(Execution execution)protected voidsetExecutionId(java.lang.String executionId)voidsetTask(Task task)voidsetTaskId(java.lang.String taskId)voidsetVariable(java.lang.String variableName, java.lang.Object value)Set a value for a process variable.voidsetVariableLocal(java.lang.String variableName, java.lang.Object value)Set a value for a local process variable.voidsignalExecution()Signals the current execution, seeRuntimeService.signal(String)voidsignalExecution(boolean endConversation)ProcessInstancestartProcessById(java.lang.String processDefinitionId)ProcessInstancestartProcessById(java.lang.String processDefinitionId, java.lang.String businessKey)ProcessInstancestartProcessById(java.lang.String processDefinitionId, java.lang.String businessKey, java.util.Map<java.lang.String,java.lang.Object> variables)ProcessInstancestartProcessById(java.lang.String processDefinitionId, java.util.Map<java.lang.String,java.lang.Object> variables)ProcessInstancestartProcessByKey(java.lang.String key)ProcessInstancestartProcessByKey(java.lang.String key, java.lang.String businessKey)ProcessInstancestartProcessByKey(java.lang.String key, java.lang.String businessKey, java.util.Map<java.lang.String,java.lang.Object> variables)ProcessInstancestartProcessByKey(java.lang.String key, java.util.Map<java.lang.String,java.lang.Object> variables)ProcessInstancestartProcessByMessage(java.lang.String messageName)ProcessInstancestartProcessByMessage(java.lang.String messageName, java.lang.String businessKey, java.util.Map<java.lang.String,java.lang.Object> processVariables)ProcessInstancestartProcessByMessage(java.lang.String messageName, java.util.Map<java.lang.String,java.lang.Object> processVariables)TaskstartTask(java.lang.String taskId)Associates the task with the provided taskId with the current conversation.TaskstartTask(java.lang.String taskId, boolean beginConversation)voidstopTask()Stop working on a task.voidstopTask(boolean endConversation)Stop working on a task.
-
-
-
Method Detail
-
startProcessById
public ProcessInstance startProcessById(java.lang.String processDefinitionId)
-
startProcessById
public ProcessInstance startProcessById(java.lang.String processDefinitionId, java.lang.String businessKey)
-
startProcessById
public ProcessInstance startProcessById(java.lang.String processDefinitionId, java.util.Map<java.lang.String,java.lang.Object> variables)
-
startProcessById
public ProcessInstance startProcessById(java.lang.String processDefinitionId, java.lang.String businessKey, java.util.Map<java.lang.String,java.lang.Object> variables)
-
startProcessByKey
public ProcessInstance startProcessByKey(java.lang.String key)
-
startProcessByKey
public ProcessInstance startProcessByKey(java.lang.String key, java.lang.String businessKey)
-
startProcessByKey
public ProcessInstance startProcessByKey(java.lang.String key, java.util.Map<java.lang.String,java.lang.Object> variables)
-
startProcessByKey
public ProcessInstance startProcessByKey(java.lang.String key, java.lang.String businessKey, java.util.Map<java.lang.String,java.lang.Object> variables)
-
startProcessByMessage
public ProcessInstance startProcessByMessage(java.lang.String messageName)
-
startProcessByMessage
public ProcessInstance startProcessByMessage(java.lang.String messageName, java.util.Map<java.lang.String,java.lang.Object> processVariables)
-
startProcessByMessage
public ProcessInstance startProcessByMessage(java.lang.String messageName, java.lang.String businessKey, java.util.Map<java.lang.String,java.lang.Object> processVariables)
-
associateExecutionById
public void associateExecutionById(java.lang.String executionId)
Associate with the provided execution. This starts a unit of work.- Parameters:
executionId- the id of the execution to associate with.
-
isAssociated
public boolean isAssociated()
returns true if anExecutionis associated.- See Also:
associateExecutionById(String)
-
signalExecution
public void signalExecution()
Signals the current execution, seeRuntimeService.signal(String)Ends the current unit of work (flushes changes to process variables set usingsetVariable(String, Object)or made on@BusinessProcessScopedbeans).- Throws:
ProcessEngineCdiException- if no execution is currently associatedProcessEngineException- if the activiti command fails
-
signalExecution
public void signalExecution(boolean endConversation)
-
startTask
public Task startTask(java.lang.String taskId)
Associates the task with the provided taskId with the current conversation.- Parameters:
taskId- the id of the task- Returns:
- the resumed task
- Throws:
ProcessEngineCdiException- if no such task is found
-
startTask
public Task startTask(java.lang.String taskId, boolean beginConversation)
-
completeTask
public void completeTask()
Completes the current UserTask, seeTaskService.complete(String)Ends the current unit of work (flushes changes to process variables set usingsetVariable(String, Object)or made on@BusinessProcessScopedbeans).- Throws:
ProcessEngineCdiException- if no task is currently associatedProcessEngineException- if the activiti command fails
-
completeTask
public void completeTask(boolean endConversation)
-
isTaskAssociated
public boolean isTaskAssociated()
-
saveTask
public void saveTask()
Save the currently associated task.- Throws:
ProcessEngineCdiException- if called from a process engine command or if no Task is currently associated.
-
stopTask
public void stopTask()
Stop working on a task. Clears the current association.
NOTE: this method does not flush any changes.
- If you want to flush changes to process variables, call
flushVariableCache()prior to calling this method, - If you need to flush changes to the task object, use
saveTask()prior to calling this method.
- Throws:
ProcessEngineCdiException- if called from a process engine command or if no Task is currently associated.
- If you want to flush changes to process variables, call
-
stopTask
public void stopTask(boolean endConversation)
Stop working on a task. Clears the current association.
NOTE: this method does not flush any changes.
- If you want to flush changes to process variables, call
flushVariableCache()prior to calling this method, - If you need to flush changes to the task object, use
saveTask()prior to calling this method.
This method allows you to optionally end the current conversation
- Parameters:
endConversation- if true, end current conversation.- Throws:
ProcessEngineCdiException- if called from a process engine command or if no Task is currently associated.
- If you want to flush changes to process variables, call
-
getVariable
public <T> T getVariable(java.lang.String variableName)
- Parameters:
variableName- the name of the process variable for which the value is to be retrieved- Returns:
- the value of the provided process variable or 'null' if no such variable is set
-
getVariableTyped
public <T extends TypedValue> T getVariableTyped(java.lang.String variableName)
- Parameters:
variableName- the name of the process variable for which the value is to be retrieved- Returns:
- the typed value of the provided process variable or 'null' if no such variable is set
- Since:
- 7.3
-
setVariable
public void setVariable(java.lang.String variableName, java.lang.Object value)Set a value for a process variable. NOTE: If no execution is currently associated, the value is temporarily cached and flushed to the process instance at the end of the unit of work- Parameters:
variableName- the name of the process variable for which a value is to be setvalue- the value to be set
-
getAndClearCachedVariableMap
public VariableMap getAndClearCachedVariableMap()
Get theVariableMapof cached variables and clear the internal variable cache.- Returns:
- the
VariableMapof cached variables - Since:
- 7.3
-
getAndClearVariableCache
@Deprecated public java.util.Map<java.lang.String,java.lang.Object> getAndClearVariableCache()
Deprecated.usegetAndClearCachedVariableMap()insteadGet the map of cached variables and clear the internal variable cache.- Returns:
- the map of cached variables
-
getCachedVariableMap
public VariableMap getCachedVariableMap()
Get a copy of theVariableMapof cached variables.- Returns:
- a copy of the
VariableMapof cached variables. - Since:
- 7.3
-
getVariableCache
@Deprecated public java.util.Map<java.lang.String,java.lang.Object> getVariableCache()
Deprecated.usegetCachedVariableMap()insteadGet a copy of the map of cached variables.- Returns:
- a copy of the map of cached variables.
-
getVariableLocal
public <T> T getVariableLocal(java.lang.String variableName)
- Parameters:
variableName- the name of the local process variable for which the value is to be retrieved- Returns:
- the value of the provided local process variable or 'null' if no such variable is set
-
getVariableLocalTyped
public <T extends TypedValue> T getVariableLocalTyped(java.lang.String variableName)
- Parameters:
variableName- the name of the local process variable for which the value is to be retrieved- Returns:
- the typed value of the provided local process variable or 'null' if no such variable is set
- Since:
- 7.3
-
setVariableLocal
public void setVariableLocal(java.lang.String variableName, java.lang.Object value)Set a value for a local process variable. NOTE: If a task or execution is currently associated, the value is temporarily cached and flushed to the process instance at the end of the unit of work - otherwise an Exception will be thrown- Parameters:
variableName- the name of the local process variable for which a value is to be setvalue- the value to be set
-
getAndClearCachedLocalVariableMap
public VariableMap getAndClearCachedLocalVariableMap()
Get theVariableMapof local cached variables and clear the internal variable cache.- Returns:
- the
VariableMapof cached variables - Since:
- 7.3
-
getAndClearVariableLocalCache
@Deprecated public java.util.Map<java.lang.String,java.lang.Object> getAndClearVariableLocalCache()
Deprecated.usegetAndClearCachedLocalVariableMap()insteadGet the map of local cached variables and clear the internal variable cache.- Returns:
- the map of cached variables
-
getCachedLocalVariableMap
public VariableMap getCachedLocalVariableMap()
Get a copy of theVariableMapof local cached variables.- Returns:
- a copy of the
VariableMapof local cached variables. - Since:
- 7.3
-
getVariableLocalCache
@Deprecated public java.util.Map<java.lang.String,java.lang.Object> getVariableLocalCache()
Deprecated.usegetCachedLocalVariableMap()insteadGet a copy of the map of local cached variables.- Returns:
- a copy of the map of local cached variables.
-
flushVariableCache
public void flushVariableCache()
This method allows to flush the cached variables to the Task or Execution.
- If a Task instance is currently associated,
the variables will be flushed using
TaskService.setVariables(String, Map) - If an Execution instance is currently associated,
the variables will be flushed using
RuntimeService.setVariables(String, Map) - If neither a Task nor an Execution is currently associated, ProcessEngineCdiException is thrown.
A successful invocation of this method will empty the variable cache.
If this method is called from an active command (ie. from inside a Java Delegate).
ProcessEngineCdiExceptionis thrown.- Throws:
ProcessEngineCdiException- if called from a process engine command or if neither a Task nor an Execution is associated.
- If a Task instance is currently associated,
the variables will be flushed using
-
setTask
public void setTask(Task task)
- See Also:
startTask(String)
-
setTaskId
public void setTaskId(java.lang.String taskId)
- See Also:
startTask(String)
-
setExecution
public void setExecution(Execution execution)
- See Also:
associateExecutionById(String)
-
setExecutionId
protected void setExecutionId(java.lang.String executionId)
- See Also:
associateExecutionById(String)
-
getProcessInstanceId
public java.lang.String getProcessInstanceId()
Returns the id of the currently associated process instance or 'null'
-
getTaskId
public java.lang.String getTaskId()
Returns the id of the task associated with the current conversation or 'null'.
-
getTask
public Task getTask()
Returns the currently associatedTaskor 'null'- Throws:
ProcessEngineCdiException- if noTaskis associated. UseisTaskAssociated()to check whether an association exists.
-
getExecution
public Execution getExecution()
Returns the currently associated execution or 'null'
-
getExecutionId
public java.lang.String getExecutionId()
- See Also:
getExecution()
-
getProcessInstance
public ProcessInstance getProcessInstance()
Returns theProcessInstancecurrently associated or 'null'- Throws:
ProcessEngineCdiException- if noExecutionis associated. UseisAssociated()to check whether an association exists.
-
assertExecutionAssociated
protected void assertExecutionAssociated()
-
assertTaskAssociated
protected void assertTaskAssociated()
-
assertCommandContextNotActive
protected void assertCommandContextNotActive()
-
-