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 Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final StringEnables the per-context Python builtin used by context-reuse tests.static final Stringstatic final StringFields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.graalvm.polyglot.ContextbootstrapReusableContext(ClassLoader classLoader) Create a reusable GraalPy context and evaluate the requested application bootstrap script.static org.graalvm.polyglot.ContextbootstrapReusableContext(ClassLoader classLoader, Map<String, String> options) Create a reusable GraalPy context and evaluate the requested application bootstrap script.static org.graalvm.polyglot.ContextbootstrapReusableContext(ClassLoader classLoader, Map<String, String> options, String applicationMain) Create a reusable GraalPy context and evaluate the requested application bootstrap script.intgetOrder()org.graalvm.polyglot.ContextgraalPyContext(org.graalvm.polyglot.HostAccess hostAccess, org.graalvm.polyglot.Engine engine, GraalPyContextConfiguration contextConfiguration) Create and initialize the GraalPy context.voidonDestroyed(BeanDestroyedEvent<org.graalvm.polyglot.Context> event) Cleanup method called during application shutdown.After a call toGracefulShutdownCapable.shutdownGracefully()report the state of the shutdown.Trigger a graceful shutdown.
-
Field Details
-
APPLICATION_PATH
- See Also:
-
APPLICATION_SRC_PATH
- See Also:
-
INTERNAL_MAIN
- See Also:
-
APPLICATION_MAIN
- See Also:
-
PYRONAUT_MAIN_CLASS
- See Also:
-
CONTEXT_ID_PROPERTY
Enables the per-context Python builtin used by context-reuse tests.- See Also:
-
-
Constructor Details
-
GraalPyContextFactory
-
-
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 ofprocessOnStartupexecutable methods) creates it earlier throughPythonRuntimeBootstrapConfigurer.- Parameters:
hostAccess- The host accessengine- The enginecontextConfiguration- 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 IOExceptionCreate a reusable GraalPy context and evaluate the requested application bootstrap script.- Parameters:
classLoader- The application class loaderoptions- 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 IOExceptionCreate a reusable GraalPy context and evaluate the requested application bootstrap script.- Parameters:
classLoader- The application class loaderoptions- Additional GraalPy context optionsapplicationMain- 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
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:
onDestroyedin interfaceBeanDestroyedEventListener<org.graalvm.polyglot.Context>- Parameters:
event- The bean created event
-
shutdownGracefully
Description copied from interface:GracefulShutdownCapableTrigger a graceful shutdown. The returnedCompletionStagewill 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:
shutdownGracefullyin interfaceGracefulShutdownCapable- Returns:
- A future that completes when this bean is fully shut down
-
reportActiveTasks
Description copied from interface:GracefulShutdownCapableAfter a call toGracefulShutdownCapable.shutdownGracefully()report the state of the shutdown. IfGracefulShutdownCapable.shutdownGracefully()has not been called the behavior of this method is undefined.- Specified by:
reportActiveTasksin interfaceGracefulShutdownCapable- Returns:
- The current number of still-active tasks before the shutdown completes, or
Optional.empty()if no state can be reported
-
getOrder
-