Harsha,

This is a feature/bug related to the difference between the lifecycle
events handled by InvokeScriptedProcessor (ISP) itself and the
scripted processor to which some lifecycle events get delegated.

For normal processors (including ISP), the initialize() method is
called when the processor is instantiated. We can't delegate to the
scripted processor, as the properties are not usually populated at
that time (unless you're instantiating a template maybe). The closest
we can get is when the processor is (re)loaded from the file or Script
Body property. Ideally what would happen is that when ISP is stopped,
we remove the reference to the existing scripted processor (which we
do) and when the processor is started, a new instance would be loaded
from the script, and initialize called on it at that time.  However,
asynchronously (and periodically) the ISP processor gets
customValidate() called upon it, and we try to delegate to the
scripted processor's validate() method. To do that we need to load the
script at that time, and then we call initialize on it. So technically
initialize is not being called when the processor is stopped, but it
is being marked as "needs to be reloaded", then whenever
customValidate is called, initialize is called as a result.

You can see that the two calls to initialize() are on different
instances by changing your output line in your scripted initialize()
method to:

log.info("Inside initialize() method for ${this.toString()}")

In logs/nifi-app.log you can see that they have different Object IDs.

Not to say that this is the "correct" behavior, but it may be more of
a feature than a bug, as initialize does only get called once, albeit
at a possibly awkward time.

Regards,
Matt

On Thu, Feb 14, 2019 at 9:00 AM Sri Harsha Chavali
<[email protected]> wrote:
>
> Hi All,
>
> I have a question regarding the behavior I'm noticing in 
> InvokeScriptedProcessor. I was testing a functionality and noticed that 
> initialize method in InvokeScriptedProcessor is called twice during the life 
> cycle of one full run. To reproduce this I kept simple print statement in the 
> initialize method and saw the statement being printed twice.
>
> First time when the processor is setup and my custom code is validated
> Second time after one execution is completed and the processor is stopped.
>
> See my sample code attached. I see the below statements being printed:
>
> 2019-02-14 08:48:30,585 INFO [NiFi logging handler] org.apache.nifi.StdOut 
> Inside initialize() method
> 2019-02-14 08:48:31,967 INFO [NiFi logging handler] org.apache.nifi.StdOut 
> Reading Flowfile onTrigger
> 2019-02-14 08:48:33,959 INFO [NiFi logging handler] org.apache.nifi.StdOut 
> Inside initialize() method
>
> Is this expected that initialize method is called when the processor is 
> stopped? I was under the assumption that initialize method is only called 
> during the processor setup. Please help.
>
> Thank you,
> Harsha
>
> Sent from Outlook

Reply via email to