Class RingoWorker


  • public final class RingoWorker
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      RingoWorker​(RhinoEngine engine)
      Create a new ringo worker
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel​(java.util.concurrent.Future<?> future)
      Cancel a scheduled call.
      long countScheduledTasks()
      Count the number of scheduled calls in this worker.
      java.lang.Object evaluateScript​(org.mozilla.javascript.Context cx, ReloadableScript script, org.mozilla.javascript.Scriptable scope)
      Evaluate a script within a given scope.
      RhinoEngine getEngine()
      Get the worker's engine.
      org.mozilla.javascript.Function getErrorListener()
      Get the current error listener for uncaught errors in this worker
      java.util.List<ScriptError> getErrors()
      Get a list of errors encountered in the last invocation of this worker.
      java.lang.Object getProperty​(org.mozilla.javascript.Scriptable obj, java.lang.String name)  
      java.lang.Object invoke​(java.lang.Object module, java.lang.Object function, java.lang.Object... args)
      Invoke the function specified by `function` in module `module` with the given arguments on the current thread.
      boolean isActive()
      Returns true if this worker is currently running.
      boolean isReloading()
      Returns true if this worker will reload modified modules between invocations.
      org.mozilla.javascript.Scriptable loadModule​(org.mozilla.javascript.Context cx, java.lang.String moduleName, org.mozilla.javascript.Scriptable loadingScope)
      Load a Javascript module into a module scope.
      java.util.concurrent.Future<java.lang.Object> loadModuleInWorkerThread​(java.lang.String module)
      Load a module in the worker's event loop thread, returning a future for the loaded module.
      protected void registerModule​(Resource resource, org.mozilla.javascript.Scriptable module)
      Add a resource/scope pair to our map of known up-to-date modules.
      void release()
      Release the worker, returning it to the engine's worker pool.
      void releaseWhenDone()
      Schedule a task that will release this worker when the current task is finished, returning it back into the engine's worker pool.
      java.util.concurrent.ScheduledFuture<java.lang.Object> schedule​(long delay, java.lang.Object module, java.lang.Object function, java.lang.Object... args)
      Submit a function to be invoked on the worker's event loop thread with the given delay and arguments, returning a future for the result.
      java.util.concurrent.ScheduledFuture<?> scheduleInterval​(long interval, java.lang.Object module, java.lang.Object function, java.lang.Object... args)
      Submit a function to be invoked repeatedly with the given interval on the worker's event loop thread, returning a future for the result.
      void setErrorListener​(org.mozilla.javascript.Function errorListener)
      Set the error listener to handle uncaught errors in this worker
      void setReloading​(boolean reload)
      Enable or disable reloading of modified modules for this worker.
      void shutdown()
      Immediately shut down this worker's event loop.
      java.util.concurrent.Future<java.lang.Object> submit​(java.lang.Object module, java.lang.Object function, java.lang.Object... args)
      Submit a function to be invoked on the worker's event loop thread and return a future for the result.
      java.lang.String toString()
      Returns a string representation of the worker.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • RingoWorker

        public RingoWorker​(RhinoEngine engine)
        Create a new ringo worker
        Parameters:
        engine - the engine
    • Method Detail

      • toString

        public java.lang.String toString()
        Returns a string representation of the worker.
        Overrides:
        toString in class java.lang.Object
      • invoke

        public java.lang.Object invoke​(java.lang.Object module,
                                       java.lang.Object function,
                                       java.lang.Object... args)
                                throws java.lang.NoSuchMethodException,
                                       java.io.IOException

        Invoke the function specified by `function` in module `module` with the given arguments on the current thread. If this worker is currently busy running another thread this method will block until the other thread is done.

        Note that if module specifies a module id, the function is looked up in the top level module scope, not the module's exports object.

        Parameters:
        module - the module id or object
        function - the function name or object
        args - optional arguments to pass to the function
        Returns:
        the return value of the function invocation
        Throws:
        java.lang.NoSuchMethodException - if the function could not be found
        java.io.IOException - if loading the module caused an IO error
      • getProperty

        public java.lang.Object getProperty​(org.mozilla.javascript.Scriptable obj,
                                            java.lang.String name)
      • submit

        public java.util.concurrent.Future<java.lang.Object> submit​(java.lang.Object module,
                                                                    java.lang.Object function,
                                                                    java.lang.Object... args)

        Submit a function to be invoked on the worker's event loop thread and return a future for the result.

        This method always returns immediately.

        Parameters:
        module - the module id or object
        function - the function name or object
        args - optional arguments to pass to the function
        Returns:
        a future for the return value of the function invocation
      • schedule

        public java.util.concurrent.ScheduledFuture<java.lang.Object> schedule​(long delay,
                                                                               java.lang.Object module,
                                                                               java.lang.Object function,
                                                                               java.lang.Object... args)

        Submit a function to be invoked on the worker's event loop thread with the given delay and arguments, returning a future for the result.

        This method always returns immediately.

        Parameters:
        delay - the delay in milliseconds
        module - the module id or object
        function - the function name or object
        args - optional arguments to pass to the function
        Returns:
        a future for the return value of the function invocation
      • scheduleInterval

        public java.util.concurrent.ScheduledFuture<?> scheduleInterval​(long interval,
                                                                        java.lang.Object module,
                                                                        java.lang.Object function,
                                                                        java.lang.Object... args)

        Submit a function to be invoked repeatedly with the given interval on the worker's event loop thread, returning a future for the result.

        This method always returns immediately.

        Parameters:
        interval - the interval in milliseconds
        module - the module id or object
        function - the function name or object
        args - optional arguments to pass to the function
        Returns:
        a future for the return value of the function invocation
      • loadModuleInWorkerThread

        public java.util.concurrent.Future<java.lang.Object> loadModuleInWorkerThread​(java.lang.String module)
        Load a module in the worker's event loop thread, returning a future for the loaded module.
        Parameters:
        module - the module id
        Returns:
        the loaded module
      • cancel

        public void cancel​(java.util.concurrent.Future<?> future)
        Cancel a scheduled call.
        Parameters:
        future - the future
      • loadModule

        public org.mozilla.javascript.Scriptable loadModule​(org.mozilla.javascript.Context cx,
                                                            java.lang.String moduleName,
                                                            org.mozilla.javascript.Scriptable loadingScope)
                                                     throws java.io.IOException
        Load a Javascript module into a module scope. This checks if the module has already been loaded in the current context and if so returns the existing module scope.
        Parameters:
        cx - the current context
        moduleName - the module name
        loadingScope - the scope requesting the module
        Returns:
        the loaded module's scope
        Throws:
        java.io.IOException - indicates that in input/output related error occurred
      • registerModule

        protected void registerModule​(Resource resource,
                                      org.mozilla.javascript.Scriptable module)
        Add a resource/scope pair to our map of known up-to-date modules.
        Parameters:
        resource - a resource
        module - the module scope
      • evaluateScript

        public java.lang.Object evaluateScript​(org.mozilla.javascript.Context cx,
                                               ReloadableScript script,
                                               org.mozilla.javascript.Scriptable scope)
                                        throws java.io.IOException
        Evaluate a script within a given scope.
        Parameters:
        cx - the current context
        script - the script
        scope - the scope
        Returns:
        the value returned by the script
        Throws:
        java.io.IOException - an I/O related error occurred
      • isReloading

        public boolean isReloading()
        Returns true if this worker will reload modified modules between invocations.
        Returns:
        true if module reloading is enabled
      • setReloading

        public void setReloading​(boolean reload)
        Enable or disable reloading of modified modules for this worker.
        Parameters:
        reload - true to enable module reloading
      • getErrorListener

        public org.mozilla.javascript.Function getErrorListener()
        Get the current error listener for uncaught errors in this worker
        Returns:
        the error listener
      • setErrorListener

        public void setErrorListener​(org.mozilla.javascript.Function errorListener)
        Set the error listener to handle uncaught errors in this worker
        Parameters:
        errorListener - the error listener
      • getEngine

        public RhinoEngine getEngine()
        Get the worker's engine.
        Returns:
        the engine
      • getErrors

        public java.util.List<ScriptError> getErrors()
        Get a list of errors encountered in the last invocation of this worker.
        Returns:
        a list of errors
      • countScheduledTasks

        public long countScheduledTasks()
        Count the number of scheduled calls in this worker.
        Returns:
        the number of scheduled calls
      • isActive

        public boolean isActive()
        Returns true if this worker is currently running.
        Returns:
        true if worker is active
      • shutdown

        public void shutdown()
        Immediately shut down this worker's event loop.
      • release

        public void release()
        Release the worker, returning it to the engine's worker pool.
      • releaseWhenDone

        public void releaseWhenDone()
        Schedule a task that will release this worker when the current task is finished, returning it back into the engine's worker pool.