The following code calls variable()
method to set up the variable in the process instance, and the data type is “Object”.
import org.flowable.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderProcessService implements OrderProcessInterface
{
@Autowired
private RuntimeService runtimeService;
@Override
public void triggerOrderProcess(final OrderDetails body)
{
final OrderProcessData orderProcessData = new OrderProcessData();
orderProcessData.setHasPostpaidEntries(body.isHasPostpaidEntries());
runtimeService.createProcessInstanceBuilder()
.processDefinitionKey("simpleProcess")
.variable("orderId", body.getOrderId())
.variable("orderProcessData", orderProcessData)
.start();
}
}
The problem is, when I try to retrieve the value using the following code snippet of the task (Delegate class), I have to cast the Object back to String. Is there an elegant way, such as generic type, to deal with type safety problem? (viz. to avoid ClassCastException)
@Override
public void execute(final DelegateExecution execution)
{
logger.info("Started {}", LocalDateTime.now());
final String orderId = (String) execution.getVariable("orderId");