void ActiveMQSessionKernel::dispose()

in activemq-cpp/src/main/activemq/core/kernels/ActiveMQSessionKernel.cpp [320:418]


void ActiveMQSessionKernel::dispose() {

    // Prevent Dispose loop if transaction has a close synchronization registered.
    if (!closed.compareAndSet(false, true)) {
        return;
    }

    class Finalizer {
    private:

        ActiveMQSessionKernel* session;
        ActiveMQConnection* connection;

    private:

        Finalizer(const Finalizer&);
        Finalizer& operator=(const Finalizer&);

    public:

        Finalizer(ActiveMQSessionKernel* session, ActiveMQConnection* connection) :
            session(session), connection(connection) {
        }

        ~Finalizer() {
            Pointer<ActiveMQSessionKernel> session(this->session);
            try {
                this->connection->removeSession(session);
            } catch(...) {
                session.release();
            }
            session.release();
        }
    };

    try {

        Finalizer final(this, this->connection);

        // Stop the dispatch executor.
        stop();

        // Dispose of all Consumers, the dispose method skips the RemoveInfo command.
        this->config->consumerLock.writeLock().lock();
        try {
            // We have to copy all the consumers to another list since we aren't using a
            // CopyOnWriteArrayList right now.
            ArrayList<Pointer<ActiveMQConsumerKernel> > consumers(this->config->consumers);
            Pointer<Iterator< Pointer<ActiveMQConsumerKernel> > > consumerIter(consumers.iterator());
            while (consumerIter->hasNext()) {
                try{
                    Pointer<ActiveMQConsumerKernel> consumer = consumerIter->next();
                    consumer->setFailureError(this->connection->getFirstFailureError());
                    consumer->dispose();
                    this->lastDeliveredSequenceId =
                        Math::max(this->lastDeliveredSequenceId, consumer->getLastDeliveredSequenceId());
                } catch (cms::CMSException& ex) {
                    /* Absorb */
                }
            }
            this->config->consumers.clear();
            this->config->consumerLock.writeLock().unlock();
        } catch (Exception& ex) {
            this->config->consumerLock.writeLock().unlock();
            throw;
        }

        // Dispose of all Producers, the dispose method skips the RemoveInfo command.
        this->config->producerLock.writeLock().lock();
        try {
            // We have to copy all the producers to another list since we aren't using a
            // CopyOnWriteArrayList right now.
            ArrayList<Pointer<ActiveMQProducerKernel> > producers(this->config->producers);
            std::auto_ptr<Iterator<Pointer<ActiveMQProducerKernel> > > producerIter(producers.iterator());

            while (producerIter->hasNext()) {
                try{
                    producerIter->next()->dispose();
                } catch (cms::CMSException& ex) {
                    /* Absorb */
                }
            }
            this->config->producers.clear();
            this->config->producerLock.writeLock().unlock();
        } catch (Exception& ex) {
            this->config->producerLock.writeLock().unlock();
            throw;
        }

        // Roll Back the transaction since we were closed without an explicit call
        // to commit it.
        if (this->transaction->isInTransaction()) {
            this->transaction->rollback();
        }
    }
    AMQ_CATCH_RETHROW( ActiveMQException )
    AMQ_CATCH_EXCEPTION_CONVERT( Exception, ActiveMQException )
    AMQ_CATCHALL_THROW( ActiveMQException )
}