2017-07-06 59 views
0

我正在編寫在MongoDB數據庫中獲取數據的Dropwizard微服務。微服務工作正常,但我努力在我的DAO中使用來自我的Dropwizard配置Java類的配置。目前,我有在建立到MongoDB數據庫的連接的方法中使用Dropwizard配置

public class XDAO implements IXDAO { 

    protected DB db; 
    protected DBCollection collection; 

    /* singleton */ 
    private static XDAO instance; 

    /* Get singleton */ 
    public static synchronized XDAO getSingleton(){ 
     if (instance == null){ 
     instance = new XDAO(); 
     }  
     return instance; 
    } 

    /* constructor */ 
    public XDAO(){ 
     initDatabase(); 
     initDatabaseIndexes(); 
    } 

    private void initDatabase(){ 
     MongoClient client = null; 

     try { 
      client = new Mongo("10.126.80.192",27017); 
      db = client.getDB("terre"); 
      //then some other code 
     } 

     catch (final MongoException e){ 
     ... 
     } 

     catch (UnknownHostException e){ 
     ... 
     } 

    } 

} 

我想unhard碼的三個參數在這兩條線:

client = new Mongo("10.126.80.192", 27017); 
    db = client.getDB("terre"); 

我MongoConfiguration Java類是:

public class MongoConfiguration extends Configuration { 

@JsonProperty 
@NotEmpty 
public String host; 

@JsonProperty 
public int port = 27017; 

@JsonProperty 
@NotEmpty 
public String db_name; 

public String getMongohost() { 
    return host; 
} 

public void setMongohost(String host) { 
    this.host = host; 
} 

public int getMongoport() { 
    return port; 
} 

public void setMongoport(int port) { 
    this.port = port; 
} 

public String getDb_name() { 
    return db_name; 
} 

public void setDb_name(String db_name) { 
    this.db_name = db_name; 
} 

} 

我的資源類使用DAO是:

@Path("/mongo") 
@Produces(MediaType.APPLICATION_JSON) 
public class MyResource { 

private XDAO xDAO = XDAO.getSingleton(); 

private String mongohost; 
private String db_name; 
private int mongoport; 

public MyResource(String db_name, String mongohost, int mongoport) { 
    this.db_name = db_name; 
    this.mongohost = mongohost; 
    this.mongoport = mongoport; 
} 

public MyResource() { 
} 

@GET 
@Path("/findByUUID") 
@Produces(value = MediaType.APPLICATION_JSON) 
@Timed 
public Entity findByUUID(@QueryParam("uuid") String uuid) { 

    return xDAO.findByUUid(uuid); 
    } 
} 

而在我的應用程序類中有

@Override 
    public void run(final MongoConfiguration configuration, final Environment environment) { 

    final MyResource resource = new MyResource(configuration.getDb_name(), configuration.getMongohost(), configuration.getMongoport()); 
    environment.jersey().register(resource); 
    } 

爲了解決我的問題,我嘗試了很多東西。我想的最後一件事是在我XDAO

private String mongohost; 
    private String db_name; 
    private int mongoport; 
    private static final MongoConfiguration configuration = new MongoConfiguration(); 

與這段代碼在XDAO的構造即將加入這四個領域:

public XDAO(){ 
    instance.mongohost = configuration.getMongohost(); 
    instance.mongoport = configuration.getMongoport(); 
    instance.db_name = configuration.getDb_name(); 
    /* then like before */ 
    initDatabase(); 
    initDatabaseIndexes(); 
} 

當我嘗試這個,我有一個空指針mongoHost和DB_NAME是空

回答

0

的問題是,你正在創建你的XDAO一個配置與private static final MongoConfiguration configuration = new MongoConfiguration();,而不是使用配置從Dropwizard」:當我的initDatabase方法被調用例外s run方法。

當你做到這一點,在配置領域hostdb_name爲空,這就是爲什麼你的實例XDAO

時獲得NPE你需要傳遞的MongoConfiguration你從獲得實例Dropwizard在您的應用程序類的XDAO,最好在創建單身XDAO所以它有db_name和非空值host

以下問題的一部分,該代碼 - 你重新創建單例,但不給XDAO配置實例MongoConfiguration

public class XDAO implements IXDAO { 

//... snip 

/* Get singleton */ 
public static synchronized XDAO getSingleton(){ 
    if (instance == null){ 
    instance = new XDAO(); // no configuration information is included! 
    }  
    return instance; 
} 

/* constructor */ 
public XDAO(){ 
    initDatabase(); // this call needs db_name & host but you haven't set those yet!! 
    initDatabaseIndexes(); 
} 

我建議您修改應用程序類沿此線創建XDAO:

@Override 
public void run(final MongoConfiguration configuration, final Environment environment) { 

    XDAO XDAOsingleton = new XDAO(configuration); 
    XDAO.setSingletonInstance(XDAOsingleton); // You need to create this static method. 

    final MyResource resource = new MyResource(configuration.getDb_name(), configuration.getMongohost(), configuration.getMongoport()); // MyResource depends on XDAO so must be created after XAO's singleton is set 
    environment.jersey().register(resource); 
} 

您可能還需要取決於如果你保持public static synchronized XDAO getSingleton()

上採取 initDatabase()等了XDAO的構造

我還建議您將MyResource的構造函數更改爲public MyResource(XDAO xdao)。資源類看起來並不需要配置信息,最好將XDAO的依賴關係顯式化(然後您也不需要將XDAO單例保存在XDAO類的靜態字段中)。

相關問題