Skip to content

Latest commit

 

History

History
106 lines (82 loc) · 4.87 KB

File metadata and controls

106 lines (82 loc) · 4.87 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,

ManagedExecutor/propagated=Remaining
ManagedExecutor/cleared=Transaction
ManagedExecutor/maxAsync=10
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,

ThreadContext/propagated=
ThreadContext/cleared=Security,Transaction
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 empty.

  • 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.

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.

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