سؤال

I am implementing a health check for my application.I have configured the classes for different logical systems in our application and have written methods which check for conditions across the environment like db count , logging errors , cpu process etc.

Now I have requirement where I have to check only certain conditions ie certain methods in the class according to the host.

What is the best way to access those methods via property file ? Please give your suggestions.

Thanks.

هل كانت مفيدة؟

المحلول

I don't like using reflection for this sort of thing. Its too easy for the property files to be changed and then the system starts generating funky error messages.

I prefer something straightforward like:

controlCheck.properties:

dbCount=true
logger=false
cpuProcess=true

Then the code is sort of like this (not real code):

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 ...
}

There are lots of ways to do it but this is simple and pretty much foolproof. All the configuration takes place in one properties file and all the code is in one place and easy for a Java programmer to comment out tests that are not relevant or to add new tests. If you add a new test, it doesn't automatically get run everywhere so you can roll it out on your own schedule and you can add a new test with a simple shell script, if you like that.

If its not running a particular test when you think it should, there's only two things to check:

  • Is it in the properties file?
  • Is the version of the code correct?

نصائح أخرى

You can define different beans for every check you need:

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

Then a bean for HealtCheck with operations bean injected:

<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();
  }
}

If with this solution you can't reload file, in HealthChecker remove activeChecker a property public void setPropertiesLocation(URL propertiesLocation); let HealthChecker implements InitializingBean and load properties in afterPropertiesSet()

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top