Subj : How to run multiple instances of a script - code attached...? To : netscape.public.mozilla.jseng From : redcoat Date : Thu Jan 20 2005 03:42 pm Hi, Connected to my previous question about the error I'm seeing, I thought I'd check if anyone with experience of running the same script in multiple threads would mind checking my code. It's based off the tutorial on shared scopes, so am perplexed as to why it's not working, and stuck as to how to proceed! Does anyone see any problem? Please see my previous message for strange error I'm seeing: http://groups-beta.google.com/group/netscape.public.mozilla.jseng/browse_thread/thread/c50e8edc6a80dd05/e954ef415255cc3d?_done=%2Fgroup%2Fnetscape.public.mozilla.jseng%3F&_doneTitle=Back+to+topics&_doneTitle=Back&&d#e954ef415255cc3d So, any help is MUCH appreciated! cheers, David -------------------------------------------- ScriptEngine.java: -------------------------------------------- package scripting; import java.io.FileReader; import java.io.IOException; import org.mozilla.javascript.Context; import org.mozilla.javascript.ImporterTopLevel; import org.mozilla.javascript.Script; import org.mozilla.javascript.Scriptable; import client.Client; /** * The ScriptEngine Action module * * @author dhay */ public class ScriptEngine { private static Scriptable sharedScope; static { Context cx = Context.enter(); try { //we call this instead of initStandardObjects() so we have // access to importClass etc. in our scripts sharedScope = new ImporterTopLevel(cx); //add any other shared objects here } finally { Context.exit(); } } public ScriptEngine() {} /* * Execute our script * */ public static String executeScript(Client client, String scriptName) throws Exception { // Associate a new Context with this thread Context cx = Context.enter(); try { // Initialize the standard objects (Object, Function, etc.) // This must be done before scripts can be executed. cx.initStandardObjects(); Thread scriptThread = new Thread(new ScriptThread(sharedScope, scriptName, client)); scriptThread.start(); return scriptThread.toString(); //scriptThread.join(); } finally { Context.exit(); } } public static Script compileScript(String fileName) throws IOException { FileReader r = new FileReader(fileName); try { Context cx = Context.enter(); try { return cx.compileReader(r, fileName, 1, null); } finally { Context.exit(); } } finally { r.close(); } } } ------------------------------------------------ ScriptThread.java ------------------------------------------------ package scripting; import java.io.IOException; import org.mozilla.javascript.Context; import org.mozilla.javascript.EvaluatorException; import org.mozilla.javascript.JavaScriptException; import org.mozilla.javascript.Script; import org.mozilla.javascript.Scriptable; import client.Client; /** * * @author dhay */ public class ScriptThread implements Runnable { private Scriptable _sharedScope; private Script _script; private Client _client; /** * Constructor * * @param sharedScope * @param script * @throws IOException */ ScriptThread(Scriptable sharedScope, String script, Client client) throws IOException { _sharedScope = sharedScope; _script = ScriptEngine.compileScript(script); _client = client; } /** * Do our stuff */ public void run() { //our context Context cx = Context.enter(); try { //share the shared scope Scriptable threadScope = cx.newObject(_sharedScope); threadScope.setPrototype(_sharedScope); //set as new top-level scope (any variables created // by assignments will be properties of this scope threadScope.setParentScope(null); //add client threadScope.put("client", threadScope, Context.javaToJS(_client, threadScope)); //execute script _script.exec(cx, threadScope); } catch (EvaluatorException e) { e.printStackTrace(); throw new ScriptRuntimeException("Error executing script: " + _script.getClass().getName(), e); } catch (JavaScriptException e) { e.printStackTrace(); throw new ScriptRuntimeException("Error executing script: " + _script.getClass().getName(), e); } finally { Context.exit(); } } } .