Class GraalPyContextFactory

java.lang.Object
io.micronaut.context.python.GraalPyContextFactory
All Implemented Interfaces:
BeanDestroyedEventListener<org.graalvm.polyglot.Context>, Ordered, GracefulShutdownCapable, EventListener

@Factory public class GraalPyContextFactory extends Object implements BeanDestroyedEventListener<org.graalvm.polyglot.Context>, GracefulShutdownCapable, Ordered
Factory bean that creates and initializes the GraalPy context. Loads the generated Python application script and makes the context available via PythonContextRuntime for bridge classes to use.
Since:
5.2.0
Author:
Micronaut Team
  • Field Details

  • Constructor Details

    • GraalPyContextFactory

      public GraalPyContextFactory(ApplicationContext applicationContext)
  • Method Details

    • graalPyContext

      @Context @Singleton @Named("python") @Order(-2147483648) public org.graalvm.polyglot.Context graalPyContext(@Named("python") org.graalvm.polyglot.HostAccess hostAccess, @Named("python") org.graalvm.polyglot.Engine engine, GraalPyContextConfiguration contextConfiguration)
      Create and initialize the GraalPy context. This bean loads on startup due to the @Context annotation; generated Python code that runs before the eager beans are initialized (type converters, beans of processOnStartup executable methods) creates it earlier through PythonRuntimeBootstrapConfigurer.
      Parameters:
      hostAccess - The host access
      engine - The engine
      contextConfiguration - The GraalPy context configuration
      Returns:
      The initialized GraalPy context
    • bootstrapReusableContext

      public static org.graalvm.polyglot.Context bootstrapReusableContext(ClassLoader classLoader) throws IOException
      Create a reusable GraalPy context and evaluate the requested application bootstrap script.
      Parameters:
      classLoader - The application class loader
      Returns:
      The initialized GraalPy context
      Throws:
      IOException - If the context cannot load application resources
    • bootstrapReusableContext

      public static org.graalvm.polyglot.Context bootstrapReusableContext(ClassLoader classLoader, Map<String,String> options) throws IOException
      Create a reusable GraalPy context and evaluate the requested application bootstrap script.
      Parameters:
      classLoader - The application class loader
      options - Additional GraalPy context options
      Returns:
      The initialized GraalPy context
      Throws:
      IOException - If the context cannot load application resources
    • bootstrapReusableContext

      public static org.graalvm.polyglot.Context bootstrapReusableContext(ClassLoader classLoader, Map<String,String> options, String applicationMain) throws IOException
      Create a reusable GraalPy context and evaluate the requested application bootstrap script.
      Parameters:
      classLoader - The application class loader
      options - Additional GraalPy context options
      applicationMain - The Python source resource to evaluate after the generated launcher
      Returns:
      The initialized GraalPy context
      Throws:
      IOException - If the context cannot load application resources
    • onDestroyed

      public void onDestroyed(BeanDestroyedEvent<org.graalvm.polyglot.Context> event)
      Cleanup method called during application shutdown.

      When the destroyed context is the one this factory built, the runtime of this application is uninstalled right away, so generated code of an enclosing application (a nested ApplicationContext.run(...) in a Python test) resolves its own runtime again, and the context is closed once it is idle. Any other context, one of an enclosing application or a reused one, is left alone.

      Specified by:
      onDestroyed in interface BeanDestroyedEventListener<org.graalvm.polyglot.Context>
      Parameters:
      event - The bean created event
    • shutdownGracefully

      public CompletionStage<?> shutdownGracefully()
      Description copied from interface: GracefulShutdownCapable
      Trigger a graceful shutdown. The returned CompletionStage will complete when the shutdown is complete.

      Note that the completion of the returned future may be user-dependent. If a user does not close their connection, the future may never terminate. Always add a timeout for a hard shutdown.

      This method should not throw an exception, nor should the returned stage complete exceptionally. Just log an error instead.

      Specified by:
      shutdownGracefully in interface GracefulShutdownCapable
      Returns:
      A future that completes when this bean is fully shut down
    • reportActiveTasks

      public OptionalLong reportActiveTasks()
      Description copied from interface: GracefulShutdownCapable
      After a call to GracefulShutdownCapable.shutdownGracefully() report the state of the shutdown. If GracefulShutdownCapable.shutdownGracefully() has not been called the behavior of this method is undefined.
      Specified by:
      reportActiveTasks in interface GracefulShutdownCapable
      Returns:
      The current number of still-active tasks before the shutdown completes, or Optional.empty() if no state can be reported
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface Ordered
      Returns:
      The order of the object. Defaults to zero (no order).