# Custom Acutator Endpoints

```java
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Map;

@Component
@Endpoint(id = "activeSessions")
public class ActiveSessionsEndpoint {

    private final SessionRegistry sessionRegistry;

    public ActiveSessionsEndpoint(SessionRegistry sessionRegistry) {
        this.sessionRegistry = sessionRegistry;
    }

    @ReadOperation
    public Map<String, Integer> activeSessionsCount() {
        return Collections.singletonMap("activeSessions", sessionRegistry.getAllPrincipals().size());
    }
}
```

**TL;DR:**

The `@Endpoint` annotation in Spring Boot Actuator allows for the creation of custom endpoints to expose specific application metrics or operations. In the provided code snippet, a custom endpoint `activeSessions` is defined to report the current number of active sessions, leveraging the `SessionRegistry` for real-time session tracking. This addition enhances monitoring capabilities by providing insights into user engagement and system load.

**Detailed Explanation:**

* **Purpose of @Endpoint:** The `@Endpoint` annotation marks a class as a custom Actuator endpoint, enabling it to expose specific data or operations through the Actuator framework. It's a powerful feature for extending the built-in monitoring and management capabilities of Spring Boot.
* **Exposing Active Sessions Count:** The `activeSessions` endpoint is designed to help monitor the current load on the application by reporting the number of active user sessions. This information is crucial for understanding user engagement and for scaling and performance tuning efforts.
* **Utilizing SessionRegistry:** The `SessionRegistry` is a Spring Security class that tracks all active sessions. By injecting `SessionRegistry` into the custom endpoint, it's possible to count and report the number of active sessions in real-time.
* **Accessing the Custom Endpoint:**
  * **HTTP GET Request:** `http://localhost:8080/actuator/activeSessions`
  * **Response Example:**

    ```json
    {
      "activeSessions": 42
    }
    ```

    This response indicates the current number of active sessions, providing immediate insight into application usage.

**Benefits:**

* **Enhanced Monitoring:** Adding a custom endpoint for active sessions directly addresses the need for real-time monitoring of user engagement, offering a clear view of the application's current load.
* **Operational Efficiency:** With this endpoint, administrators and developers can quickly assess the application's performance and responsiveness, enabling proactive adjustments to infrastructure and application settings.
* **Strategic Decision Making:** Information on active sessions aids in capacity planning and scalability strategies, ensuring that the application can handle peak loads efficiently.

By integrating a custom Actuator endpoint to monitor active sessions, Spring Boot applications can significantly improve their operational monitoring and management capabilities. This approach not only provides valuable insights into application performance but also supports informed decision-making for enhancing user experience and system reliability.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.katyella.com/springboot/recipes/custom-acutator-endpoints.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
