{title:'Config', created:'8.0.0'}

The following methods can be used to define the configuration for your microservice using the powerful Config API:

If you do not specify any of this information, we attempt to resolve it through the following methods:

  1. Resolve file first in working directory, then in classpath, using the following names:
    1. The "configFile" argument in the command line arguments passed in through the constructor.
    2. The value of the Main-Config entry in the manifest file.
    3. A config file in the same location and with the same name as the executable jar file. (e.g. "java -jar myjar.jar" will look for "myjar.cfg").
  2. Resolve any "*.cfg" file that can be found in the working directory.
  3. Resolve any of the following files in the classpath:
    1. juneau.cfg
    2. default.cfg
    3. application.cfg
    4. app.cfg
    5. settings.cfg

If no configuration file is found, and empty in-memory configuration is used.

The {@link oaj.microservice.Microservice.Builder#configName(String) configName(String)} method allows you to explicitly specify the name of the external configuration file location for your microservice.

| Microservice | .create() | .config("my-files/MyMicroservice.cfg") | .build() | .start() | ;

By default, we try to find the file on the file system and then the classpath. If located on the file system, then the configuration is writeable and the microservice can automatically listen for and react to changes in the configuration file on the file system. If located on the classpath, then the configuration can still react to modifications made to it through the Config API but the changes cannot be persisted since the location prevents the file from being modified.

The {@link oaj.microservice.Microservice.Builder#configStore(ConfigStore) configStore(ConfigStore)} method can be used to explicitly specify a configuration store. This can include your own custom configuration store, such as one that's implemented in a relational database.

| Microservice | .create() | .configStore(new MyConfigSqlStore()) | .configName("MyConfig") | .build() | .start() | ;

The {@link oaj.microservice.Microservice.Builder#config(Config) config(Config)} method can be used to explicitly specify a {@link oaj.config.Config} file as the microservice configuration. When this method is used, the above two methods are bypassed entirely.

| Config config = getMyOwnConfig(); | | Microservice | .create() | .config(config) | .build() | .start() | ;

Once the configuration is resolved, it is made as the system default configuration available through the {@link oaj.config.Config#getSystemDefault()}. This in turn allows it to be used by REST resources that reference the system default configuration via the "SYSTEM_DEFAULT" such as those implementing the BasicRestConfig interface.

BasicRestConfig.java

| @Rest( | config="$S{juneau.configFile,SYSTEM_DEFAULT}" | ... | )

The {@link oaj.microservice.Microservice#getConfig()} method can be used to get access to the configuration.

| Config config = Microservice.getInstance().getConfig(); | | File logDir = config.get("Logging/logDir").as(File.class).orElse(null); | boolean append = config.get("Logging/append").asBoolean().orElse(null); | String format = config.get("Logging/format", "[{date} {level}] {msg}%n").orElse(null); | long limit = config.get("Logging/limit").asLong().orElse(null); | Map<String,Level> levels = config.get("Logging/levels").as(Map.class, String.class, Level.class).orElse(null);

Changes to the configuration file can trigger notifications that can be used to restart your microservice or make various other on-the-fly changes. This can be accomplished by either overriding the {@link oaj.microservice.Microservice#onConfigChange(ConfigEvents)} or implementing a listener and using the {@link oaj.microservice.MicroserviceListener#onConfigChange(Microservice,ConfigEvents)} methods. These will be described in detail later.