Skip to content

Provide a mechanism to detect ConfigValue changes #63

@struberg

Description

@struberg

Splitting this out from #9 into a separate ticket as discussed in our last EG meeting.

When people make use of ConfigValue they often want to trigger some action when a new value get's picked up.
Remember that ConfigValue is only needed for configured values which might change during runtime.

Often a user wants to be aware when the underlying value got changed. Either to log it out, or to perform some other code.

This is essentially done by registering a lambda as callback which gets called whenver the underlying config gets accessed an a change gets detected.
https://github.com/struberg/ConfigJSR-1/blob/i9_configValue/api/src/main/java/javax/config/ConfigValue.java#L162

Here is a code examples to highlight why this is an important use case:

@ApplicationScoped
public class FtpService {
  private ConfigValue<String> urlCfg;

  @Inject 
  void setConfig(Config cfg) {
    cfg.access("myapp.ftp.url")
      .cacheFor(1, TimeUnits.DAY)
      .evaluateVariables(true)
      .withLookupChain("${myapp.tenant}")
      .onChange((propertyName, oldV, newV) 
          -> log.info("Switchint to a new FTP URL from {} to {}", oldV, newV));
  }

  public void store(String name, byte[] content) {
    FtpConnection = access(urlCfg.getValue());
  }
}

This is really important for our Ops team. Otherwise they don't really have a clue when the system eventually picked up new values!

The onChange callback might also be used to e.g. clean cached values, etc. It's really very versatile and an important use case!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions