Can i turn off serialization if i don't need process variables to be persisited to database?

camunda version: 7.15.0

I try to use camunda as an embedded process engine, i found when i set a process variable, it will be serialized immedately after delegate.setVariable is invoked.

What i expected is to turn off serialization, just pass reference.

  • org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntityFactory#build will serialize process variables.
  • org.camunda.bpm.engine.impl.core.variable.scope.SimpleVariableInstance.SimpleVariableInstanceFactory#build will not serialize process variables.

I failed to find any config items to switch VariableInstanceEntityFactory to SimpleVariableInstanceFactory.

I don’t need those process varibles to be persisted to database.

@Service
public class SelectUserByStateTask implements JavaDelegate {
    @Autowired
    private UserRepository userRepository;

    @Override
    public void execute(DelegateExecution delegate) throws Exception {
        List<User> users = userRepository.findUsersByStateNotLimitedTo("已处理", 500);
        delegate.setVariable("items", users);
    }
}
java.io.NotSerializableException: com.tankilo.demo.dto.User
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) ~[na:1.8.0_222-4-redhat]
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) ~[na:1.8.0_222-4-redhat]
	at java.util.ArrayList.writeObject(ArrayList.java:766) ~[na:1.8.0_222-4-redhat]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_222-4-redhat]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_222-4-redhat]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_222-4-redhat]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_222-4-redhat]
	at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1140) ~[na:1.8.0_222-4-redhat]
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496) ~[na:1.8.0_222-4-redhat]
	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) ~[na:1.8.0_222-4-redhat]
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) ~[na:1.8.0_222-4-redhat]
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) ~[na:1.8.0_222-4-redhat]
	at org.camunda.bpm.engine.impl.variable.serializer.JavaObjectSerializer.serializeToByteArray(JavaObjectSerializer.java:72) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.variable.serializer.AbstractSerializableValueSerializer.writeValue(AbstractSerializableValueSerializer.java:54) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.variable.serializer.AbstractSerializableValueSerializer.writeValue(AbstractSerializableValueSerializer.java:31) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.persistence.entity.util.TypedValueField.writeValue(TypedValueField.java:182) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.persistence.entity.util.TypedValueField.setValue(TypedValueField.java:142) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.<init>(VariableInstanceEntity.java:131) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.create(VariableInstanceEntity.java:150) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntityFactory.build(VariableInstanceEntityFactory.java:31) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntityFactory.build(VariableInstanceEntityFactory.java:25) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariableLocal(AbstractVariableScope.java:336) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariable(AbstractVariableScope.java:303) ~[camunda-engine-7.15.0.jar:7.15.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariable(AbstractVariableScope.java:284) ~[camunda-engine-7.15.0.jar:7.15.0]
	at com.tankilo.demo.service.SelectUserByStateTask.execute(SelectUserByStateTask.java:39) ~[classes/:na]

Hi @tankilo,

maybe just save the UserId as a String or Number in the process variable?

Hope this helps, Ingo

I solved this question on my own.

import org.camunda.bpm.engine.impl.variable.serializer.AbstractTypedValueSerializer;
import org.camunda.bpm.engine.impl.variable.serializer.ValueFields;
import org.camunda.bpm.engine.variable.impl.value.UntypedValueImpl;
import org.camunda.bpm.engine.variable.type.ValueType;
import org.camunda.bpm.engine.variable.value.TypedValue;

public class NoopTypedValueSerializer extends AbstractTypedValueSerializer {

    public static final String NAME = "noop";

    public NoopTypedValueSerializer(ValueType type) {
        super(type);
    }

    @Override
    protected boolean canWriteValue(TypedValue value) {
        return true;
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public void writeValue(TypedValue value, ValueFields valueFields) {

    }

    @Override
    public TypedValue readValue(ValueFields valueFields, boolean deserializeValue, boolean isTransient) {
        return null;
    }

    @Override
    public TypedValue convertToTypedValue(UntypedValueImpl untypedValue) {
        return untypedValue;
    }
}
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        ................
        List<TypedValueSerializer> customPreVariableTypes = new ArrayList<>();
        customPreVariableTypes.add(new NoopTypedValueSerializer(ValueType.OBJECT));
        configuration.setCustomPreVariableSerializers(customPreVariableTypes);
        configuration.setDefaultSerializationFormat(null);
}

Currently, it works fine.