Custom SpringBoot Actuator for Triggering Maintenance Tasks
Note that this article is providing generic technical information about a narrow subject and does not represent a fully considered solution; especially with regard to security.
SpringBoot is a popular framework for developing application software with the Java programming language. One feature of SpringBoot is the “Actuator”. The Actuator facility allows for monitoring and management of the running system. It is able to expose data in a number of ways, but it’s HTTP-based interface is commonly used. Functionality exposed includes;
- metrics
- health checks
- caches
- logging
A possible runtime topology might look like this;
It is possible to create an Actuator that provides and interface for triggering maintenance tasks. The code below shows how this might be setup;
package myapp.maintenance;
import myapp.maintenance.model.MaintenanceService;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "mymaintenance")
public class MaintenanceEndpoint {
public enum Type {
DAILY,
HOURLY
}
private final MaintenanceService maintenanceService;
public MaintenanceEndpoint(MaintenanceService maintenanceService) {
this.maintenanceService = maintenanceService;
}
@WriteOperation
public String trigger(Type type) {
return switch (type) {
case DAILY -> {
maintenanceService.daily();
yield "ok";
}
case HOURLY -> {
maintenanceService.hourly();
yield "ok";
}
};
}
}
To configure the port on which the Actuator will be vended, set the property management.server.port
.
You need to enable the new custom actuator, edit the property management.endpoints.web.exposure
and add mymaintenance
to the list. One your application is running it will be possible to make HTTP requests to it in order to trigger this logic;
curl -X POST -H "Content-Type: application/json" \
--data '{"type":"HOURLY"}' "${URL_BASE}/actuator/mymaintenance"