Skip to content

Latest commit

 

History

History
109 lines (85 loc) · 5.31 KB

File metadata and controls

109 lines (85 loc) · 5.31 KB

Establishing default values with MicroProfile Config

If a MicroProfile Config implementation is available, MicroProfile Config can be used to establish default values for configuration attributes of ManagedExecutor and ThreadContext. This allows the application to bypass configuration of one or more attributes when using the builders to create instances.

For example, you could specify MicroProfile Config properties as follows to establish a set of defaults for ManagedExecutor,

mp.context.ManagedExecutor.propagated=Remaining
mp.context.ManagedExecutor.cleared=Transaction
mp.context.ManagedExecutor.maxAsync=10
mp.context.ManagedExecutor.maxQueued=-1

With these defaults in place, the application can create a ManagedExecutor instance without specifying some of the configuration attributes,

executor = ManagedExecutor.builder().maxAsync(5).build();

In the code above, the application specifies only the maxAsync attribute, limiting actions and tasks requested to run async to at most 5 running at any given time. The other configuration attributes are defaulted as specified in MicroProfile config, with no upper bound on queued tasks, Transaction context cleared, and all other context types propagated.

As another example, the following MicroProfile Config properties establish defaults for ThreadContext,

mp.context.ThreadContext.propagated=None
mp.context.ThreadContext.cleared=Security,Transaction
mp.context.ThreadContext.unchanged=Remaining

With these defaults in place, the application can create a ThreadContext instance without specifying some of the configuration attributes,

cdiContextPropagator = ThreadContext.builder()
                                    .propagated(ThreadContext.CDI)
                                    .build();

In the code above, the application specifies only the propagated attribute, indicating that only CDI context is propagated. The other configuration attributes inherit the defaults, which includes clearing Security and Transaction context and leaving all other thread context types unchanged.

Specifying Defaults for Array Properties in MicroProfile Config

When using MicroProfile Config to define defaults for array type properties (propagated, cleared, and unchanged), the following rules apply for config property values:

  • The value can be a single array element, multiple elements (delimited by ,), or the value None, which is interpreted as an empty array of context types.

  • Array elements can be any value returned by a ThreadContextProvider's getThreadContextType() method.

  • Array elements can be any thread context type constant value from ThreadContext (such as Security, Application, or Remaining).

  • The usual rules from the MicroProfile Config specification apply, such as escaping special characters.

As of MicroProfile Config 2.0 and above, the value of None must be used to indicate an empty array. Prior to MicroProfile Config 2.0, an empty string value could be used to specify an empty array. In order to guarantee that empty string config values are interpreted properly, the MicroProfile Context Propagation implementation must interpret both of the following as indicating empty:

  • empty array

  • array containing the empty String as its singular element

This is necessary due to a lack of clarity in the first several versions of the MicroProfile Config specification about how the empty string value is to be interpreted for arrays of String. MicroProfile Config 2.0 removed the ability to configure empty arrays and lists altogether.

Overriding values with MicroProfile Config

MicroProfile Config can also be used in the standard way to enable configuration attributes of the ManagedExecutor and ThreadContext builders to be overridden. For example,

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
public @interface SecurityAndAppContext {}

@Produces @ApplicationScoped @SecurityAndAppContext
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)
                          .propagated(ThreadContext.SECURITY, ThreadContext.APPLICATION)
                          .cleared(ThreadContext.ALL_REMAINING)
                          .build();
}

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

exec1.maxAsync=10
exec1.maxQueued=15