Class JavaMethodToProcessWrapper


  • public class JavaMethodToProcessWrapper
    extends java.lang.Object
    This class outsources the call to an arbitrary method into a separate process. This is specifically relevant if you work with libraries that do no respect the interrupt-functionality. Using this wrapper should allow you to conveniently interrupt any method you like. There are of course some drawbacks one must consider: - Execution is MUCH slower compared to execution in a local thread (up to a factor of 10) - all items (target object, inputs, and the expected output) must be serializable - communication with the executed logic is highly limited - no call by reference possible. If the routine to be outsourced needs to update the object on which it is invoked, encapsulate the logic first into a method that returns the object. - there may be strange side effects, e.g., if the subprocess loads libraries manually, this may happen very often and flood temp directories in the file system
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getAbsoluteClasspath()  
      java.io.File getTmpDir()  
      static java.util.List<JavaMethodToProcessWrapper> getWrappers()  
      static void main​(java.lang.String[] args)  
      java.lang.Object run​(java.lang.String clazz, java.lang.String method, java.lang.Object target, java.lang.Object... inputs)  
      java.lang.Object run​(java.lang.String clazz, java.lang.String method, java.lang.Object target, java.util.List<java.lang.Object> inputs)  
      java.util.Optional<java.lang.Object> runWithTimeout​(java.lang.String clazz, java.lang.String method, java.lang.Object target, org.api4.java.algorithm.Timeout timeout, java.lang.Object... inputs)  
      void setMemory​(java.lang.String pMemory)  
      void setTmpDir​(java.io.File tmpDir)  
      • Methods inherited from class java.lang.Object

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

      • JavaMethodToProcessWrapper

        public JavaMethodToProcessWrapper()
    • Method Detail

      • getAbsoluteClasspath

        public static java.lang.String getAbsoluteClasspath()
      • run

        public java.lang.Object run​(java.lang.String clazz,
                                    java.lang.String method,
                                    java.lang.Object target,
                                    java.lang.Object... inputs)
                             throws java.io.IOException,
                                    java.lang.InterruptedException,
                                    java.lang.reflect.InvocationTargetException,
                                    ProcessIDNotRetrievableException
        Throws:
        java.io.IOException
        java.lang.InterruptedException
        java.lang.reflect.InvocationTargetException
        ProcessIDNotRetrievableException
      • runWithTimeout

        public java.util.Optional<java.lang.Object> runWithTimeout​(java.lang.String clazz,
                                                                   java.lang.String method,
                                                                   java.lang.Object target,
                                                                   org.api4.java.algorithm.Timeout timeout,
                                                                   java.lang.Object... inputs)
                                                            throws org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException,
                                                                   java.lang.reflect.InvocationTargetException,
                                                                   java.lang.InterruptedException
        Parameters:
        clazz -
        method -
        target -
        timeout -
        inputs -
        Returns:
        Throws:
        java.io.IOException
        java.lang.reflect.InvocationTargetException
        java.lang.InterruptedException - This is only thrown if the executing thread is interrupted from *outside* but not when it is canceled due to the timeout
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
      • run

        public java.lang.Object run​(java.lang.String clazz,
                                    java.lang.String method,
                                    java.lang.Object target,
                                    java.util.List<java.lang.Object> inputs)
                             throws java.io.IOException,
                                    java.lang.InterruptedException,
                                    java.lang.reflect.InvocationTargetException,
                                    ProcessIDNotRetrievableException
        Throws:
        java.io.IOException
        java.lang.InterruptedException
        java.lang.reflect.InvocationTargetException
        ProcessIDNotRetrievableException
      • main

        public static void main​(java.lang.String[] args)
      • setMemory

        public void setMemory​(java.lang.String pMemory)
      • getTmpDir

        public java.io.File getTmpDir()
      • setTmpDir

        public void setTmpDir​(java.io.File tmpDir)