I have a graph where I read data from a file. If there is no data read - I would like to stop execution of the remainder of the graph. Any ideas on how to do this ?
Hello,
you should check the file before running the graph and, if it is empty, don’t run such graph at all.
But if you persist on aborting graph by itself, it can be done in Reformat component. See following example class you can use in Reformat component:
import org.jetel.component.DataRecordTransform;
import org.jetel.data.DataRecord;
import org.jetel.exception.TransformException;
public class StopGraph extends DataRecordTransform {
int recNo=0;
public int transform(DataRecord[] arg0, DataRecord[] arg1)
throws TransformException {
defaultTransform(arg0, arg1);
recNo++;
return ALL;
}
public void finished() {
if (recNo == 0) {
System.err.println("There is no data. Killing the graph.");
getGraph().getWatchDog().abort();
}
}
}
In transform method you just copy input record to output record and in finish method you kill the running graph. The Reader and Reformat should be in earlier phase than the rest of graph. Note that such solution make your graph slower for not empty files while you have to split the graph into the phases and add one more Reformat.
Thanks for the abort logic:
getGraph().getWatchDog().abort();
There should definitely be an Abort component by default that does this
We actually do have a component called Fail now. It requires at least one record flowing into it to work but this can be worked around by using a dummy record.
Hi @imriskal,
Could a status code option be added to the fail component? Such as “End as Failed” or “End as Aborted”.
One example scenario I have is that I have a failed graph event listener that emails when something goes wrong. But there are times when I just want to abort the graph without triggering an “error”.
An option to just end the graph as “Success” would be helpful in this regard as well. Maybe an option to the existing Success component can be added that says “Cancel future phases and end graph after current phase finishes”.
Hi Mukunku,
the fail component is designed in a way that it can abort the parent jobflow or graph with a user-specified error message and at the same time return some results through the dictionary. Having said that, you could use the Fail component for both purposes. Let me present you with an example:
-
Adding a Status Code
[list:o3t2v0sj][*:o3t2v0sj]You would place Fail as a standalone component in the very last phase of your jobflow. -
You could assign a user-specified error message, “End as Aborted” for instance.
-
This would help you distinguish between a jobflow that ended by this intentional user abort and a jobflow that finished with an actual error.
[/*o3t2v0sj] -
Cancel future phases and end graph after current phase finishes
[*:o3t2v0sj]Let’s say you have at least 2 different phases in your jobflow. -
If you placed the Fail component in between those 2 existing phases, you could intentionally prevent the higher phase from being executed.
[/*o3t2v0sj][/list:o:o3t2v0sj]
Note: this could not be achieved by using the Success component as it does not function as a stopper and the graph/jobflow would simply continue to run.
Please also note that the aforementioned examples would answer just the basic questions. If they do not address your particular situation sufficiently, feel free to provide us with the graph in question so that we can get a better insight into the specific case. In similar situations, a minor redesign of a graph might do the trick.
Regards,