Search in sources :

Example 1 with JobCloudEvent

use of org.kie.kogito.jobs.api.event.JobCloudEvent in project kogito-runtimes by kiegroup.

the class JobCloudEventDeserializer method deserialize.

public JobCloudEvent<?> deserialize(byte[] data) {
    try {
        CloudEvent cloudEvent = objectMapper.readValue(data, CloudEvent.class);
        CloudEventData cloudEventData = Objects.requireNonNull(cloudEvent.getData(), "JobCloudEvent data field must not be null");
        if (cloudEvent.getType().equals(CreateProcessInstanceJobRequestEvent.CREATE_PROCESS_INSTANCE_JOB_REQUEST)) {
            Job job = objectMapper.readValue(cloudEventData.toBytes(), Job.class);
            return CreateProcessInstanceJobRequestEvent.builder().withValuesFrom(cloudEvent).withContextFrom(cloudEvent).job(job).build();
        } else if (cloudEvent.getType().equals(CancelJobRequestEvent.CANCEL_JOB_REQUEST)) {
            CancelJobRequestEvent.JobId jobId = objectMapper.readValue(cloudEventData.toBytes(), CancelJobRequestEvent.JobId.class);
            return CancelJobRequestEvent.builder().withValuesFrom(cloudEvent).withContextFrom(cloudEvent).jobId(jobId.getId()).build();
        }
        throw new DeserializationException("Unknown JobCloudEvent event type: " + cloudEvent.getType());
    } catch (IOException e) {
        throw new DeserializationException("An error was produced during a JobCloudEvent deserialization: " + e.getMessage(), e);
    }
}
Also used : CloudEventData(io.cloudevents.CloudEventData) IOException(java.io.IOException) Job(org.kie.kogito.jobs.api.Job) CloudEvent(io.cloudevents.CloudEvent) JobCloudEvent(org.kie.kogito.jobs.api.event.JobCloudEvent)

Aggregations

CloudEvent (io.cloudevents.CloudEvent)1 CloudEventData (io.cloudevents.CloudEventData)1 IOException (java.io.IOException)1 Job (org.kie.kogito.jobs.api.Job)1 JobCloudEvent (org.kie.kogito.jobs.api.event.JobCloudEvent)1