2017-11-25

Integration with Vertx (Extension Module)

Tamaya JNDI is an extension module. Refer to the extensions documentation for further details.

What functionality this module provides ?

Tamaya Vertx provides configuration services that can be used in a Vertx environment:

  • AbstractConfiguredVerticle defines a subclass extending AbstractVerticle, which allows you to use Tamaya Injection API.

  • Additionally you deply a ConfigVerticle, which registers services to access configuration using asynchronous event bus.

Compatibility

The module requires Java 8, so it will not run on Java 7.

Installation

To use Tamaya’s Vertx support you only must add the corresponding dependency to your module:

<dependency>
  <groupId>org.apache.tamaya.ext</groupId>
  <artifactId>tamaya-vertx-alpha</artifactId>
  <version>{tamaya_version}</version>
</dependency>

The Functionality Provided

Note
This module is in alpha state. Please give feedback via our JIRA, so we can improve it.

Extending AbstractConfiguredVerticle

Main artifact is the AbstractConfiguredVerticle class, which implements a base verticle class for Vertx:

public abstract class AbstractConfiguredVerticle extends AbstractVerticle{

    private Configuration configuration;

    public AbstractConfiguredVerticle() {
        configure();
    }

    public Configuration getConfiguration(){
        if(this.configuration==null){
          this.configuration = ConfigurationProvider.getConfiguration();
        }
        return this.configuration;
    }

    public void setConfiguration(Configuration configuration){
        this.configuration = configuration;
        configure();
    }

    protected void configure(){
        ConfigurationInjection.getConfigurationInjector().configure(this, getConfiguration());
    }

    protected final String getConfigProperty(String key);
    protected final String getConfigPropertyOrDefault(String key, String defaultValue);
    protected final <T> T getConfigProperty(String key, Class<T> type);
    protected final <T> T getConfigPropertyOrDefault(String key, Class<T> type, T defaultValue);
}

Using this verticle as a superclass, provides you

  • embedded convenience methods for programmatic configuration access (getConfigProperty* methods).

  • support for configuration injection based on Tamaya’s injection API.

The following code snippet gives you an example, what you can do with this functionality:

public cllass MyVerticle extends AbstractConfiguredVerticle{

   @Override
   public void start(){
     String configuredValue = getConfigPropertyOrDefault("myKey");
     [...]
     BigDecimal bd = getConfigureddPropertyOrDefault("MyNum", BigDecimal.ZERO);
     [...]
   }
}

As menioned you can also use the injection API:

public cllass MyVerticle extends AbstractConfiguredVerticle{

   @Config("myKey")
   private String configuredValue;

   @Config(value="MyNum", defaultValue="0.0")
   private BigDecimal bd;


   @Override
   public void start(){
     [...]
   }
}

Accessing Configuration using the Vertx event bus

Additionally the extension allows to access configuration values from the event bus:

@Override
public void start(){
  // the selector allows to apply a regex on the configuration key to select a
  // a configuration sub set.
  String selector = "user.";
  vertx().eventBus().send(
                "CONFIG-MAP", // event bus address
                selector,
                new Handler<AsyncResult<Message<String>>>() {
                    @Override
                    public void handle(AsyncResult<Message<String>> reply) {
                        testContext.assertNotNull(reply.result());
                        testContext.assertNotNull(reply.result().body());
                        Map<String,String> config = Json.decodeValue(reply.result().body(),
                                Map.class);
                        // do something with the config
                        // ...
                    }
                 });

Similar only single values can be accessed:

@Override
public void start(){
  vertx().eventBus().send(
             "CONFIG-VAL", // event bus address
             "user.home",  // property key
             new Handler<AsyncResult<Message<String>>>() {
                       @Override
                       public void handle(AsyncResult<Message<String>> reply) {
                           String value = reply.result().body();
                           // do something with the config value
                           // ...
                       }
                   });

Finally the event bus targets to be used can be configured using Tamaya configuration, see the code snippet from the implementation:

@Config(value = "tamaya.vertx.config.map", defaultValue = "CONFIG-MAP")
private String mapBusTarget;

@Config(value = "tamaya.vertx.config.value", defaultValue = "CONFIG-VAL")
private String valBusTarget;