Serializing and Deserializing Process Variable Error

*Hi

variables.put(“next”,Variables.objectValue(customObject).serializationDataFormat(“application/json”).create());

how do i retrieve back ?
I followed what is in the camunda doc but no luck

ObjectValue=customObject= externaltask.GetVariableTyped(“next”)
string customObject2=customObject.getValueSerialized();

its throwing me ValueMapperException… any idea what i am doing wrong

vish

@vishucamunda,

When using a serialized representation of variables, the Java serialization format is forbidden by default. You should either use another format (JSON or XML), or explicitly enable the Java serialization with the help of javaSerializationFormatEnabled configuration parameter.

Refer the below configuration:

<process-application
  xmlns="http://www.camunda.org/schema/1.0/ProcessApplication"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <process-archive>
    <resource>process.bpmn</resource>
    <properties>
      <property name="isDeleteUponUndeploy">false</property>
      <property name="isScanForProcessDefinitions">true</property>
      <property name="javaSerializationFormatEnabled">true</property>
    </properties>
  </process-archive>

</process-application>

Order.java:

@Getter
@Builder
@ToString
@AllArgsConstructor
public class Order implements Serializable {
  private static final long serialVersionUID = 1L;
  private final String orderId;
  private final String orderType;
  private final String orderName;
  private final double orderPrice;
}

ObjectSerializeDelegate.java:

@Component
public class ObjectSerializeDelegate implements JavaDelegate {

  @Override
  public void execute(DelegateExecution execution) throws Exception {
    Order order = Order.builder().orderId("OR3487").orderName("Laptop").orderPrice(80000.00).orderType("Electronics")
        .build();
    execution.setVariable("orderDataJava",
        Variables.objectValue(order).serializationDataFormat(Variables.SerializationDataFormats.JAVA).create());
  }
}

ObjectDeserializeDelegate.java:

@Slf4j
@Component
public class ObjectDeserializeDelegate implements JavaDelegate {

  @Override
  public void execute(DelegateExecution execution) throws Exception {
    ObjectValue typedObjectOrderValue = execution.getVariableTyped("orderDataJava");
    log.info("Type name:{}, SerializationDataFormat:{}", typedObjectOrderValue.getObjectTypeName(),
        typedObjectOrderValue.getSerializationDataFormat());
    Order deserializedOrder = (Order) typedObjectOrderValue.getValue();
    log.info("deserializedOrder:{}", deserializedOrder);
  }
}

Logs:

2020-08-13 00:32:13.553  INFO 5324 --- [aTaskExecutor-2] c.e.w.d.ObjectDeserializeDelegate        : Type name:com.example.workflow.dto.Order, SerializationDataFormat:application/x-java-serialized-object
2020-08-13 00:32:13.554  INFO 5324 --- [aTaskExecutor-2] c.e.w.d.ObjectDeserializeDelegate        : deserializedOrder:Order(orderId=OR3487, orderType=Electronics, orderName=Laptop, orderPrice=80000.0)

image

thanks Arvind

I had similar approach but then i am assuming the variable across other process too i figured out the issue , when type casted from A to B , both A and B should be also in the similar package to get the serialized value back into string

vish

Check the processes.xml configuration

arvind,i am not able to pass the serialzied data object to sub process via call activity step, is there any specific format or way i should be using ?

vish

@vishucamunda refer the below example:

BPM File: callActivityExample.bpmn (8.0 KB)

<process-application
  xmlns="http://www.camunda.org/schema/1.0/ProcessApplication"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <process-archive>
    <resource>process.bpmn</resource>
    <properties>
      <property name="isDeleteUponUndeploy">false</property>
      <property name="isScanForProcessDefinitions">true</property>
      <property name="javaSerializationFormatEnabled">true</property>
    </properties>
  </process-archive>

</process-application>

@Slf4j
@Component
public class ObjectDeserializeDelegate implements JavaDelegate {

  @Override
  public void execute(DelegateExecution execution) throws Exception {
    ObjectValue typedObjectOrderValue = execution.getVariableTyped("orderDataJava");
    log.info("Type name:{}, SerializationDataFormat:{}", typedObjectOrderValue.getObjectTypeName(),
        typedObjectOrderValue.getSerializationDataFormat());
    Order deserializedOrder = (Order) typedObjectOrderValue.getValue();
    log.info("deserializedOrder:{}", deserializedOrder);
  }

}

@Component
public class ObjectSerializeDelegate implements JavaDelegate {

  @Override
  public void execute(DelegateExecution execution) throws Exception {
    Order order = Order.builder().orderId("OR3487").orderName("Laptop").orderPrice(80000.00).orderType("Electronics")
        .build();
    ObjectValue orderTypedObjectValue = Variables.objectValue(order)
        .serializationDataFormat(Variables.SerializationDataFormats.JAVA).create();
    execution.setVariable("orderDataJava", orderTypedObjectValue);
  }

}

@Getter
@Builder
@ToString
@AllArgsConstructor
public class Order implements Serializable {

  private static final long serialVersionUID = 1L;

  private final String orderId;

  private final String orderType;

  private final String orderName;

  private final double orderPrice;

}

thanks arvind , infact i had tried all this
variables.put(WorkerProperties.SOME_VALUE,
Variables.objectValue(headerObjectValues).
serializationDataFormat(Variables.SerializationDataFormats.JSON).
create());

and then i use external task not delegate and the task gets deleted before the call activity is called

@vishucamunda provide your bpmn model here

Just for the record, this issue was resolved after Java beans were packaged in JAR, and placed in camunda-bpm-run-ee-7.13.1-ee/configuration/userlib.

The cause that for this issue was:
When call-activity tries to map the process variables, it tries to get Typed value of process variable. And at that point it fails to deserialize it as it can not find the class in the context of process engine (where process is deployed).