Skip to content

Latest commit

 

History

History
103 lines (81 loc) · 4.84 KB

File metadata and controls

103 lines (81 loc) · 4.84 KB

Integration with MicroProfile Config

If a MicroProfile Config implementation is available, MicroProfile Config can be used to override configuration attributes of ManagedExecutor and ThreadContext. This is true of instances that are built by the application as well as those produced by the container for injection as CDI beans. The former involves standard usage of MicroProfile Config. The latter relies upon the convention of defining MicroProfile Config property names that correspond to the fully qualified name of the injection point for which the instance is produced.

Application usage of MicroProfile Config

Applications can use MicroProfile Config in the standard way to enable configuration attributes of the ManagedExecutor and ThreadContext builders to be overriden. For example,

@Produces @ApplicationScoped @NamedInstance("executor1")
ManagedExecutor createExecutor(
    @ConfigProperty(name="exec1.maxAsync", defaultValue="5") Integer a,
    @ConfigProperty(name="exec1.maxQueued", defaultValue="20") Integer q) {
    return ManagedExecutor.builder().maxAsync(a).maxQueued(q).build();
}

MicroProfile Config can be used to override configuration attributes from the above example as follows,

exec1.maxAsync=10
exec1.propagated=Application,CDI
exec1.cleared=Remaining

Container usage of MicroProfile Config

The container produces an instance per unqualified ManagedExecutor injection point, which may optionally be annotated with the @ManagedExecutorConfig annotation to supply default configuration. The container also produces an instance per ManagedExecutor injection point that is annotated with both the @ManagedExecutorConfig annotation and the @NamedInstance qualifier. And likewise for ThreadContext and @ThreadContextConfig.

In each of these cases, MicroProfile Config can be used to override the configuration of the instance that is produced by the container.

To override a configuration attribute of an instance that is produced for injection into a field, specify a MicroProfile Config property with the name equal to the fully qualified class name, field name, and configuration attribute name, delimited by the . character.

package org.eclipse.microprofile.example;
...
public class MyBean {
    @Inject
    ManagedExecutor exec2;

    @Inject @ManagedExecutorConfig(maxAsync=3)
    ManagedExecutor exec3;

    @Inject @NamedInstance("executor4") @ManagedExecutorConfig(maxAsync=4)
    ManagedExecutor exec4;

    @Inject @NamedInstance("executor4")
    ManagedExecutor exec5;
}
org.eclipse.microprofile.example.MyBean.exec2.maxAsync=5
org.eclipse.microprofile.example.MyBean.exec3.maxAsync=6
org.eclipse.microprofile.example.MyBean.exec4.maxAsync=7

Note that org.eclipse.microprofile.example.MyBean.exec4.maxAsync overrides the configuration of the instance that is produced for injection into exec4. This same instance is injected into exec5 per the matching @NamedInstance("executor4") qualifier. It is an error to specify org.eclipse.microprofile.example.MyBean.exec5.maxAsync, which will not apply to exec5.

To override a configuration attribute of an instance that is produced for injection into a parameter, specify a MicroProfile Config property with its name equal to the fully qualified class name, method name, parameter number (starting at 1), and configuration attribute name, delimited by the . character.

package org.eclipse.microprofile.example;
...
public class MyBean {
    @Produces @ApplicationScoped @NamedInstance("executor6")
    ManagedExecutor createExecutor(@ManagedExecutorConfig(maxAsync=6) exec) {
        return exec;
    }

    @Inject @NamedInstance("executor6")
    ManagedExecutor exec6;
}
org.eclipse.microprofile.example.MyBean.createExecutor.1.maxAsync=10

Again, it would be wrong to specify org.eclipse.microprofile.example.MyBean.exec6.maxAsync, because the container does not produce a new instance for the exec6 injection point. The container produces the new instance for the exec injection point, and matches that same instance for injection into exec6 per the @NamedInstance("executor6") qualifier.