2013-08-16 135 views
1

我實現我的application.I健康檢查已配置在我們的應用程序不同的邏輯系統中的類,並寫入了檢查像分貝數,記錄錯誤,CPU處理等整個環境狀況的方法使用spring/pojo在類中調用某些方法?

現在,我已經要求我必須只檢查某些條件,即根據主機在班級中的某些方法。

通過屬性文件訪問這些方法的最佳方式是什麼?請給出你的建議。

謝謝。

回答

0

我不喜歡使用反射這種事情。對屬性文件進行更改太容易,然後系統開始生成時髦的錯誤消息。

我更喜歡簡單的東西:

controlCheck.properties:

dbCount=true 
logger=false 
cpuProcess=true 

然後代碼是有點像這樣(沒有真正的代碼):

Properties props = ... read property file 

boolean isDbCount = getBoolean(props, "dbCount"); // returns false if prop not found 
... repeat for all others ... 

CheckUtilities.message("Version " + version); // Be sure status show version of this code. 
if (isDbCount) { 
    CheckUtilities.checkDbCount(); // logs all statuses 
} 
if (... other properties one by one ...) { 
    ... run the corresponding check ... 
} 

有很多方法來做到這一點,但這很簡單,非常簡單。所有的配置都在一個屬性文件中進行,所有的代碼都放在一個地方,Java程序員很容易將註釋與不相關的測試進行註釋或添加新的測試。如果你添加一個新的測試,它不會自動到處運行,所以你可以按照自己的時間表推出它,如果你喜歡,你可以用一個簡單的shell腳本添加一個新的測試。

如果它沒有運行特定的測試,當你認爲它應該,這裏只有兩件事情來檢查:

  • 是它的屬性文件?
  • 代碼的版本是否正確?
+1

我使用了這種方法...非常感謝 –

0

您可以定義不同的豆子,每檢查你需要:

<bean id="dbcountBean" class="DBCountHealtCheck" scope="prototype"> 
    <!-- bean properties --> 
</bean> 

那麼對於HealtCheck一個bean與操作豆注入:

<bean id="healtChecker" class="HealtChecker" scope="prototype"> 
    <property name="activeChecker"><bean class="PropertiesFactoryBean> 
    <property name="location">file:path/to/file</property></bean> 
    </property> 
    <property name="dbCountCheck" ref="dbCountBean" /> 
<!-- other properties --> 
</bean> 

class HealtChecker { 
    private DBCountHealtCheck dbCountChecker; 
    private Properties activeChecker; 

    public void setDbcount(DBCountHealtCheck dbCountChecker) { 
    this.dbCountChecker = dbCountChecker; 
    } 

    public void setActiveChecker(Properties activeChecker) { 
    this.activeChecker = activeChecker; 
    } 

    public void check() { 
    if("true".equals(this.activeChecker.get("dbCount")) { 
     this.dbCountChecker.check(); 
    } 
} 

如果與此解決方案您無法重新加載文件,在HealthChecker刪除activeChecker屬性public void setPropertiesLocation(URL propertiesLocation);HealthChecker執行InitializingBean和負載屬性afterPropertiesSet()

相關問題