pro.taskana.adapter.impl.TaskanaTaskStarter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taskana-adapter Show documentation
Show all versions of taskana-adapter Show documentation
Taskana Adapter for executing tasks from external systems
The newest version!
package pro.taskana.adapter.impl;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import pro.taskana.adapter.exceptions.TaskCreationFailedException;
import pro.taskana.adapter.manager.AdapterManager;
import pro.taskana.adapter.systemconnector.api.ReferencedTask;
import pro.taskana.adapter.systemconnector.api.SystemConnector;
import pro.taskana.adapter.taskanaconnector.api.TaskanaConnector;
import pro.taskana.task.api.exceptions.TaskAlreadyExistException;
import pro.taskana.task.api.models.Task;
/** Retrieves tasks in an external system and starts corresponding tasks in TASKANA. */
@Component
public class TaskanaTaskStarter {
private static final Logger LOGGER = LoggerFactory.getLogger(TaskanaTaskStarter.class);
@Value("${taskana.adapter.run-as.user}")
protected String runAsUser;
@Autowired AdapterManager adapterManager;
@Scheduled(
fixedRateString =
"${taskana.adapter.scheduler.run.interval.for.start.taskana.tasks.in.milliseconds:5000}")
public void retrieveNewReferencedTasksAndCreateCorrespondingTaskanaTasks() {
if (!adapterIsInitialized()) {
return;
}
synchronized (TaskanaTaskStarter.class) {
if (!adapterManager.isInitialized()) {
return;
}
LOGGER.debug(
"-retrieveNewReferencedTasksAndCreateCorrespondingTaskanaTasks started---------------");
try {
UserContext.runAsUser(
runAsUser,
() -> {
retrieveReferencedTasksAndCreateCorrespondingTaskanaTasks();
return null;
});
} catch (Exception ex) {
LOGGER.error(
"Caught exception while trying to create Taskana tasks from referenced tasks", ex);
}
}
}
public void retrieveReferencedTasksAndCreateCorrespondingTaskanaTasks() {
LOGGER.trace(
"TaskanaTaskStarter.retrieveReferencedTasksAndCreateCorrespondingTaskanaTasks ENTRY ");
for (SystemConnector systemConnector : (adapterManager.getSystemConnectors().values())) {
try {
List tasksToStart = systemConnector.retrieveNewStartedReferencedTasks();
List newCreatedTasksInTaskana =
createAndStartTaskanaTasks(systemConnector, tasksToStart);
systemConnector.taskanaTasksHaveBeenCreatedForNewReferencedTasks(newCreatedTasksInTaskana);
} finally {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(
String.format(
"TaskanaTaskStarter.retrieveReferencedTasksAndCreateCorrespondingTaskanaTasks "
+ "Leaving handling of new tasks for System Connector %s",
systemConnector.getSystemUrl()));
}
}
}
}
public void createTaskanaTask(
ReferencedTask referencedTask, TaskanaConnector connector, SystemConnector systemConnector)
throws TaskCreationFailedException {
LOGGER.trace("TaskanaTaskStarter.createTaskanaTask ENTRY ");
referencedTask.setSystemUrl(systemConnector.getSystemUrl());
addVariablesToReferencedTask(referencedTask, systemConnector);
Task taskanaTask = connector.convertToTaskanaTask(referencedTask);
connector.createTaskanaTask(taskanaTask);
LOGGER.trace("TaskanaTaskStarter.createTaskanaTask EXIT ");
}
private List createAndStartTaskanaTasks(
SystemConnector systemConnector, List tasksToStart) {
List newCreatedTasksInTaskana = new ArrayList<>();
for (ReferencedTask referencedTask : tasksToStart) {
try {
createTaskanaTask(referencedTask, adapterManager.getTaskanaConnector(), systemConnector);
newCreatedTasksInTaskana.add(referencedTask);
} catch (TaskCreationFailedException e) {
if (e.getCause() instanceof TaskAlreadyExistException) {
newCreatedTasksInTaskana.add(referencedTask);
} else {
LOGGER.warn(
"caught Exception when attempting to start TaskanaTask for referencedTask {}",
referencedTask,
e);
systemConnector.taskanaTaskFailedToBeCreatedForNewReferencedTask(referencedTask, e);
systemConnector.unlockEvent(referencedTask.getOutboxEventId());
}
} catch (Exception e) {
LOGGER.warn(
"caught unexpected Exception when attempting to start TaskanaTask "
+ "for referencedTask {}",
referencedTask,
e);
systemConnector.taskanaTaskFailedToBeCreatedForNewReferencedTask(referencedTask, e);
systemConnector.unlockEvent(referencedTask.getOutboxEventId());
}
}
return newCreatedTasksInTaskana;
}
private void addVariablesToReferencedTask(
ReferencedTask referencedTask, SystemConnector connector) {
if (referencedTask.getVariables() == null) {
String variables = connector.retrieveReferencedTaskVariables(referencedTask.getId());
referencedTask.setVariables(variables);
}
}
private boolean adapterIsInitialized() {
synchronized (AdapterManager.class) {
if (!adapterManager.isInitialized()) {
adapterManager.init();
return false;
}
return true;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy