@Override public void close() { this.running = false; LOG.info("Closing source"); if (client != null) { // client seems to be thread-safe client.stop(); } // leave main method synchronized (waitLock) { waitLock.notify(); } }
@Override public void reconnect() { clientBase.reconnect(); }
public BasicClient(String name, Hosts hosts, StreamingEndpoint endpoint, Authentication auth, boolean enableGZip, HosebirdMessageProcessor processor, ReconnectionManager reconnectionManager, RateTracker rateTracker, ExecutorService executorService, @Nullable BlockingQueue<Event> eventsQueue, HttpParams params, SchemeRegistry schemeRegistry) { Preconditions.checkNotNull(auth); HttpClient client; if (enableGZip) { client = new RestartableHttpClient(auth, enableGZip, params, schemeRegistry); } else { DefaultHttpClient defaultClient = new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry), params); /** Set auth **/ auth.setupConnection(defaultClient); client = defaultClient; } this.canRun = new AtomicBoolean(true); this.executorService = executorService; this.clientBase = new ClientBase(name, client, hosts, endpoint, auth, processor, reconnectionManager, rateTracker, eventsQueue); }
/** * Stops the current connection. No reconnecting will occur. Kills thread + cleanup. * Waits for the loop to end **/ public void stop(int waitMillis) throws InterruptedException { try { if (!isDone()) { setExitStatus(new Event(EventType.STOPPED_BY_USER, String.format("Stopped by user: waiting for %d ms", waitMillis))); } if (!waitForFinish(waitMillis)) { logger.warn("{} Client thread failed to finish in {} millis", name, waitMillis); } } finally { rateTracker.shutdown(); } }
public boolean isDone() { return clientBase.isDone(); }
@Override public String getName() { return clientBase.getName(); }
public void shutdown(int millis) { try { stop(millis); } catch (InterruptedException e) { logger.warn("Client failed to shutdown due to interruption", e); } }
/** * This method should only be called after the client is done */ public Event getExitEvent() { return clientBase.getExitEvent(); }
@Override public StreamingEndpoint getEndpoint() { return clientBase.getEndpoint(); }
@Override public StatsReporter.StatsTracker getStatsTracker() { return clientBase.getStatsTracker(); }
@VisibleForTesting boolean waitForFinish(int millis) throws InterruptedException { return clientBase.waitForFinish(millis); } }
public void restart() { HttpClient old = underlying.get(); if (old != null) { // this will kill all of the connections and release the resources for our old client old.getConnectionManager().shutdown(); } setup(); }
private void setExitStatus(Event event) { logger.info("{} exit event - {}", name, event.getMessage()); addEvent(event); exitEvent.set(event); }
public void close() { if (this.request != null && !this.request.isAborted()) { // aborting the request this.request.abort(); } if (client instanceof RestartableHttpClient) { // restart the entire client ((RestartableHttpClient) client).restart(); } try { Closeables.close(this.stream, true); } catch (IOException e) { throw new RuntimeException(e); // should never happen } } }
@Override public void stop() { stop(DEFAULT_STOP_TIMEOUT_MILLIS); }
public Event getExitEvent() { if (!isDone()) { throw new IllegalStateException(name + " Still running"); } return exitEvent.get(); }
@Override public String toString() { return String.format("%s, endpoint: %s", getName(), endpoint.getURI()); }
@Override public void stop(int waitMillis) { logger.info("Stopping the client: " + this.clientBase); try { clientBase.stop(waitMillis); logger.info("Successfully stopped the client: {}", this.clientBase); } catch (InterruptedException e) { logger.info("Thread interrupted when attempting to stop the client: {}", this.clientBase); } executorService.shutdown(); }
/** * {@inheritDoc} * Forks a new thread to do the IO in. */ @Override public void connect() { if (!canRun.compareAndSet(true, false) || clientBase.isDone()) { throw new IllegalStateException("There is already a connection thread running for " + this.clientBase); } executorService.execute(clientBase); logger.info("New connection executed: {}", this.clientBase); }