private void shutdown() { if (state.getAndSet(EventProcessor.State.CLOSED) != EventProcessor.State.CLOSED) { // shut down only if has not been shut down before LOGGER.debugLog("Shutting down event processing."); executor.close(); } }
shutdown(); try { if (!executor.awaitTermination(timeout, unit)) { LOGGER.log(CONNECTION_ERROR_LEVEL, LocalizationMessages.EVENT_SOURCE_SHUTDOWN_TIMEOUT(target.getUri().toString()));
/** * Open the connection to the supplied SSE underlying {@link WebTarget web target} and start processing incoming * {@link InboundEvent events}. * * @throws IllegalStateException in case the event source has already been opened earlier. */ public void open() { if (!state.compareAndSet(EventProcessor.State.READY, EventProcessor.State.OPEN)) { switch (state.get()) { case OPEN: throw new IllegalStateException(LocalizationMessages.EVENT_SOURCE_ALREADY_CONNECTED()); case CLOSED: throw new IllegalStateException(LocalizationMessages.EVENT_SOURCE_ALREADY_CLOSED()); } } EventProcessor.Builder builder = EventProcessor.builder(target, state, executor, this, shutdownHandler) .boundListeners(boundListeners) .unboundListeners(unboundListeners) .reconnectDelay(reconnectDelay, TimeUnit.MILLISECONDS); if (disableKeepAlive) { builder.disableKeepAlive(); } EventProcessor processor = builder.build(); executor.submit(processor); // return only after the first request to the SSE endpoint has been made processor.awaitFirstContact(); }
private EventSource(final WebTarget target, final String name, final long reconnectDelay, final boolean disableKeepAlive, final boolean open) { if (target == null) { throw new NullPointerException("Web target is 'null'."); } this.target = SseFeature.register(target); this.reconnectDelay = reconnectDelay; this.disableKeepAlive = disableKeepAlive; final String esName = (name == null) ? createDefaultName(target) : name; this.executor = new CloseableClientExecutor(Executors.newSingleThreadScheduledExecutor( new ThreadFactoryBuilder().setNameFormat(esName + "-%d") .setDaemon(true) .build())); if (open) { open(); } }