Skip to content

Invoker support for asynchronous methods #859

@Azquelt

Description

@Azquelt

Is your feature request related to a problem? Please describe.

Invokers cannot be used to portably call asynchronous methods. This would prevent Invokers being used to call Jakarta REST resource methods which execute asynchronously and return CompletionStage in a safe way without relying on non-spec features of the CDI container in use.

Background

Invokers allow the target instance and arguments for the method being invoked to be looked up from CDI. It looks up the bean matching the type and qualifiers, uses that as the instance or argument, calls the method and then destroys the reference afterwards.

For dependent scoped beans, this means that the bean will live for the duration of the method invocation and then be destroyed.

This is a problem if the method represents an asynchronous operation which may still be ongoing when the method returns and still require access to the beans used as the instance or arguments.

The spec [does allow][spec-impl-specific] for implementation-specific solutions to this problem, but such solutions are not portable.

Additionally, some practical considerations I think are important here:

  • this only affects @Dependent scoped beans
    • normal-scoped beans will have their lifecycle tied to their context. The reference injected will be a proxy object which will delegate to the bean instance from the current context.
  • some scopes (including @RequestScoped) may not be usable for asynchronous use anyway
  • as far as I'm aware, the only consequence of destroying a @Dependent scoped bean is that any @PreDestroy methods of that bean or its dependents will be run. If there are no pre-destroy lifecycle callbacks, there may not be an issue with continuing to use the bean instance after it's been destroyed.

Describe the solution you'd like

An API is added which allows the looked up bean references to remain valid until after the asynchronous operation ends. There are various options for what this API may look like discussed in #749 and #768.

Describe alternatives you've considered
TODO add options discussed previously

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions