@Override public Optional<AutoCloseable> subscribe(@NonNull String rel, @NonNull SSEObserver<S> observer, @NonNull String mainEventName, long reconnectionTime) { checkArgument(reconnectionTime > 0); CallContext pair = resolve(rel); if (pair == null) { return Optional.empty(); } // this hack is needed because of // https://github.com/jersey/jersey/pull/3600 // setting a global last event id header via LastEventIdHeaderFilter and // synchronizing all sse // sources :-( synchronized (lock) { // this hack is needed because of // https://github.com/jersey/jersey/pull/3600 if (observer.lastKnownEventId().isPresent()) { pair.target.property(SseFeature.LAST_EVENT_ID_HEADER, observer.lastKnownEventId().get()); } EventSource eventSource = EventSource.target(pair.target).named("SSE" + UUID.randomUUID()) .usePersistentConnections().reconnectingEvery(reconnectionTime, TimeUnit.MILLISECONDS).build(); SSEListener<S> sseListener = new SSEListener<>(responseClass, responseBuilder, observer, mainEventName, uri); eventSource.register(sseListener); EventSourceWithCloseGuard ev = new EventSourceWithCloseGuard(eventSource, reconnectionTime, sseListener); ev.open(); return Optional.of(ev); } }