private synchronized void internalDisconnect()

in dbus-java/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java [524:582]


    private synchronized void internalDisconnect() {

        if (connected == false) { // already disconnected
            return;
        }

        logger.debug("Sending disconnected signal");
        try {
            handleMessage(new org.freedesktop.dbus.interfaces.Local.Disconnected("/"), false);
        } catch (Exception ex) {
            logger.debug("Exception while disconnecting", ex);
        }


        logger.debug("Disconnecting Abstract Connection");

        workerThreadPoolLock.writeLock().lock();
        try {
            // try to wait for all pending tasks.
            workerThreadPool.shutdown();
            workerThreadPool.awaitTermination(10, TimeUnit.SECONDS); // 10 seconds should be enough, otherwise fail

        } catch (InterruptedException _ex) {
            logger.error("Interrupted while waiting for worker threads to be terminated.", _ex);
        } finally {
            workerThreadPoolLock.writeLock().unlock();
        }

        // shutdown sender executor service, send all remaining messages in main thread
        for (Runnable runnable : senderService.shutdownNow()) {
            runnable.run();
        }

        // stop the main thread
        run = false;
        connected = false;

        readerThread.setTerminate(true);

        // disconnect from the transport layer
        try {
            if (transport != null) {
                transport.close();
                transport = null;
            }
        } catch (IOException exIo) {
            logger.debug("Exception while disconnecting transport.", exIo);
        }

        // stop all the workers
        workerThreadPoolLock.writeLock().lock();
        try {
            if (!workerThreadPool.isTerminated()) { // try forceful shutdown
                workerThreadPool.shutdownNow();
            }
        } finally {
            workerThreadPoolLock.writeLock().unlock();
        }
    }