luni, 5 mai 2025

Configure svenstaro/keycloak-http-webhook-provider for Keycloak 24.0.5

To configure svenstaro/keycloak-http-webhook-provider with Keycloak 24.0.5, I followed these steps:

Clone the repository: https://github.com/svenstaro/keycloak-http-webhook-provider

Create a Dockerfile in the root of the repository with the following content to build a custom Keycloak image that includes the webhook provider:

FROM quay.io/keycloak/keycloak:24.0.5-0

COPY target/keycloak_http_webhook_provider.jar /opt/keycloak/providers/

ENTRYPOINT ["/opt/keycloak/bin/kc.sh", "start-dev"]

Build the JAR

Build the Docker image:

docker buildx build --debug -t keycloak-http-webhook-provider .

Run the container with the required environment variables to configure the webhook:

docker run -e KC_SPI_EVENTS_LISTENER_HTTP_WEBHOOK_SERVER_URL=https://your-webhook-url \
           -e KC_SPI_EVENTS_LISTENER_HTTP_WEBHOOK_USERNAME=youruser \
           -e KC_SPI_EVENTS_LISTENER_HTTP_WEBHOOK_PASSWORD=yourpassword \
           -e KEYCLOAK_ADMIN=admin \
           -e KEYCLOAK_ADMIN_PASSWORD=admin \
           -p 8080:8080 keycloak-http-webhook-provider

luni, 28 aprilie 2025

Short Example: Streaming OpenAI Chat Completions in Java Using WebClient

 

Here is a short example of how to stream OpenAI chat completions using Spring's WebClient and Flux in Java.

First, set up your WebClient:

 

private final WebClient webClient;

public YourClassName(String openAiBaseUrl, String openAiApiKey) {
    this.webClient = WebClient.builder()
        .baseUrl(openAiBaseUrl)
        .defaultHeader("Authorization", "Bearer " + openAiApiKey)
        .build();
}

Then, send a request to stream chat completions:

public Flux<ChatCompletionResponseChunk> streamChatCompletion(ChatCompletionRequest request) {
    return webClient.post()
        .uri("/chat/completions")
        .header("Accept", "text/event-stream")
        .contentType(MediaType.APPLICATION_JSON)
        .bodyValue(request)
        .retrieve()
        .bodyToFlux(ChatCompletionResponseChunk.class)
        .onErrorResume(error -< {
            // See https://hilla.dev/blog/ai-chatbot-in-java/calling-chatgpt-and-openai-apis-in-spring-boot/
            // The stream terminates with a `[DONE]` message, which causes a serialization error.
            // Ignore this error and return an empty stream instead.
            if (error.getMessage().contains("JsonToken.START_ARRAY")) {
                return Flux.empty();
            } else {
                // If the error is not caused by the `[DONE]` message, propagate the error.
                return Flux.error(error);
            }
        });
}

sâmbătă, 29 martie 2025

Monitoring HikariCP Metrics Without Micrometer or OpenTelemetry

You can quickly monitor HikariCP connection pool metrics without enabling Micrometer or OpenTelemetry. Just follow these two simple steps:

1. Enable Debug Logging in logback-spring.xml. Add one of the following logging configurations: 

For general Hikari pool logging:

<logger name="com.zaxxer.hikari.pool.HikariPool" level="DEBUG" />
For more detailed logging:
<logger name="com.zaxxer.hikari" level="DEBUG" />
2. Set the Housekeeping Interval via VM Option Set the following JVM option to adjust the logging frequency (default is 30s; this reduces it to 5 seconds):
-Dcom.zaxxer.hikari.housekeeping.periodMs=5000

Example Log Output 

Once enabled, you’ll start seeing logs like this:

2025-03-29 16:30:07,869 DEBUG c.z.h.p.HikariPool[HikariPool-1 housekeeper] - HikariPool-1 - Pool stats (total=2, active=0, idle=2, waiting=0)

Breakdown of Pool Stats: 

  • total=2 - The total number of connections in the pool (both active and idle). 
  • active=0 - The number of connections currently in use (0 means no connections are being used). 
  • idle=2 - The number of available (idle) connections that are not in use. 
  • waiting=0 - The number of threads waiting for a connection (0 means no requests are waiting). 

By enabling this, you get real-time insights into your connection pool without additional monitoring tools.

joi, 27 martie 2025

How to Modify a File Inside a Running Docker Container

Let's assume we need to modify the file logback-spring.xml inside the container bb9d1daac389.

docker exec -it bb9d1daac389 sh
# cd /app
# vim logback-spring.xml
/bin/sh: 1: vim: not found
# apt update
# apt install vim
# vim logback-spring.xml
If the container is not based on an Ubuntu image, you can check the OS using cat /etc/os-release and use the corresponding package manager, such as apt, pacman, or another appropriate tool.

vineri, 7 februarie 2025

Kitty SSH Issue: No Output in top and htop Error

With Kitty, the top command in SSH shows nothing. I tried htop, but it returned an error.

What I did: Googled the issue and looked for a better solution.

Solution 1: Directly in the SSH terminal session:

TERM=xterm top

Solution 2: Add to local .ssh/config file

SetEnv TERM=xterm