title: In nifi groovy script giving exception is already marked for transfer
body: <p>I am having the input payload <code>{"ENV": {"MAC": "6CECEB5D0302","NS": "NM","DATE": "170619","TIME": "114635","PM2.5": "10","PM10": "8.272681196722012"}}</code>.</p>
  
  <p>I have used the below script for creating multiple flowfiles and it worked fine. But same code not working for transferring one flowfile. In the below code based on the <code>if</code> code I wanted the script should either transfer flow to next processor or it should just log info and drop that flowfile.</p>
  
  <pre class="lang-groovy prettyprint-override"><code>import groovy.json.*;
  import org.apache.commons.io.IOUtils;
  import java.nio.charset.*;
  import java.lang.*;
  
  def flowFile = session.get();
  if (flowFile == null) {
      return;
  }
  def flowFiles = [] as List&lt;FlowFile&gt;
  flowFile = session.write(flowFile,
      { inputStream, outputStream -&gt;
          try{
              def data = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
              def input = new JsonSlurper().parseText( data );
              if( Double.parseDouble(input.ENV["PM2.5"])&gt;7.2) {
                  Map out =
                      [ENVIRONMENT: [
                        DATE: input.ENV.DATE,
                        "PM2.5": input.ENV["PM2.5"],
                        TIME: input.ENV.TIME,
                        DATE_TIME: input.ENV.DATE,
                        NS: input.ENV.NS,
                        MAC: input.ENV.MAC
                      ]];
                  String finalJson = JsonOutput.toJson out;
                  outputStream.write(finalJson.getBytes(StandardCharsets.UTF_8))
                  flowFiles &lt;&lt; flowFile
              } else {
                  log.info('else condition executed')
              }
          } catch(Exception e){}
      } as StreamCallback)
  session.transfer(flowFiles, REL_SUCCESS)
  session.remove(flowFile)
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>{"ENVIRONMENT":{"DATE":"170619","PM2.5":"10","TIME":"114635","DATE_TIME":"170619","NS":"NM","MAC":"6CECEB5D0302"}}
  </code></pre>
  
  <p>But if the input is having <code>PM2.5</code> less than 7.2, it should drop that flowfile.</p>
  
  <p>After removing empty try and catch exception log is:</p>
  
  <pre><code>2019-10-07 12:08:29,237 ERROR [Timer-Driven Process Thread-5] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=716b08ff-7a78-3806-bdd6-99d57cfd7cb7] Failed to process session due to org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: javax.script.ScriptException: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=c8217e1d-ec25-4017-a175-25ef12ad64ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1570308702876-1, container=default, section=1], offset=53731, length=114],offset=0,name=c8217e1d-ec25-4017-a175-25ef12ad64ba,size=114] is already marked for transfer: org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: javax.script.ScriptException: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=c8217e1d-ec25-4017-a175-25ef12ad64ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1570308702876-1, container=default, section=1], offset=53731, length=114],offset=0,name=c8217e1d-ec25-4017-a175-25ef12ad64ba,size=114] is already marked for transfer
  org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: javax.script.ScriptException: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=c8217e1d-ec25-4017-a175-25ef12ad64ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1570308702876-1, container=default, section=1], offset=53731, length=114],offset=0,name=c8217e1d-ec25-4017-a175-25ef12ad64ba,size=114] is already marked for transfer
      at org.apache.nifi.processors.script.ExecuteScript.onTrigger(ExecuteScript.java:248)
      at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1162)
      at org.apache.nifi.controller.tasks.ConnectableTask.invoke(ConnectableTask.java:209)
      at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:117)
      at org.apache.nifi.engine.FlowEngine$2.run(FlowEngine.java:110)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: javax.script.ScriptException: javax.script.ScriptException: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=c8217e1d-ec25-4017-a175-25ef12ad64ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1570308702876-1, container=default, section=1], offset=53731, length=114],offset=0,name=c8217e1d-ec25-4017-a175-25ef12ad64ba,size=114] is already marked for transfer
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:162)
      at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
      at org.apache.nifi.script.impl.GroovyScriptEngineConfigurator.eval(GroovyScriptEngineConfigurator.java:54)
      at org.apache.nifi.processors.script.ExecuteScript.onTrigger(ExecuteScript.java:232)
      ... 11 common frames omitted
  Caused by: javax.script.ScriptException: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=c8217e1d-ec25-4017-a175-25ef12ad64ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1570308702876-1, container=default, section=1], offset=53731, length=114],offset=0,name=c8217e1d-ec25-4017-a175-25ef12ad64ba,size=114] is already marked for transfer
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324)
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159)
      ... 14 common frames omitted
  Caused by: org.apache.nifi.processor.exception.FlowFileHandlingException: StandardFlowFileRecord[uuid=c8217e1d-ec25-4017-a175-25ef12ad64ba,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1570308702876-1, container=default, section=1], offset=53731, length=114],offset=0,name=c8217e1d-ec25-4017-a175-25ef12ad64ba,size=114] is already marked for transfer
      at org.apache.nifi.controller.repository.StandardProcessSession.validateRecordState(StandardProcessSession.java:3139)
      at org.apache.nifi.controller.repository.StandardProcessSession.validateRecordState(StandardProcessSession.java:3121)
      at org.apache.nifi.controller.repository.StandardProcessSession.remove(StandardProcessSession.java:1979)
      at org.apache.nifi.processor.ProcessSession$remove$3.call(Unknown Source)
      at Script107.run(Script107.groovy:40)
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321)
      ... 15 common frames omitted
  </code></pre>
  
  <p>Same code I used for generating one flowfile with multiple output this is working in both cases 1)if it satisfies condition it will generate the multiple output based on the input. 2)it'll drop if it not satisfies.:</p>
  
  <p>Input:</p>
  
  <pre><code> {
      "ENV": [{
              "MAC": "6CECEB5D0302",
              "NS": "NM",
              "DATE": "170619",
              "TIME": "114635",
              "PM2.5": "7.222410585417936",
              "PM10": "8.272681196722012"
          },
          {
              "MAC": "6CECEB5D0302",
              "NS": "NM",
              "DATE": "170619",
              "TIME": "114635",
              "PM2.5": "7.222410585417936",
              "PM10": "8.272681196722012"
          }
      ]
  }
  </code></pre>
  
  <p>The above input will generate two flowfile as 7.22... is greater than 7.2. If the value for <code>PM2.5</code> is less than 7.2 then it won't generate any transfer flowfile for next processor I mean it'll drop there itself.</p>
  
  <pre><code>import groovy.json.*;
  import org.apache.commons.io.IOUtils;
  import java.nio.charset.*;
  
  
  
  def flowFile = session.get()
  if(!flowFile) return
  def flowFiles = [] as List&lt;FlowFile&gt;
  def inputStream = session.read(flowFile)
  
  def writer = new StringWriter();
      try {
              IOUtils.copy(inputStream, writer, "UTF-8");
              data=writer.toString();
              inputStream.close();
              def input = new JsonSlurper().parseText( data );
              for (def i=0;i&lt;input.ENV.size();i++){
              if( Double.parseDouble(input.ENV[i]["PM2.5"])&gt;7.2) {
              def newFlowFile = session.create(flowFile);
  Map out =
  [ENVIRONMENT: [
    DATE: input.ENV[i].DATE,
    "PM2.5": input.ENV[i]["PM2.5"],
    TIME: input.ENV[i].TIME,
    DATE_TIME: input.ENV[i].DATE,
    NS: input.ENV[i].NS,
    MAC: input.ENV[i].MAC
  ]];String finalJson = JsonOutput.toJson out;
              newFlowFile = session.write(newFlowFile, { outputStream -&gt; 
                       outputStream.write( finalJson.getBytes(StandardCharsets.UTF_8) )
              } as OutputStreamCallback)
              flowFiles &lt;&lt; newFlowFile
              }
              else{
              }
          }} catch (IOException e) {
  
          }
  session.transfer(flowFiles, REL_SUCCESS)
  session.remove(flowFile)
  </code></pre>