⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmsconnection.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        try {
            if (_stopped) {
                // propagate the start to all the associated sessions. When
                // that is complete transition the state of the connection to
                // RUNNING
                Enumeration sessions = _sessions.elements();
                while (sessions.hasMoreElements()) {
                    JmsSession session = (JmsSession) sessions.nextElement();
                    session.start();
                }
                // set the  state of the connection to start
                _stopped = false;
            }
        } catch (JMSException exception) {
            // do we need to change _stopped to true if the any of the
            // sessions fail to start
            throw exception;
        }
    }

    // implementation of Connection.stop
    public synchronized void stop() throws JMSException {
        ensureOpen();
        setModified();

        if (!_stopped) {
            // propagate the stop to all the encapsulated sessions before
            // changing the state of the connection. Only when all the
            // sessions have successfully transitioned, without exception,
            // do we change the state of the connection
            Enumeration sessions = _sessions.elements();
            while (sessions.hasMoreElements()) {
                JmsSession session = (JmsSession) sessions.nextElement();
                session.stop();
            }

            // set the state of the connection to stopped before stopping
            // all the enclosed sessions
            _stopped = true;
        }
    }

    // implementation of Connection.close
    public synchronized void close() throws JMSException {
        if (!_closed) {
            try {
                // before we close we should stop the connection and any
                // associated sessions
                stop();

                // propagate the close to all the encapsulated sessions before
                // changing the state of the connection. Only when all the
                // sessions have successfully transitioned, without exception,
                // do we change the state of the connection. All the sessions
                // are removed from the connection.
                Object sessions[] = _sessions.toArray();
                for (int i = 0; i < sessions.length; ++i) {
                    JmsSession session = (JmsSession) sessions[i];
                    session.close();
                    // the session deregisters itself with the connection via
                    // removeSession()
                }

                // send a close to the stub and then null the stub
                getJmsConnectionStub().close();
                _stub = null;

                // remove yourself from the list of connections managed by the
                // connection factory and then null the factory.
                _factory.removeConnection(this);
                _factory = null;

                // set the closed flag so calling it multiple times is
                // cool
                _closed = true;
            } finally {
                stopEventManager();
            }
        }
    }

    /**
     * Release all resources used by this connection, including supporting
     * sessions
     *
     * @throws JMSException - error completing this request
     */
    public synchronized void destroy() throws JMSException {
        if (!_closed) {
            try {
                // propagate the close to all the encapsulated sessions before
                // changing the state of the connection. Only when all the
                // sessions have successfully transitioned, without exception,
                // do we change the state of the connection. All the sessions
                // are removed from the connection.
                Object sessions[] = _sessions.toArray();
                for (int i = 0; i < sessions.length; ++i) {
                    JmsSession session = (JmsSession) sessions[i];
                    session.destroy();
                }

                // send a close to the stub and then null the stub
                getJmsConnectionStub().destroy();
                _stub = null;

                // remove yourself from the list of connections managed by the
                // connection factory and then null the factory.
                _factory.removeConnection(this);
                _factory = null;

                // set the closed flag so calling it multiple times is
                // cool
                _closed = true;
            } finally {
                stopEventManager();
            }
        }
    }

    /**
     * Return the identity of this connection. An identity is created by the
     * server and is unique within that servers environment
     *
     * @return      String
     */
    public String getConnectionId() {
        return _connectionId;
    }

    /**
     * Return an instance of the remote server connection. If one has not
     * been assigned then throw the JMSException exception
     *
     * @return      JmsConnectionStub   connection to the server
     * @throws   JMSException
     */
    JmsConnectionStubIfc getJmsConnectionStub() throws JMSException {
        if (_stub == null) {
            throw new JMSException("The connectionstub is set to null");
        }

        return _stub;
    }

    /**
     * Add the specified session to the list of managed sessions
     *
     * @param       session         session to register
     */
    protected void addSession(JmsSession session) {
        _sessions.addElement(session);
    }

    /**
     * Remove the specified session from the list of managed sessions.
     * If it doesn't exist then fail silently
     *
     * @param       session         session to remove
     */
    protected void removeSession(JmsSession session) {
        _sessions.removeElement(session);
    }

    /**
     * Test whether the specified session is managed by this connection
     *
     * @param       session         session to test against
     * @return      boolean         true if managed
     */
    protected boolean isManaged(JmsSession session) {
        return _sessions.contains(session);
    }

    /**
     * Returns an enumeration of all sessions managed by this connection
     *
     * @return an enumeration of all sessions managed by this connection
     */
    protected Enumeration getSessions() {
        return _sessions.elements();
    }

    /**
     * Return the running state of the connection
     *
     * @return <code>true</code> if stopped
     */
    protected boolean isStopped() {
        return _stopped;
    }

    /**
     * Flags this connection as being modified. Subsequent attempts
     * to invoke {@link #setClientID} will result in an
     * <code>IllegalStateException</code> being thrown
     */
    protected void setModified() {
        _modified = true;
    }

    /**
     * Delete the temporary destination and all the registered sessions
     * consumers waiting to receive messages from this destination will
     * be stopped.
     * <p>
     * It will throw a JMSException if the specified destination is not
     * temporary or if the destination is null or if the destination is
     * not owned by this connection
     *
     * @param       destination         temporary destination to delete
     * @throws   JMSException
     */
    synchronized void deleteTemporaryDestination(JmsDestination destination)
        throws JMSException {
        if ((destination != null) &&
            (destination instanceof JmsTemporaryDestination)) {
            JmsTemporaryDestination temp_dest =
                (JmsTemporaryDestination) destination;

            // check to see that this destination was actually created by
            // this connection
            if (temp_dest.getOwningConnection() == this) {
                // this is currently a no-op but we probably need a way to
                // clean up on the server side
            } else {
                throw new JMSException("The temp destination cannot be " +
                    "used outside the scope of the connection creating it");
            }
        } else {
            throw new JMSException("The destination is not temporary");
        }
    }

    /**
     * Verifies that the connection is open
     *
     * @throws IllegalStateException if the connection is closed
     */
    protected void ensureOpen() throws IllegalStateException {
        if (_closed) {
            throw new IllegalStateException(
                "Cannot perform operation - session has been closed");
        }
    }

    /**
     * This method will start the event manager service on the first connection
     * that is created
     */
    private static void startEventManager() {
        try {
            if (_activeConnections++ == 0) {
                try {
                    BasicEventManager.instance().start();
                } catch (ServiceException exception) {
                    // ignore
                }
            }
        } catch (Exception exception) {
            _log.error(exception.getMessage(), exception);
        }
    }

    /**
     * This method will terminate the event manager service when the last
     * connection has been closed
     */
    private static void stopEventManager() {
        try {
            if (--_activeConnections == 0) {
                BasicEventManager.instance().stop();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

} //-- JmsConnection

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -