How can consume data from event listener

Hello everyone, y have a problem, i have an event listener, the event listener is working and give me the username for an user login, but i need consume that username, i using this method

public static void postUsername(String username) {
try {
URL url = new URL(“http://localhost:8081/receive-username”);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “application/json; utf-8”);

        String requestBody = "{\"username\": \"" + username + "\"}";

        OutputStream os = conn.getOutputStream();
        os.write(requestBody.getBytes());
        os.flush();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            log.info("Se envio username correctamente");
        } else {
            log.warn("No se pudo enviar el username");
        }

        conn.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

but i have this error

2024-04-01 17:03:57 java.net.ConnectException: Connection refused
2024-04-01 17:03:57 at java.base/sun.nio.ch.Net.connect0(Native Method)
2024-04-01 17:03:57 at java.base/sun.nio.ch.Net.connect(Net.java:579)
2024-04-01 17:03:57 at java.base/sun.nio.ch.Net.connect(Net.java:568)
2024-04-01 17:03:57 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:593)
2024-04-01 17:03:57 at java.base/java.net.Socket.connect(Socket.java:633)
2024-04-01 17:03:57 at java.base/java.net.Socket.connect(Socket.java:583)
2024-04-01 17:03:57 at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:533)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:638)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.(HttpClient.java:281)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:386)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:408)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1309)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1057)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1430)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1401)
2024-04-01 17:03:57 at it.aboutbits.ExternalAPIClient.postUsername(ExternalAPIClient.java:27)
2024-04-01 17:03:57 at it.aboutbits.SessionRestrictorEventListenerProvider.onEvent(SessionRestrictorEventListenerProvider.java:33)
2024-04-01 17:03:57 at org.keycloak.events.EventBuilder.sendNow(EventBuilder.java:266)
2024-04-01 17:03:57 at org.keycloak.events.EventBuilder.send(EventBuilder.java:253)
2024-04-01 17:03:57 at org.keycloak.events.EventBuilder.success(EventBuilder.java:221)
2024-04-01 17:03:57 at org.keycloak.services.managers.AuthenticationManager.finishedRequiredActions(AuthenticationManager.java:1031)
2024-04-01 17:03:57 at org.keycloak.authentication.AuthenticationProcessor.authenticationComplete(AuthenticationProcessor.java:1133)
2024-04-01 17:03:57 at org.keycloak.authentication.AuthenticationProcessor.authenticationAction(AuthenticationProcessor.java:995)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.processFlow(LoginActionsService.java:364)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.processAuthentication(LoginActionsService.java:335)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.authenticate(LoginActionsService.java:327)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.authenticateForm(LoginActionsService.java:392)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService$quarkusrestinvoker$authenticateForm_32b8e198ac3110abd1d5774e83a4cf87858129f4.invoke(Unknown Source)
2024-04-01 17:03:57 at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
2024-04-01 17:03:57 at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:141)
2024-04-01 17:03:57 at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:147)
2024-04-01 17:03:57 at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:582)
2024-04-01 17:03:57 at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
2024-04-01 17:03:57 at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
2024-04-01 17:03:57 at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
2024-04-01 17:03:57 at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
2024-04-01 17:03:57 at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
2024-04-01 17:03:57 at java.base/java.lang.Thread.run(Thread.java:840)

i tested the receive-api in postman and working very good, but when i deployed in the event listener i having that error, if somebody can help me to solve this.

Two things to start, please put code and logs in code blocks, it makes everyones life easier and don’t post the same thing twice, it will divert efforts from people who may want to help you and others.

How are you deploying keycloak?
Is it running in a container?

3 Likes

Sorry for that, I will keep it in mind for the next time, and yes, i am using keycloak in a docker container.

localhost inside the container will be limited to the containers network namespace.
If you want to reach another service you’ll have to make them reachable for each other.

There are a couple of options

  • using the host network
  • run both via compose and connect to the name of the other container
  • use host.docker.internal

each have their individual drawbacks.

thanks for your help, i am going to do that.