Start process instance with javax.mail.Message object

Hi all, I want to start a process instance and add a javax.mail.Message object as a variable to it. Several attempts have failed:

  • to serialize it with Variables.objectValue and without any formatter ran into a StackOverflowException.
  • to serialize it with Variables.objectValue and json formatter ran into an exception
  • to serialize it with Variables.objectValue and java formatter fails because javax.mail.Message does not implement java.io.Serializable
  • to serialize it via ObjectOutputStream and ByteArrayOutputStream into byte[] failed at the deserialization because of a java.io.OptionalDataException
  • deserialization failed both if the message was serialized with Variables.bytesArrayValue and Variables.objectValue

Now I ran out of ideas. Has anybody ever managed to achieve that?

Many thanks in advance,
Christian

1 Like

If you could share the code snippet, that would be helpful.

sorry. This is the starter:

		try (ByteArrayOutputStream baos = new ByteArrayOutputStream())
		{
			try (ObjectOutputStream oos = new ObjectOutputStream(baos))
			{
				message.writeTo(oos);
			}
			byte[] byteArray = baos.toByteArray();
			logger.info("byte array has %d bytes", byteArray.length);
			Map<String, Object> variables = new HashMap<>();
			BytesValue bytesValue = Variables.byteArrayValue(byteArray, true);
			variables.put(VariableNames.MIME_MESSAGE, bytesValue);
			ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process_IntraDay", variables);
			logger.info("started process instance %s", processInstance.getProcessInstanceId());
		}
		catch (IOException | MessagingException exception)
		{
			throw new RuntimeException(exception);
		}

and this is the first JavaDelegate that wants to use the javax.mail.Message:

	@Override
	public void execute(DelegateExecution execution)
		throws Exception
	{
		logger.info("extract attachment from mail...");
		byte[] bytes = (byte[]) execution.getVariable(VariableNames.MIME_MESSAGE);
		logger.info("byte array has %d bytes", bytes.length);
		Message message = fromByteArray(bytes);
		...
	}

	private Message fromByteArray(byte[] bytes)
		throws ClassNotFoundException,
			IOException
	{
		try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes))
		{
			try (ObjectInputStream ois = new ObjectInputStream(bais))
			{
				Message result = (Message) ois.readObject();
				return result;
			}
		}
	}

ois.readObject gets the exception:

Caused by: java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1606) ~[?:?]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:430) ~[?:?]

Would you please try with serializing with Spin.

https://docs.camunda.org/manual/7.12/user-guide/data-formats/json/#serializing-process-variables

That was also something I had tried:

ObjectValue objectValue = Variables.objectValue(message, true).serializationDataFormat("application/json").create();
Map<String, Object> variables = new HashMap<>();
variables.put(VariableNames.MIME_MESSAGE, objectValue);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process_IntraDay", variables);

resulted in

org.camunda.bpm.engine.ProcessEngineException: Cannot serialize object in variable 'mimeMessage': SPIN/JACKSON-JSON-01009 Unable to map object 'com.sun.mail.imap.IMAPMessage@ddb0500' to json node
	at org.camunda.bpm.engine.impl.variable.serializer.AbstractSerializableValueSerializer.writeValue(AbstractSerializableValueSerializer.java:57) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.variable.serializer.AbstractSerializableValueSerializer.writeValue(AbstractSerializableValueSerializer.java:31) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.util.TypedValueField.writeValue(TypedValueField.java:182) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.util.TypedValueField.setValue(TypedValueField.java:142) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.<init>(VariableInstanceEntity.java:131) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.create(VariableInstanceEntity.java:150) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntityFactory.build(VariableInstanceEntityFactory.java:31) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntityFactory.build(VariableInstanceEntityFactory.java:25) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariableLocal(AbstractVariableScope.java:340) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariable(AbstractVariableScope.java:307) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariable(AbstractVariableScope.java:288) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.core.variable.VariableUtil.setVariables(VariableUtil.java:89) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.core.variable.scope.AbstractVariableScope.setVariables(AbstractVariableScope.java:248) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.start(PvmExecutionImpl.java:278) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.start(ExecutionEntity.java:458) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.start(PvmExecutionImpl.java:263) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:66) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:38) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:28) ~[camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:110) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:46) [camunda-engine-spring-7.14.0.jar:7.14.0]
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) [spring-tx-5.3.2.jar:5.3.2]
	at org.camunda.bpm.engine.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:44) [camunda-engine-spring-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:70) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.interceptor.CommandCounterInterceptor.execute(CommandCounterInterceptor.java:35) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.executeWithVariablesInReturn(ProcessInstantiationBuilderImpl.java:166) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:132) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:128) [camunda-engine-7.14.0.jar:7.14.0]
	at org.camunda.bpm.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:109) [camunda-engine-7.14.0.jar:7.14.0]
	at com.eon.flink.misc.gpfc.cz.util.process.IntradayStarter.onApplicationEvent(IntradayStarter.java:39) [classes/:?]
	at com.eon.flink.misc.gpfc.cz.util.process.IntradayStarter.onApplicationEvent(IntradayStarter.java:1) [classes/:?]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.3.2.jar:5.3.2]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.3.2.jar:5.3.2]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.3.2.jar:5.3.2]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:426) [spring-context-5.3.2.jar:5.3.2]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) [spring-context-5.3.2.jar:5.3.2]