[ 
https://issues.apache.org/jira/browse/NIFI-8625?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17351419#comment-17351419
 ] 

KevinSky edited comment on NIFI-8625 at 5/26/21, 1:34 AM:
----------------------------------------------------------

hi [#Otto Fowler], you can refer that :

https://issues.apache.org/jira/browse/NIFI-8461?focusedCommentId=17349069&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17349069

Here is a better solution:
 
nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/script/impl/JythonScriptEngineConfigurator.java
{code:java}
package org.apache.nifi.script.impl;import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processors.script.ScriptEngineConfigurator;
import org.python.core.PyString;import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;/**
 * A helper class to configure the Jython engine with any specific requirements
 */
public class JythonScriptEngineConfigurator implements ScriptEngineConfigurator 
{    private final Map<ScriptEngine, CompiledScript> compiledScriptMap = new 
ConcurrentHashMap<>();    private String prefix = null;    @Override
    public String getScriptEngineName() {
        return "python";
    }    @Override
    public URL[] getModuleURLsForClasspath(String[] modulePaths, ComponentLog 
log) {
        // We don't need to add the module paths to the classpath, they will be 
added via sys.path.append
        return new URL[0];
    }    @Override
    public Object init(ScriptEngine engine, String scriptBody, String[] 
modulePaths) throws ScriptException {
        if (engine == null) {
            return null;
        }        CompiledScript compiled = compiledScriptMap.get(engine);
        if (compiled != null) {
            return compiled;
        }        // Add prefix for import sys and all jython modules
        if (prefix == null) {
            prefix = "import sys\n"
                    + Arrays.stream(modulePaths).map((modulePath) -> 
"sys.path.append(" + PyString.encode_UnicodeEscape(modulePath, true) + ")")
                    .collect(Collectors.joining("\n"));
        }        compiled = ((Compilable) engine).compile(prefix + scriptBody);
        compiledScriptMap.put(engine, compiled);
        return compiled;
    }    @Override
    public Object eval(ScriptEngine engine, String scriptBody, String[] 
modulePaths) throws ScriptException {
        if (engine == null) {
            return null;
        }        CompiledScript compiled = compiledScriptMap.get(engine);
        if (compiled == null) {
            throw new ScriptException("Jython script has not been compiled, the 
processor must be restarted.");
        }        return 
compiled.eval(engine.getBindings(ScriptContext.ENGINE_SCOPE));
    }
}{code}
nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/script/ScriptingComponentHelper.java
{code:java}
    public void stop() {
        if (engineQ != null) {
            engineQ.clear();
        }        scriptEngineConfiguratorMap.clear();
    }{code}


was (Author: kevinsky1093):
hi [#Otto Fowler],

Here is a better solution:
 
nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/script/impl/JythonScriptEngineConfigurator.java
{code:java}
package org.apache.nifi.script.impl;import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processors.script.ScriptEngineConfigurator;
import org.python.core.PyString;import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;/**
 * A helper class to configure the Jython engine with any specific requirements
 */
public class JythonScriptEngineConfigurator implements ScriptEngineConfigurator 
{    private final Map<ScriptEngine, CompiledScript> compiledScriptMap = new 
ConcurrentHashMap<>();    private String prefix = null;    @Override
    public String getScriptEngineName() {
        return "python";
    }    @Override
    public URL[] getModuleURLsForClasspath(String[] modulePaths, ComponentLog 
log) {
        // We don't need to add the module paths to the classpath, they will be 
added via sys.path.append
        return new URL[0];
    }    @Override
    public Object init(ScriptEngine engine, String scriptBody, String[] 
modulePaths) throws ScriptException {
        if (engine == null) {
            return null;
        }        CompiledScript compiled = compiledScriptMap.get(engine);
        if (compiled != null) {
            return compiled;
        }        // Add prefix for import sys and all jython modules
        if (prefix == null) {
            prefix = "import sys\n"
                    + Arrays.stream(modulePaths).map((modulePath) -> 
"sys.path.append(" + PyString.encode_UnicodeEscape(modulePath, true) + ")")
                    .collect(Collectors.joining("\n"));
        }        compiled = ((Compilable) engine).compile(prefix + scriptBody);
        compiledScriptMap.put(engine, compiled);
        return compiled;
    }    @Override
    public Object eval(ScriptEngine engine, String scriptBody, String[] 
modulePaths) throws ScriptException {
        if (engine == null) {
            return null;
        }        CompiledScript compiled = compiledScriptMap.get(engine);
        if (compiled == null) {
            throw new ScriptException("Jython script has not been compiled, the 
processor must be restarted.");
        }        return 
compiled.eval(engine.getBindings(ScriptContext.ENGINE_SCOPE));
    }
}{code}
nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/script/ScriptingComponentHelper.java
{code:java}
    public void stop() {
        if (engineQ != null) {
            engineQ.clear();
        }        scriptEngineConfiguratorMap.clear();
    }{code}

> ExecuteScript processor always stuck after restart or multi thread
> ------------------------------------------------------------------
>
>                 Key: NIFI-8625
>                 URL: https://issues.apache.org/jira/browse/NIFI-8625
>             Project: Apache NiFi
>          Issue Type: Bug
>          Components: Core Framework
>    Affects Versions: 1.13.2
>            Reporter: KevinSky
>            Priority: Critical
>              Labels: ExecuteScript,stcuk
>         Attachments: executeScript_nifi_8625.xml, 
> image-2021-05-21-16-22-34-775.png
>
>
> In single thread, executeScript just stop and start, flow file always stuck 
> in connections.
> In multi thread.executeScript always throw exception like is already marked 
> for transfer in or is not known in this session.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to