Set a transient object variable without serializing in Camunda

I have a process in Camunda that doesn’t have any user tasks. Iy has only some service tasks. I want to set a transient Object variable and then get in several tasks and manipulate it but Camunda forces me to make it implement the Serializable interface.

My code is:

TypedValue transient_ctx = Variables.objectValue( new BpmContext(), true).create();

ProcessInstanceWithVariables process_res = 
       runtimeService.createProcessInstanceByKey(param.getProcessKey())
                    .setVariable("Param", transient_ctx)
                    .executeWithVariablesInReturn();

but get the following exception:

org.camunda.bpm.engine.ProcessEngineException: Cannot find serializer for value 'ObjectValue [value=org.BpmContext@433a4e29, isDeserialized=true, serializationDataFormat=null, objectTypeName=null, serializedValue=null, isTransient=true]'.

Is there any way for setting a transient object normally without serializing that?

Answer

If you are going to transport the data as process data, then Camunda requires its serialization, be it in Java or other serialization data formats.

Since you are ok with a transient process data it means you do not expect the data to be available for longer than the life of the current thread. I guess a ThreadLocal variable would also do the trick for you. There are many examples on the internet. This is one: https://codedelay.com/threadlocal-variable-in-java-with-example/