SseEventSource.open() takes too long

Viewed 170

I am using javax.ws.rs.sse.SseEventSource to listen to events sent by my API. Here is my implementation:

        String bearer = getBearerToken();
        Client client = ClientBuilder.newClient().register((ClientRequestFilter) clientRequestContext -> {
             MultivaluedMap<String, Object> headers = clientRequestContext.getHeaders();
             headers.add("Authorization", "Bearer " + bearer);
        });

        SseEventSource.target(client.target(url)).build(); 
        sseEventSource.register(this::onMessage, this::onError);
        sseEventSource.open();

Everthing works great except for the fact that sseEventSource.open() blocks for a substantial amount of time (15s-40s). When I print the thread state I get the following:

"main" #1 prio=5 os_prio=31 cpu=2616.95ms elapsed=34.80s tid=0x00007fbd38009800 nid=0x1703 waiting on condition  [0x00007000046a7000]
   java.lang.Thread.State: WAITING (parking)
    at jdk.internal.misc.Unsafe.park(java.base@11.0.6/Native Method)
    - parking to wait for  <0x000000070d8cbe00> (a java.util.concurrent.CountDownLatch$Sync)
    at java.util.concurrent.locks.LockSupport.park(java.base@11.0.6/LockSupport.java:194)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(java.base@11.0.6/AbstractQueuedSynchronizer.java:885)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(java.base@11.0.6/AbstractQueuedSynchronizer.java:1039)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(java.base@11.0.6/AbstractQueuedSynchronizer.java:1345)
    at java.util.concurrent.CountDownLatch.await(java.base@11.0.6/CountDownLatch.java:232)
    at org.jboss.resteasy.plugins.providers.sse.client.SseEventSourceImpl$EventHandler.awaitConnected(SseEventSourceImpl.java:417)
    at org.jboss.resteasy.plugins.providers.sse.client.SseEventSourceImpl.open(SseEventSourceImpl.java:174)
    at org.jboss.resteasy.plugins.providers.sse.client.SseEventSourceImpl.open(SseEventSourceImpl.java:163)
    at org.jboss.resteasy.plugins.providers.sse.client.SseEventSourceImpl.open(SseEventSourceImpl.java:158)

I was wondering what is the thread waiting for, is it expecting something to be published in the stream before proceeding (if so, is it possible to configure it otherwise)? Anyway, my main question is how to have a faster SseEventSource.open()?


Update: I found out that SseEventSource.open() is waiting to receive a message for the first time. It is unfortunate because the actual connection is established quickly and I have to wait about 30s for the method to return. So far I was not able to find out a good solution for this.

0 Answers
Related