Uncategorized

Custom Endpoints Under /actuator

Spring “actuator” is an add on feature which will help you to monitor and manage applications using various end points. These end ponits are invoked using JMX as well as HTTP proptocols. And these endpoints can also be enabled/ disabled depending on the environment Or requirements. All these endpoints are availabe under/actuator base path in a spring boot application which has actuator as the dependency in it.

We can even add custom endpoints under /actuator base path if we want to expose additional end points apart from what is already exposed. The operations on these custom end points can be invoked via JMX as well as HTTP protocol.

Actuator has provided some of the following classes to expose custom endpoints under /actutor base path. And there are some custom Annotation which can expose technology specific end points like @JMXEndPoint or @WebEndPoint too.

import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;

Writing some custom end points

import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;

@Component
@Endpoint(id = "custom")
public class CustomBean {

	@ReadOperation(produces = MediaType.APPLICATION_JSON_VALUE)
	public String getCustomData() {
		Gson gson = new Gson();
		return gson.toJson(new CustomData("test", 5));
	}

	@ReadOperation(produces = MediaType.APPLICATION_JSON_VALUE)
	public String getCustomDataByName(@Selector String name) {
		Gson gson = new Gson();
		return gson.toJson(new CustomData(name, 5));
	}

	@WriteOperation(produces = MediaType.APPLICATION_JSON_VALUE)
	public String updateCustomData(String name, int counter) {
		CustomData customData = new CustomData(name, counter);
		return "custom data updated";
	}
	
	@WriteOperation(produces = MediaType.APPLICATION_JSON_VALUE)
	public String updateCustomDataName(@Selector String name) {
		CustomData customData = new CustomData(name,5);
		return "custom data updated";
	}
	
	@DeleteOperation
	public String deleteCustomData(@Selector String name) {
		
		return "success";
	}
}

These end can be found under actuator as,

And In JMX console as

This is an excellent way of adding additional application management end points in spring applications.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s