use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.
the class ProcessInstanceDuplicatedExceptionMapper method toResponse.
@Override
public Response toResponse(ProcessInstanceDuplicatedException ex) {
ProcessInstanceDuplicatedException exception = (ProcessInstanceDuplicatedException) ex;
Map<String, String> response = new HashMap<>();
response.put(MESSAGE, exception.getMessage());
response.put(PROCESS_INSTANCE_ID, exception.getProcessInstanceId());
return conflict(response);
}
use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.
the class MapProcessInstances method importInstance.
@Override
public ProcessInstance importInstance(ExportedProcessInstance instance, Process process) {
ProcessInstance imported = marshaller.importProcessInstance(instance, process);
if (exists(imported.id())) {
throw new ProcessInstanceDuplicatedException(imported.id());
}
create(imported.id(), imported);
return imported;
}
use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.
the class $Type$MessageConsumer method consume.
public CompletionStage<Void> consume(Message<byte[]> msg) {
final String trigger = "$Trigger$";
try {
IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
if (useCloudEvents.orElse(true)) {
final $DataEventType$ eventData = json.readValue(msg.getPayload(), $DataEventType$.class);
final $Type$ model = new $Type$();
io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
String correlation = correlationEvent(eventData, msg);
if (correlation != null) {
LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
Optional possiblyFound = process.instances().findById(correlation);
if (possiblyFound.isPresent()) {
ProcessInstance pInstance = (ProcessInstance) possiblyFound.get();
LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
pInstance.send(Sig.of(trigger, eventData.getData()));
return null;
}
}
LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
try {
ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
pi.start(trigger, null, eventData.getData());
} catch (ProcessInstanceDuplicatedException e) {
ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
pi.send(Sig.of(trigger, eventData.getData()));
}
return null;
});
} else {
final $DataType$ eventData = json.readValue(msg.getPayload(), $DataType$.class);
final $Type$ model = new $Type$();
io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
String correlation = correlationPayload(eventData, msg);
if (correlation != null) {
LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
Optional possiblyFound = process.instances().findById(correlation);
if (possiblyFound.isPresent()) {
ProcessInstance pInstance = (ProcessInstance) possiblyFound.get();
LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
pInstance.send(Sig.of(trigger, eventData));
return null;
}
}
LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
try {
ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
pi.start(trigger, null, eventData);
} catch (ProcessInstanceDuplicatedException e) {
ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
pi.send(Sig.of(trigger, eventData));
}
return null;
});
}
return msg.ack();
} catch (Exception e) {
LOGGER.error("Error when consuming message for process {}", process.id(), e);
return msg.nack(e);
}
}
use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.
the class $Type$MessageConsumer method consume.
public CompletionStage<Void> consume(Message<String> msg) {
metrics.messageReceived(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
final String trigger = "$Trigger$";
try {
IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
final $DataType$ eventData;
final $Type$ model;
final String correlation;
LOGGER.debug("Received message with payload '{}'", msg.getPayload());
boolean accepted;
if (useCloudEvents.orElse(false)) {
$DataEventType$ event;
String contentType = appProperty(msg, "contentType");
model = new $Type$();
if (contentType != null && contentType.startsWith("application/cloudevents+json")) {
// structured
event = json.readValue(msg.getPayload(), $DataEventType$.class);
eventData = event.getData();
} else {
// binary
eventData = convert(msg, $DataType$.class);
event = new $DataEventType$(appProperty(msg, "ce_specversion"), appProperty(msg, "ce_id"), appProperty(msg, "ce_source"), appProperty(msg, "ce_type"), appProperty(msg, "ce_subject"), appProperty(msg, "ce_time"), eventData);
cloudEventsExtensions(msg, event);
}
correlation = correlationEvent(event, msg);
accepted = acceptedEvent(event, msg);
} else {
eventData = convert(msg, $DataType$.class);
model = new $Type$();
correlation = correlationPayload(eventData, msg);
accepted = acceptedPayload(eventData, msg);
}
if (!accepted) {
metrics.messageRejected(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
LOGGER.debug("Message has been rejected by filter expression");
return msg.ack();
}
io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
if (correlation != null) {
LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
Collection possiblyFound = process.instances().findByIdOrTag(io.automatiko.engine.api.workflow.ProcessInstanceReadMode.MUTABLE, correlation);
if (!possiblyFound.isEmpty()) {
metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
possiblyFound.forEach(pi -> {
ProcessInstance pInstance = (ProcessInstance) pi;
LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
pInstance.send(Sig.of(canStartInstance() ? trigger : "Message-" + trigger, eventData));
});
return null;
}
}
if (canStartInstance()) {
LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
try {
ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
pi.start(trigger, null, eventData);
} catch (ProcessInstanceDuplicatedException e) {
ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
pi.send(Sig.of(trigger, eventData));
}
} else {
metrics.messageMissed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
LOGGER.warn("Received message without reference id and no correlation is set/matched, for trigger not capable of starting new instance '{}'", trigger);
}
return null;
});
return msg.ack();
} catch (Exception e) {
metrics.messageFailed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
LOGGER.error("Error when consuming message for process {}", process.id(), e);
return msg.nack(e);
}
}
use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.
the class $Type$MessageConsumer method consume.
public CompletionStage<Void> consume(Message<?> msg) {
metrics.messageReceived(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
final String trigger = "$Trigger$";
try {
io.smallrye.reactive.messaging.kafka.KafkaRecord<?, ?> record = (io.smallrye.reactive.messaging.kafka.KafkaRecord<?, ?>) msg;
final $DataType$ eventData;
final $Type$ model;
final String correlation;
LOGGER.debug("Received message with key '{}' and payload '{}'", record.getKey(), msg.getPayload());
boolean accepted;
if (useCloudEvents.orElse(false)) {
$DataEventType$ event;
String contentType = header(record, "content-type");
model = new $Type$();
if (contentType != null && contentType.startsWith("application/cloudevents+json")) {
// structured
event = json.readValue(record.getPayload().toString(), $DataEventType$.class);
eventData = event.getData();
} else {
// binary
eventData = convert(record, $DataType$.class);
event = new $DataEventType$(header(record, "ce_specversion"), header(record, "ce_id"), header(record, "ce_source"), header(record, "ce_type"), header(record, "ce_subject"), header(record, "ce_time"), eventData);
cloudEventsExtensions(msg, event);
}
correlation = correlation(event, msg);
accepted = acceptedEvent(event, msg);
} else {
eventData = convert(msg, $DataType$.class);
model = new $Type$();
correlation = correlation(eventData, msg);
accepted = acceptedPayload(eventData, msg);
}
if (!accepted) {
metrics.messageRejected(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
LOGGER.debug("Message has been rejected by filter expression");
return msg.ack();
}
IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
if (correlation != null) {
LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
Collection possiblyFound = process.instances().findByIdOrTag(io.automatiko.engine.api.workflow.ProcessInstanceReadMode.MUTABLE, correlation);
if (!possiblyFound.isEmpty()) {
metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
possiblyFound.forEach(pi -> {
ProcessInstance pInstance = (ProcessInstance) pi;
LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
pInstance.send(Sig.of(canStartInstance() ? trigger : "Message-" + trigger, eventData));
});
return null;
}
}
if (canStartInstance()) {
LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
try {
ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
pi.start(trigger, null, eventData);
} catch (ProcessInstanceDuplicatedException e) {
ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
pi.send(Sig.of(trigger, eventData));
}
} else {
metrics.messageMissed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
LOGGER.warn("Received message without reference id and no correlation is set/matched, for trigger not capable of starting new instance '{}'", trigger);
}
return null;
});
return msg.ack();
} catch (Exception e) {
metrics.messageFailed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
LOGGER.error("Error when consuming message for process {}", process.id(), e);
return msg.nack(e);
}
}
Aggregations