-
Notifications
You must be signed in to change notification settings - Fork 23
/
NamedInstance.java
101 lines (92 loc) · 3.55 KB
/
NamedInstance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.microprofile.concurrent;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Qualifier;
/**
* <p>Qualifies a CDI injection point for a {@link ManagedExecutor} or {@link ThreadContext} with a unique name.</p>
*
* <p>This annotation can be used in combination with the {@link ManagedExecutorConfig} or {@link ThreadContextConfig}
* annotation to define a new instance. For example,</p>
*
* <pre><code> @Inject @NamedInstance("myExecutor") @ManagedExecutorConfig(maxAsync=10)
* ManagedExecutor myExecutor;
*
* @Inject @NamedInstance("myContext") @ThreadContextConfig(propagated = { ThreadContext.SECURITY, ThreadContext.CDI })
* ThreadContext myThreadContext;
* </code></pre>
*
* <p>This annotation can be used on its own to qualify an injection point with the name of
* an existing instance. For example, referencing a name from the previous example,</p>
*
* <pre><code> @Inject @NamedInstance("myExecutor")
* ManagedExecutor exec1;
*
* @Inject @NamedInstance("myContext")
* ThreadContext myContextPropagator;
* </code></pre>
*
* <p>Alternatively, an application can use this annotation as a normal CDI qualifier,
* defining its own scope, producer, and disposer. For example,</p>
*
* <pre><code> @Produces @ApplicationScoped @NamedInstance("exec2")
* ManagedExecutor exec2 = ManagedExecutor.builder().maxAsync(5).build();
*
* public void shutdown(@Disposes @NamedInstance("exec2") ManagedExecutor executor) {
* executor.shutdown();
* }
*
* int doSomething(@Inject @NamedInstance("exec2") ManagedExecutor executor) {
* ...
* }
* </code></pre>
*/
@Qualifier
@Retention(RUNTIME)
@Target({ FIELD, METHOD, PARAMETER, TYPE })
public @interface NamedInstance {
/**
* Unique name that qualifies a {@link ManagedExecutor} or {@link ThreadContext}.
*/
String value();
/**
* Supports inline instantiation of the {@link NamedInstance} qualifier.
*
*/
public final class Literal extends AnnotationLiteral<NamedInstance> implements NamedInstance {
private static final long serialVersionUID = 1L;
private final String value;
public static Literal of(String value) {
return new Literal(value);
}
public String value() {
return value;
}
private Literal(String value) {
this.value = value;
}
}
}