@Oktay_Ibis You can change the serialized Json string to collection like below:
SpinJsonNode spinJsonNode = Spin.JSON(execution.getVariable("pdcEvaluationGroupJson"));
//If the list values is type of primitives:
List<String> pdcEvaluationGroup = spinJsonNode.mapTo(List.class);
execution.setVariable("pdcEvaluationGroup",pdcEvaluationGroup);
//If type is custom object, and the java class doesn't exist in the classpath:
List<String> usersList = new ArrayList<>();
usersList.add(spinJsonNode.jsonPath("username").stringValue()); //extract username from the array
execution.setVariable("pdcEvaluationGroup",usersList);
//If type is custom object, and the java class exist in the classpath:
List<AuthUser> pdcEvaluationGroupList = spinJsonNode.mapTo(List.class);
List<String> usersList = new ArrayList<>();
pdcEvaluationGroupList.forEach(group->{usersList.add(group.getUsername())});
execution.setVariable("pdcEvaluationGroup",usersList);
Now use this variable βpdcEvaluationGroupβ as the collection variable for assignments.
Refer the below example:
BPMN File: voting.bpmn (3.7 KB)
External Worker configuration which sets the variable votersListJsonValue as serialized json value with type as Json:
@Slf4j
@Component
public class VotersPreparationWorker {
@Autowired
private Gson gsonParser;
@Autowired
private ExternalWorkerConfiguration externalWorkerConfiguration;
@EventListener(ApplicationReadyEvent.class)
public void handleExternalTasks() {
log.info("Initialzing the ExternalTaskServiceWorker with configuration:{}", externalWorkerConfiguration);
ExternalTaskClient externalTaskClient = ExternalTaskClient.create()
.baseUrl(externalWorkerConfiguration.getBaseUrl())
.addInterceptor(requestContext -> requestContext.addHeader("Authorization",
externalWorkerConfiguration.getAuthorization()))
.asyncResponseTimeout(1000).maxTasks(2).workerId("FETCH_APPROVERS").build();
TopicSubscription topicSubscription = externalTaskClient.subscribe("FETCH_APPROVERS")
.handler((externalTask, externalTaskService) -> {
List<Voters> votersList = new ArrayList<>();
votersList.add(Voters.builder().username("myself1").uniqueId("XC02334241").age(25).city("mycity1")
.email("myemail1").allowedToVote(true).build());
votersList.add(Voters.builder().username("myself2").uniqueId("XC02334242").age(25).city("mycity2")
.email("myemail2").allowedToVote(true).build());
JsonValue votersListJsonValue = ClientValues.jsonValue(gsonParser.toJson(votersList));
Map<String, Object> variables = new HashMap<>();
variables.put("votersListJsonValue", votersListJsonValue);
externalTaskService.complete(externalTask, variables);
log.info("ExternalTask {} with businessKey {} completed!!!", externalTask.getId(),
externalTask.getBusinessKey());
}).lockDuration(30000).open();
log.info("ExternalTaskServiceWorker has subscribed to the topic {} sucessfully",
topicSubscription.getTopicName());
}
}
Voter object which i kept part of the external task worker.
@Getter
@Builder
public class Voters implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String email;
private String city;
private int age;
private String uniqueId;
private boolean allowedToVote;
}
Now, the Voters object doesnβt present in the process application and I have the serialized json value for the list of Voters object.
I have configured the below Execution listener(PrepareVotersListListener) to the event type βtakeβ for the outgoing sequence from external task.
Now extracting the users list will be like:
@Slf4j
@Component
public class PrepareVotersListListener implements ExecutionListener {
@Override
public void notify(DelegateExecution execution) {
log.info("Executing PrepareVotersListListener.....");
SpinJsonNode spinJsonNode = Spin.JSON(execution.getVariable("votersListJsonValue"));
List<String> votersList = new ArrayList<>();
spinJsonNode.elements().forEach(elem ->{
String username = elem.jsonPath("username").stringValue();
log.info("Username of the voter:{}", username);
votersList.add(username);
});
execution.setVariable("votersList", votersList);
}
}
For using SpinJsonNode make sure below dependencies are in the classpath of process apllication:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-bom</artifactId>
<scope>import</scope>
<type>pom</type>
<version>1.9.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-core</artifactId>
</dependency>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-dataformat-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-dataformat-xml-dom</artifactId>
</dependency>
JSON Value inserted from worker for the Voters object like:
From process application, using spinnode the extracted users list value looks like:
Cockpit view of the process history: