Adding a custom health module
Adding a new custom module to the Spring Boot application is not so complex. To demonstrate this feature, assume that if a service gets more than two transactions in a minute, then the server status will be set as Out of Service.
In order to customize this, we have to implement the HealthIndicator
interface and override the health
method. The following is a quick and dirty implementation to do the job:
class TPSCounter { LongAdder count; int threshold = 2; Calendar expiry = null; TPSCounter(){ this.count = new LongAdder(); this.expiry = Calendar.getInstance(); this.expiry.add(Calendar.MINUTE, 1); } boolean isExpired(){ return Calendar.getInstance().after(expiry); } boolean isWeak(){ return (count.intValue() > threshold); } void increment(){ count.increment(); } }
The preceding class is a simple POJO class that maintains the transaction counts in the window. The isWeak
method checks whether the transaction...