
Context Switch
When executing a process instance, the process engine has to know which process application provides the corresponding resources. It then internally performs a context switch. This has the following effects:- The thread context class loader is set to the process application classloader. This enables loading classes from the process application, e.g., a Java Delegate implementation.
- The process engine can access the resources it manages for that particular process application. This enables invoking scripting engines or Spin data formats specific to the process application.
Mechanics behind the Context SwitchNote that the actual mechanics behind the context switch are platform dependent. For example: in a servlet container like Apache Tomcat, it is only necessary to set the Thread’s current Context Classloader to the web application Classloader. Context specific operations like the resolution of application-local JNDI names all build on this. In an EJB container, this is more complex. This is why the ProcessApplication class is an EJB itself in that environment (see: EJB Process Application). The process engine can then add an invocation of a business method of that EJB to the call stack and have the Application Server perform its specific logic behind the scenes.
- Delegation Code Invocation: Whenever delegation code like Java Delegates, execution/task listeners (Java code or scripts), etc. is called by the process engine
- Explicit Process Application Context Declaration: For every engine API invocation, when a process application was declared using the utility class
org.camunda.bpm.application.ProcessApplicationContext
Declare Process Application Context
Process application context must be declared whenever custom code uses the engine API that is not part of delegation code and when a context switch is needed for proper function.Example
To clarify the use case, we assume that a process application employs the feature to serialize object-type variables in the JSON format. However, for that application JSON serialization shall be customized (think about the numerous ways to serialize a date as a JSON string). The process application therefore contains a ASEE Flow Spin data format configurator implementation that configures the Spin JSON data format in the desired way. In turn, the process engine manages a Spin data format for that specific process application to serialize object values with. Now, we assume that a Java servlet calls the process engine API to submit a Java object and serialize it with the JSON format. The code might look as follows:org.camunda.bpm.application.ProcessApplicationContext provides. In particular, the method #setCurrentProcessApplication declares the process application to switch into for following engine API invocations. The method #clear resets this declaration. In the example, we wrap the #setVariable invocation accordingly:
#setVariable call in. It therefore can access the correct JSON data format and serializes the variable correctly.
Java API
The methodsProcessApplicationContext#setCurrentProcessApplication declare process application context for all following API invocations until ProcessApplicationContext#clear is called. It is therefore advised to use a try-finally block to ensure clearance even when exceptions are raised. In addition, the methods ProcessApplicationContext#withProcessApplicationContext execute a Callable and declare the context during the Callable’s execution.