Activiti自定义变量类型问题

时间:2013-11-20 12:24:39

标签: java activiti

在Activiti中,所有复杂对象都被序列化并使用Java序列化存储在数据库中。我希望为我的自定义类型覆盖此行为并将对象存储为JSON。这将帮助我更好地控制持久化对象。

我已创建自定义VariableType来执行此操作。以下是摘录

public class CustomVariableType extends ByteArrayType{
// overrided all the needed method.
}

这些类型在activiti配置impl中配置如下:

public class WorklistConfigurationService {

    private ProcessEngineConfigurationImpl processEngineConfigurationImpl;
    private List<CustomVariableType> customVarsTypes;
    public void init(){
        logger.debug("inside init");
        for(VOVariableType varType : voVars){
            logger.debug("adding type {}", varType.getTypeName());
            processEngineConfigurationImpl.getVariableTypes().addType(varType, 0);
        }
    }
   // getter and setters...
}

以下是我在Spring中将值注入上面的方法

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="worklistConfigurationService"
        class="...WorklistConfigurationService" init-method="init">
        <property name="customVarTypes">
            <list>
                <bean id="var1" class="...CustomVariableType">
                    <constructor-arg type="java.lang.String" value="custom" />
                    <constructor-arg type="java.lang.Class"
                        value="..CustomType" />
                </bean>
            </list>
        </property>
        <property name="processEngineConfigurationImpl" ref="processEngineConfiguration" />
    </bean>

但是新的序列化机制从未被activiti引擎使用。相反,它始终是使用的默认序列化。有人可以帮助解决问题。

1 个答案:

答案 0 :(得分:1)

我终于找到了问题的根本原因。在下面的DeserializedObject类中是代码

public class DeserializedObject {

  Object deserializedObject;
  byte[] originalBytes;
  VariableInstanceEntity variableInstanceEntity;

  public DeserializedObject(Object deserializedObject, byte[] serializedBytes, VariableInstanceEntity variableInstanceEntity) {
    this.deserializedObject = deserializedObject;
    this.originalBytes = serializedBytes;
    this.variableInstanceEntity = variableInstanceEntity;
  }

  public void flush() {
    // this first check verifies if the variable value was not overwritten with another object
    if (deserializedObject==variableInstanceEntity.getCachedValue() && !variableInstanceEntity.isDeleted()) {
      byte[] bytes = VOVariableType.serialize(deserializedObject, variableInstanceEntity);
      if (!Arrays.equals(originalBytes, bytes)) {
        variableInstanceEntity.setBytes(bytes);
      }
    }
  }
} 

这一行是问题的原因:

byte[] bytes = SerializableType.serialize(deserializedObject, variableInstanceEntity);

代码始终使用SerializableType来序列化对象。序列化默认为默认序列化。有一个问题......

我现在不知道如何进一步行动。