-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ManagedExecutorService and ContextService default resources as injectable CDI beans #229
Comments
I did actually wonder why with the entire platform moving to CDI we actually bothered with It's probably historical, as Concurrency has it roots tracing back as one of the oldest specs in Jakarta EE (despite having its first release much later). Maybe we can even go a step further though and deprecate |
How to process the JDNI name/lookup when using |
|
Here are some examples. First, the straightforward case of the default instances:
could instead be done as:
In the case of configured instances, the following is currently possible:
To do this with
It turns out that names that don't start with java:comp, java:module, java:app, or java:global are actually allowed already, but unfortunately get interpreted as a java:comp/env, so in this case, We would need to standardize the qualifier. Reusing
It seems like this should be possible then for both the default instances and configured instances of these resources. I'd be curious what thoughts others have on this. |
I'm definitely for providing a default producer for injecting ManagedExecutorService, ContextService and anything else that can be injected using @resource now (e.g. ManagedScheduledExecutorService, ManagedThreadFactory), as in the original proposal by @njr-11. In other cases, I'm not sure that we need a different mechanism than @resource. In my view, it's always better practice to define logical CDI qualifiers for beans by the user, such as We should rather encourage users to create their own qualifiers and add the mapping to a producer, like this:
Replacing |
We should also remember that some applications may already contain producers for the default concurrency services, such as:
So a default producer should be created by the container only if there's no producer defined for it already. |
I would like to simplify the JNDI name as CDI naming directly. @ManagedExecutorDefinition(name = "MyExecutor", ...) And inject it directly. @Inject
@Named("MyExecutor")
ManagedExecutorService executor; |
There is a wish to root out What about something like: Define @MyExecutor
@ManagedExecutorDefinition(
hungTaskThreshold = 120000,
maxAsync = 5)
@ApplicationScoped
public class ExecutorProducer {
} Inject @Inject
@MyExecutor
ManagedExecutorService executor; So simply do not use String based names at all. Only use typesafe annotations. |
That's an interesting approach. Avoiding String names is appealing. The part I don't understand is how it can be assumed that
which would make it precise enough, but comes at a cost of adding some awkwardness. |
What about using the first approach when there’s only one definition and the second when there are more definitions? If the first approach is used with multiple definitions, deployment would fail, with a message that suggests using the second approach. |
However, @resource would still be needed to refer to executors defined outside of the app. But it’s not worth dropping support for that and replace it with another annotation. @resource is a general annotation from the Annotations spec, I would simply keep it for the purpose of injecting via JNDI if needed. If a qualifier is used with @ManagedExecutorDefinition, the name parameter should become optional, with a generated JNDI name if omitted. |
The question is whether that is needed. We don't need @resource to refer to e.g. an At some point people thought it would be absolutely necessary to define CDI beans outside the app and/or using XML (a sub-project was even started for it), but eventually it became clear this wasn't needed. It just reflected the old way of thinking (e.g. the managed-bean element in faces-config.xml). |
We may need to restrict the cases where qualifiers can be used for We may use embedded classes, or maybe adding target FIELD to @ApplicationScoped
public class ExecutorProducer {
@ManagedExecutorDefinition(
hungTaskThreshold = 120000,
maxAsync = 5)
@Produces
@BackgroundJobs
ManagedExecutorService executor;
@ManagedExecutorDefinition(
hungTaskThreshold = 10000,
maxAsync = 1)
@Produces
@SequentialExecutor
ManagedExecutorService executor;
} |
I think restricting would be fine, and the example with
It seems complicated and confusing to have two different approaches and I don't think the first approach is clear even in the simple scenario. Unless it is restricted such as what Arjan proposed, how can it be known unambiguously whether or not annotations ought to be treated as qualifiers when they happen to appear alongside a
+1 Also I would point out that support for |
For this issue, we should also decide if this constitutes a breaking change such that the next version of Concurrency should be 4.0 rather than 3.1. It seems to me that it does because a user could currently have a Producer of ManagedExecutorService (and the other types) without a qualifier and already be using the following for it,
Hopefully few if any users are actually doing that and always using their own qualifiers, in which case there will be no change to their applications, but it is possible to impact current behavior if they have done this without a qualifier. |
I think that it's possible to define a bean by the container only if it's not already defined by the application. In the afterBeanDiscovery even handler, it should be possible to check whether a ManagedExecutorService bean with no qualifier is defined and create it if it's not. This should be also pretty easy to cover by the TCK. Then we don't need to break any existing applicatoins. |
I just realized there is another problem with:
In addition to applying the |
True. It would rather have to be something like:
|
…iers Signed-off-by: Nathan Rauh <[email protected]>
I created pull #348 to add the above pattern, along with the other part of this issue/proposal, which is the ability to inject the default ManagedExecutorService and so forth without qualifiers. |
True, although how much does it matter? Typically those init / definition beans are not meant for injection anyway. Of course the catch is in "typically", and I agree, sometimes the user would want such init bean to be injectable. |
Signed-off-by: Nathan Rauh <[email protected]>
…nd example Signed-off-by: Nathan Rauh <[email protected]>
Some CDI users would like to be able inject the default instances of ManagedExecutorService, ManagedScheduledExecutorService and/or ContextService into CDI beans with
@Inject
instead of@Resource
.For example,
instead of
in order to get the default resource "java:comp/DefaultManagedExecutorService".
This can currently be done within the application supplying a CDI producer, but the Jakarta EE product provider could have it built-in and make that unnecessary and some might already be doing so. It would be nice to have standardization of one or the other approach so that applications are portable.
The text was updated successfully, but these errors were encountered: