Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get my configuration values in yml - using dropwizard (microservice) Jersey D.I @Injection?

here's my code snippets.

here's my yml file:

productionServer:
  host: production-server.amazonaws.com
  publicIp: xx.xx.xx.xx
  privateIp: xx.xx.xx.xx
  userName: xx.xx.xx.xx
  password: xx.xx.xx.xx
  remoteFilePath: fake/path/
  fileName: test.txt
  privateKey: private-public-key.ppk

server:
  applicationConnectors:
    - type: http
      port: 8080
    - type: https
      port: 8443
      keyStorePath: key.keystore
      keyStorePassword: password
      validateCerts: false
  adminConnectors:
    - type: http
      port: 8081
    - type: https
      port: 8444
      keyStorePath: key.keystore
      keyStorePassword: password
      validateCerts: false

MyConfiguration class:

import io.dropwizard.Configuration;

public class MyConfiguration extends Configuration{

    @NotNull
    @JsonProperty
    private ProductionServer productionServer;

    // getters

public class ProdctionServer{

      @NotEmpty
      @JsonProperty
      private host;

      @NotEmpty
      @JsonProperty
      private publicIp;

      // getters

Application class:

import io.dropwizard.Application;

public class MyApplication extends Application<MyConfiguration> {

    public static void main(String[] args) throws Exception{
        new MysApplication().run(args);
    }

    @Override
    public String getName(){ return "micro-service"; }

    @Override
    public void initialize(Bootstrap<MyConfiguration> bootstrap){}

    @Override
    public void run(MyConfiguration conf, Environment environment ){
        final MyResource myResource = new MyResource();
        // health check

        // environment.healthChecks().register("template",healthCheck);

        System.out.println( "==> " + conf );
        System.out.println( "==> " + conf.getProductionServer() );

        // register
        environment.jersey().register( MyResource );

and when running this app:

i received a logged as follows:

==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@623e088f, io.dropwizard.jetty.HttpsConnectorFactory@39fcbef6], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@34f22f9d, io.dropwizard.jetty.HttpsConnectorFactory@77d67cf3], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@663411de]}}
==> com.mycompany.myproject.model.ProductionServer@5b04476e

meaning it is successfully gets the value of my yaml. but my problem is during the D.I or dependency injection of MyConfiguration class. i cannot get the value of my ProductionServer though the Object MyConfiguration seems not null in my Service.

here's my code snippet of dependency binding the MyService.class and the MyConfiguration.class

DependencyBinder.class

import org.glassfish.hk2.utilities.binding.AbstractBinder;

public class DependencyBinder extends AbstractBinder {

@Override
protected void configure() {
    bind(MyConfiguration.class).to(MyConfiguration.class);
    bind(MyService.class).to(MyService.class);
}

MyService.class

public class MyService {

    @Inject
    MyConfiguration conf;

    public void invoke(){
        System.out.println( "=============================== " );
        System.out.println( "==> " + conf );
        System.out.println("==> " + conf.getProductionServer() );
    }

and during the invoking of the method invoke()... i got a logged as follows:

=============================== 
==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@34e82c4d], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@19b70fbd], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@543f81c9]}}
==> null

now my problem is during the D.I or dependency injection of MyConfiguration class in MyService.class. i cannot get the value of my ProductionServer though the Object MyConfiguration seems not null in my Service. please give me some resolution? thnx.

like image 210
teodoro Avatar asked Mar 09 '16 03:03

teodoro


People also ask

What is configuration class in Dropwizard?

Creating A Configuration Class Each Dropwizard application has its own subclass of the Configuration class which specifies environment-specific parameters. These parameters are specified in a YAML configuration file which is deserialized to an instance of your application's configuration class and validated.

Which is better spring boot vs Dropwizard?

The bare bone spring boot app starts in 1.64 seconds whereas the bare bone Dropwizard app took 1.526 seconds to startup. Spring Boot consumes much more memory. This was true. Spring Boot loaded 7591 classes whereas Dropwizard loaded 6255 classes.

How do I run a Dropwizard application in Intellij?

1) Start by clicking the drop down next to the run buttons in the top right of the screen. Then Select edit configuration. 2) Now click the add button (“+”) at the top left of the configuration screen, and choose “Application”. 3) Set your working directory of your application, it may already be set.


1 Answers

The problem is, with this configuration

bind(MyConfiguration.class).to(MyConfiguration.class);

HK2 will create a new instance of the MyConfiguration. It will not be the same instance populated by DW. What you can do though, is use the instance created by DW, by simply binding that same instance in your HK2 configuration

public class MyApplication extends Application<MyConfiguration> {

    @Override
    public void run(final MyConfiguration config, Environment env) {
        env.jersey().register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(config).to(MyConfiguration.class);
            }
        });
    }
}
like image 108
Paul Samsotha Avatar answered Nov 15 '22 00:11

Paul Samsotha