Search code examples
springmethodshealth-monitoring

Call certain methods alone in a class using spring/pojo?


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.


Solution

  • 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?