-
Notifications
You must be signed in to change notification settings - Fork 41
Pooled annotation like in EJB @Stateless, but for CDI beans #810
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
8caac67
43ad4f8
43e1cf1
e566893
e30f1f8
c16730b
ee05b78
a58dda7
4748e50
128621a
6659a3b
01e5cf4
7662842
6556b15
0786f4c
268dbbb
3c81743
cb4724c
1b92c64
4b2159c
9f66a1e
6d51d2e
5786e03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| /* | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0, which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * This Source Code may also be made available under the following Secondary | ||
| * Licenses when the conditions for such availability set forth in the | ||
| * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
| * version 2 with the GNU Classpath Exception, which is available at | ||
| * https://www.gnu.org/software/classpath/license.html. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
| */ | ||
| package jakarta.enterprise.concurrent; | ||
|
|
||
| import jakarta.enterprise.context.NormalScope; | ||
| import jakarta.enterprise.util.AnnotationLiteral; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.TYPE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
| import static java.util.concurrent.TimeUnit.MINUTES; | ||
|
|
||
| /** | ||
| * Specify that a bean is to be pooled. | ||
| * | ||
| * <p> | ||
| * Pooled beans can have multiple instances that are shared between all threads of an application, but can never be | ||
| * called by more than one thread at the same time. When a method call on a pooled bean is performed, an instance is | ||
| * selected from the pool and is locked until the method call ends. When performing multiple method calls directly after | ||
| * each other, multiple different beans may be used. | ||
| * </p> | ||
| * | ||
| * @since 3.2 | ||
| */ | ||
| @Inherited | ||
| @NormalScope | ||
| @Documented | ||
| @Target({TYPE, METHOD}) | ||
arjantijms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Retention(RUNTIME) | ||
| public @interface Pooled { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add "name" parameter to give the bean a name, which could be used in configuration to specify the values externally (via vendor-specific means, until Jakarta Config is available). |
||
|
|
||
| /** | ||
| * {@return the maximum number of instances in the pool.} | ||
| */ | ||
| int value() default 10; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest that default should be some magic constant, e.g. -1, so that implementations can distinguish between the default value and value that's explicitly set. In case of the default, implementations would be able to change the default in an external configuration.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for the |
||
|
|
||
| /** | ||
| * {@return the maximum amount of time to attempt to obtain a lock on an instance from the pool.} | ||
KyleAure marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| long accessTimeout() default 5; | ||
arjantijms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * {@return the {@link TimeUnit} to use for the {@link #instanceLockTimeout()}} | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| TimeUnit unit() default MINUTES; | ||
|
|
||
| /** | ||
| * The types of {@link Throwable Throwables} which must cause the destruction of a pooled bean instance. | ||
| * <p> | ||
| * If the pooled bean instances throws any Throwable which is an instance of a type set in the destroyOn property, | ||
| * then the bean will be destroyed, except if the throwable is also an instance of a type set in the {@link | ||
| * #dontDestroyOn()} property. | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * </p> | ||
| * | ||
| * @return The types of {@link Throwable Throwables} which must cause the destruction of a pooled bean instance | ||
| */ | ||
| Class<?>[] destroyOn() default {}; | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * The types of {@link Throwable Throwables} which must not cause the destruction of a pooled bean instance. | ||
| * <p> | ||
| * If the {@link #destroyOn()} property is empty, but the dontDestroyOn property is not, then the bean will be | ||
| * destroyed for any Throwable that isn't an instance of a type in the dontDestroyOn property. When the {@link #destroyOn()} property is not empty, then the dontDestroyOn property takes precedence. If a pooled bean instance throws a throwable which is an instance of a type in both the destroyOn and dontDestroyOn properties, then the bean will not be destroyed. | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * </p> | ||
| * | ||
| * @return The types of {@link Throwable Throwables} which must not cause the destruction of a pooled bean instance. | ||
| */ | ||
| Class<?>[] dontDestroyOn() default {}; | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Supports inline instantiation of the Pooled annotation. | ||
| * | ||
| * @since 3.0 | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public static final class Literal extends AnnotationLiteral<Pooled> implements Pooled { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final int maxNumberOfInstances; | ||
| private final long instanceLockTimeout; | ||
| private final TimeUnit instanceLockTimeoutUnit; | ||
| private final Class<?>[] destroyOn; | ||
| private final Class<?>[] dontDestroyOn; | ||
|
|
||
| /** | ||
| * Default instance of the {@link Pooled} annotation. | ||
| */ | ||
| public static final Literal INSTANCE = of( | ||
| 10, | ||
| 5, | ||
| MINUTES, | ||
arjantijms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new Class[]{}, | ||
| new Class[]{} | ||
| ); | ||
|
|
||
| /** | ||
| * Instance of the {@link Pooled} annotation. | ||
| * | ||
| * @param maxNumberOfInstances The maximum number of instances in the pool. | ||
| * @param instanceLockTimeout The maximum amount of time to attempt to obtain a lock on an instance from the pool. | ||
| * @param instanceLockTimeoutUnit The {@link TimeUnit} to use for the {@link #instanceLockTimeout()} | ||
| * @param destroyOn The types of {@link Throwable Throwables} which must cause the destruction of a pooled bean instance. | ||
| * @param dontDestroyOn The types of {@link Throwable Throwables} which must not cause the destruction of a pooled bean instance. | ||
|
|
||
| * @return instance of the {@link Pooled} annotation | ||
| */ | ||
| public static Literal of( | ||
| final int maxNumberOfInstances, | ||
| final long instanceLockTimeout, | ||
| final TimeUnit instanceLockTimeoutUnit, | ||
| final Class<?>[] destroyOn, | ||
| final Class<?>[] dontDestroyOn | ||
|
|
||
|
|
||
| ) { | ||
| return new Literal( | ||
arjantijms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| maxNumberOfInstances, | ||
| instanceLockTimeout, | ||
| instanceLockTimeoutUnit, | ||
| destroyOn, | ||
| dontDestroyOn | ||
| ); | ||
| } | ||
arjantijms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private Literal( | ||
| final int maxNumberOfInstances, | ||
| final long instanceLockTimeout, | ||
| final TimeUnit instanceLockTimeoutUnit, | ||
| final Class<?>[] destroyOn, | ||
| final Class<?>[] dontDestroyOn | ||
| ) { | ||
|
|
||
| this.maxNumberOfInstances = maxNumberOfInstances; | ||
| this.instanceLockTimeout = instanceLockTimeout; | ||
| this.instanceLockTimeoutUnit = instanceLockTimeoutUnit; | ||
| this.destroyOn = destroyOn; | ||
| this.dontDestroyOn = dontDestroyOn; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * {@return the maximum number of instances in the pool.} | ||
| */ | ||
| @Override | ||
| public int maxNumberOfInstances() { | ||
| return maxNumberOfInstances; | ||
| } | ||
|
|
||
| /** | ||
| * {@return the maximum amount of time to attempt to obtain a lock on an instance from the pool.} | ||
| */ | ||
| @Override | ||
| public long instanceLockTimeout() { | ||
| return instanceLockTimeout; | ||
| } | ||
|
|
||
| /** | ||
| * {@return the {@link TimeUnit} to use for the {@link #instanceLockTimeout()}} | ||
| */ | ||
| @Override | ||
| public TimeUnit instanceLockTimeoutUnit() { | ||
| return instanceLockTimeoutUnit; | ||
| } | ||
|
|
||
| /** | ||
| * {@return the types of {@link Throwable Throwables} which must cause the destruction of a pooled bean instance.} | ||
| */ | ||
| @Override | ||
| public Class<?>[] destroyOn() { | ||
| return destroyOn; | ||
| } | ||
|
|
||
| /** | ||
| * {@return the types of {@link Throwable Throwables} which must not cause the destruction of a pooled bean instance.} | ||
| */ | ||
| @Override | ||
| public Class<?>[] dontDestroyOn() { | ||
| return dontDestroyOn; | ||
| } | ||
|
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.