use of org.apache.hop.workflow.IActionListener in project hop by apache.
the class LocalWorkflowEngine method startExecution.
@Override
public Result startExecution() {
if (!(workflowRunConfiguration.getEngineRunConfiguration() instanceof LocalWorkflowRunConfiguration)) {
log.logError("Error starting workflow", new HopException("A local workflow execution expects a local workflow configuration, not an instance of class " + workflowRunConfiguration.getEngineRunConfiguration().getClass().getName()));
result = new Result();
result.setNrErrors(1L);
return result;
}
LocalWorkflowRunConfiguration config = (LocalWorkflowRunConfiguration) workflowRunConfiguration.getEngineRunConfiguration();
// See if we need to enable transactions...
//
IExtensionData parentExtensionData = getParentPipeline();
if (parentExtensionData == null) {
parentExtensionData = getParentWorkflow();
}
String connectionGroup = null;
if (parentExtensionData != null && parentExtensionData.getExtensionDataMap() != null) {
connectionGroup = (String) parentExtensionData.getExtensionDataMap().get(Const.CONNECTION_GROUP);
}
//
if (config.isTransactional() && connectionGroup == null) {
// Store a value in the parent...
//
connectionGroup = getWorkflowMeta().getName() + " - " + UUID.randomUUID();
// We also need to commit/rollback at the end of this workflow...
//
addWorkflowFinishedListener(workflow -> {
String group = (String) workflow.getExtensionDataMap().get(Const.CONNECTION_GROUP);
List<Database> databases = DatabaseConnectionMap.getInstance().getDatabases(group);
Result result = workflow.getResult();
for (Database database : databases) {
//
try {
if (result.getResult() && !result.isStopped() && result.getNrErrors() == 0) {
try {
database.commit(true);
workflow.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were committed at the end of the workflow!");
} catch (HopDatabaseException e) {
workflow.getLogChannel().logError("Error committing database connection " + database.getDatabaseMeta().getName(), e);
result.setNrErrors(result.getNrErrors() + 1);
}
} else {
// Error? Rollback!
try {
database.rollback(true);
workflow.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were rolled back at the end of the workflow!");
} catch (HopDatabaseException e) {
workflow.getLogChannel().logError("Error rolling back database connection " + database.getDatabaseMeta().getName(), e);
result.setNrErrors(result.getNrErrors() + 1);
}
}
} finally {
// Always close connection!
try {
database.closeConnectionOnly();
workflow.getLogChannel().logDebug("Database connection '" + database.getDatabaseMeta().getName() + "' closed successfully!");
} catch (HopDatabaseException hde) {
workflow.getLogChannel().logError("Error disconnecting from database - closeConnectionOnly failed:" + Const.CR + hde.getMessage());
workflow.getLogChannel().logError(Const.getStackTracker(hde));
}
// Definitely remove the connection reference the connections map
DatabaseConnectionMap.getInstance().removeConnection(group, null, database);
}
}
});
}
//
if (connectionGroup != null && getExtensionDataMap() != null) {
// Set the connection group for this workflow
//
getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
}
// Pass down the value of the connection group value to actions before they're executed...
//
addActionListener(new IActionListener() {
@Override
public void beforeExecution(IWorkflowEngine workflow, ActionMeta actionMeta, IAction action) {
String connectionGroup = (String) workflow.getExtensionDataMap().get(Const.CONNECTION_GROUP);
if (connectionGroup != null) {
action.getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
}
}
@Override
public void afterExecution(IWorkflowEngine workflow, ActionMeta actionMeta, IAction action, Result result) {
// Nothing
}
});
return super.startExecution();
}
Aggregations