Can not receive complex object after message-process instantiation

Hello,

i’am trying to instantiate a processinstance with a message start event. This is my example: pizza_order.bpmn (9.9 KB)
The Java delegate, which is implementing “Order pizza”-sending task has to create a new processinstance with that order. The code is as follows:

public class SendPizzaOrder implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) throws Exception {
    RuntimeService runtimeService = delegateExecution.getProcessEngineServices().getRuntimeService();
    PizzaOrder pizzaOrder = new PizzaOrder("Pizza Funghi", 2);
   
    // serialize complex object
    ObjectValue pizzaOrderSerialized = Variables.objectValue(pizzaOrder)
            .serializationDataFormat(Variables.SerializationDataFormats.JSON) // also tried it with JAVA
            .create();

    VariableMap variablesToSend = Variables.createVariables()
            .putValueTyped("order", pizzaOrderSerialized); // I have also tried putValue

    // Set variable in order to compare it later with the delivered pizza
    delegateExecution.setVariable("order", pizzaOrderSerialized);
    Map<String, Object> variables = runtimeService.getVariables(delegateExecution.getProcessInstanceId());
    
    // Testing the deserialization, works fine)
    PizzaOrder order = (PizzaOrder) variables.get("order");

    // Start processinstance with message - works fine!
    runtimeService.startProcessInstanceByMessage("Pizzabestellung", variablesToSend);
}
}

This is my order Object:

public class PizzaOrder implements Serializable {
    private String pizzaname;
    private int numberOfPizzas;

public PizzaOrder(String pizzaname, int numberOfPizzas) {
    this.pizzaname = pizzaname;
    this.numberOfPizzas = numberOfPizzas;
}

public String getPizzaname() {
    return pizzaname;
}

public void setPizzaname(String pizzaname) {
    this.pizzaname = pizzaname;
}

public int getNumberOfPizzas() {
    return numberOfPizzas;
}

public void setNumberOfPizzas(int numberOfPizzas) {
    this.numberOfPizzas = numberOfPizzas;
}

}

The (process-vendor) processinstance is instantiated as i want, but i can’t get an access to the process variable. For simple types it is working well, but not for a complex type as the PizzaOrder object. When i try this:

Map<String, Object> variablesOfVerkauf = processEngine().getRuntimeService()
.getVariables(<process-vendor-processInstanceId>);

I’am getting the deserialization-error for both SerializationFormats (JSON, JAVA). As you can see in the delegate, the deserialization worked fine, but not in the newly created vendor-instance:

13:07:35.195 [main] ERROR org.camunda.bpm.engine.context - ENGINE-16004 Exception while closing command context: Cannot deserialize object in variable ‘order’: SPIN/JACKSON-JSON-01006 Cannot deserialize ‘{"pizzaname’ to java type ‘[simple type, class org.model.PizzaOrder]’
org.camunda.bpm.engine.ProcessEngineException: Cannot deserialize object in variable ‘order’: SPIN/JACKSON-JSON-01006 Cannot deserialize ‘{"pizzanam…’ to java type ‘[simple type, class org.model.PizzaOrder]’

What i’am doing wrong here?

Thanks a lot for your help! :slight_smile:

Hi @Andy,

try to add an empty Constructor on you order Class.

Hope this helps,

Ingo

Great it works! Thank you very much!