Class ScriptManager


  • @ThreadSafe
    public class ScriptManager
    extends java.lang.Object
    Utility to load, reload and execute scripts (in whichever format/language supported by ScriptEngine) via URI (e.g., local file, classpath, URL).

    N.B.: Scripts must be explicitly registered by calling manageScript(URI) in order to be evaluated.

    N.B.: If a compilation or access error is encountered while reloading a previously compiled script, the latest compiled version is retained, until it can be replaced with a newer one.

    Since:
    4.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ScriptManager​(ScriptManagerProperties properties, org.springframework.scheduling.TaskScheduler taskScheduler, java.util.concurrent.ExecutorService executorService, javax.script.ScriptEngineManager scriptEngineManager, org.springframework.core.io.ResourceLoader resourceLoader, io.micrometer.core.instrument.MeterRegistry meterRegistry)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Object evaluateScript​(java.net.URI scriptUri, javax.script.Bindings bindings, long timeout)
      Evaluate a given script.
      void manageScript​(java.net.URI scriptUri)
      Start managing the given script, loading it ASAP (asynchronously) and refreshing it periodically.
      • Methods inherited from class java.lang.Object

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

      • ScriptManager

        public ScriptManager​(ScriptManagerProperties properties,
                             org.springframework.scheduling.TaskScheduler taskScheduler,
                             java.util.concurrent.ExecutorService executorService,
                             javax.script.ScriptEngineManager scriptEngineManager,
                             org.springframework.core.io.ResourceLoader resourceLoader,
                             io.micrometer.core.instrument.MeterRegistry meterRegistry)
        Constructor.
        Parameters:
        properties - properties
        taskScheduler - task scheduler
        executorService - executor service
        scriptEngineManager - script engine manager
        resourceLoader - resource loader
        meterRegistry - meter registry
    • Method Detail

      • manageScript

        public void manageScript​(java.net.URI scriptUri)
        Start managing the given script, loading it ASAP (asynchronously) and refreshing it periodically. Because the execution of this task is asynchronous, this method returns immediately and does not surface any loading errors encountered.
        Parameters:
        scriptUri - the script to load and manage
      • evaluateScript

        protected java.lang.Object evaluateScript​(java.net.URI scriptUri,
                                                  javax.script.Bindings bindings,
                                                  long timeout)
                                           throws ScriptNotConfiguredException,
                                                  ScriptExecutionException
        Evaluate a given script.
        Parameters:
        scriptUri - the script URI
        bindings - the input parameter bindings
        timeout - the timeout in milliseconds
        Returns:
        the result of the evaluation
        Throws:
        ScriptNotConfiguredException - if the script is not loaded (due to invalid URI or compilation errors).
        ScriptExecutionException - if the script evaluation produces an error