public FanoutTransport() throws InterruptedIOException { // Setup a task that is used to reconnect the a connection async. reconnectTaskFactory = new TaskRunnerFactory(); reconnectTaskFactory.init(); reconnectTask = reconnectTaskFactory.createTaskRunner( new Task() { @Override public boolean iterate() { return doConnect(); } }, "ActiveMQ Fanout Worker: " + System.identityHashCode(this)); }
public void stopAsync() { // If we're in the middle of starting then go no further... for now. synchronized (this) { pendingStop = true; if (starting) { // log return; } } if (stopping.compareAndSet(false, true)) { if (context != null) { context.getStopping().set(true); } try { stopTaskRunnerFactory.execute( new Runnable() { @Override public void run() { serviceLock.writeLock().lock(); try { doStop(); } catch (Throwable e) { // LOG } finally { stopped.countDown(); serviceLock.writeLock().unlock(); } } }); } catch (Throwable t) { // LOG stopped.countDown(); } } }
@Override public void stop() throws Exception { try { synchronized (reconnectMutex) { ServiceStopper ss = new ServiceStopper(); if (!started) { return; } started = false; disposed = true; connected = false; for (Iterator<FanoutTransportHandler> iter = transports.iterator(); iter.hasNext(); ) { FanoutTransportHandler th = iter.next(); if (th.transport != null) { ss.stop(th.transport); } } LOG.debug("Stopped: " + this); ss.throwFirstException(); } } finally { reconnectTask.shutdown(); reconnectTaskFactory.shutdownNow(); } }
public void delayedStop(final int waitTime, final String reason, Throwable cause) { if (waitTime > 0) { synchronized (this) { pendingStop = true; stopError = cause; } try { stopTaskRunnerFactory.execute( new Runnable() { @Override public void run() { try { Thread.sleep(waitTime); stopAsync(); } catch (InterruptedException e) { } } }); } catch (Throwable t) { // log error } } }
@Override protected void doStop(ServiceStopper stopper) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("Stopping transport " + this); } // Closing the streams flush the sockets before closing.. if the socket // is hung.. then this hangs the close. // closeStreams(); if (socket != null) { if (closeAsync) { // closing the socket can hang also final CountDownLatch latch = new CountDownLatch(1); // need a async task for this final TaskRunnerFactory taskRunnerFactory = new TaskRunnerFactory(); taskRunnerFactory.execute( new Runnable() { public void run() { LOG.trace("Closing socket {}", socket); try { socket.close(); LOG.debug("Closed socket {}", socket); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug( "Caught exception closing socket " + socket + ". This exception will be ignored.", e); } } finally { latch.countDown(); } } }); try { latch.await(1, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { taskRunnerFactory.shutdownNow(); } } else { // close synchronously LOG.trace("Closing socket {}", socket); try { socket.close(); LOG.debug("Closed socket {}", socket); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug( "Caught exception closing socket " + socket + ". This exception will be ignored.", e); } } } } }