- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
Creating multiple flow files and ExecuteScript error handling
- Labels:
-
Apache NiFi
Created 03-26-2019 01:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If I am in the process of creating multiple flow files during an ExecuteScript processor, and an error occurs before I'm done, I transfer the original flow file to its failure relationship. My doubt, however, is whether or not I have to do anything with the flow files that I've created thus far.
Consider, for example, the following groovy:
def flowFile = session.get()
if (!flowFile) return
def filesDir = flowFile.getAttribute('filesDirectory')
def flowFiles = [] as List<FlowFile>
try {
// Get a list of all the trades in the portfolio directory.
File folder = new File(filesDir);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
// Create a new flow file with that single trade.
def newFlowFile = session.create(flowFile)
flowFiles << newFlowFile
// put some attributes here, or do something that might throw an exception
}
}
}
catch (e) {
session.transfer(flowFile, REL_FAILURE)
return
}
session.remove(flowFile)
session.transfer(flowFiles, REL_SUCCESS)
My doubt is whether or not I need something like this in the "catch" block:
catch (e) {
// is this necessary?
for (FlowFile theFlowFile : flowFiles) {
session.remove(theFlowFile)
}
session.transfer(flowFile, REL_FAILURE)
return
}
...or will the session automatically clean them up on the basis that they haven't been directed to any relationship?
Created 03-26-2019 01:58 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Once a flow file has been created in a session, it must be removed or transferred before the session is committed (which happens at the end of ExecuteScript). Since your try is outside the loop that creates new flow files, you'll want to remove all the created ones, namely the flowFiles list. You can do that with simply:
session.remove(flowFiles)
rather than the loop you have in your catch statement.
Created 03-26-2019 01:58 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Once a flow file has been created in a session, it must be removed or transferred before the session is committed (which happens at the end of ExecuteScript). Since your try is outside the loop that creates new flow files, you'll want to remove all the created ones, namely the flowFiles list. You can do that with simply:
session.remove(flowFiles)
rather than the loop you have in your catch statement.
Created 03-27-2019 06:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, Matt.
Created 03-29-2019 07:15 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Maybe there is a spot somewhere this may be mentioned in your already-excellent ExecuteScript Cookbook articles for the next person?
Created 03-29-2019 07:20 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's a great idea, thanks! I've been meaning to update it, hopefully sooner than later 🙂
