jmsconnection.java

来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 643 行 · 第 1/2 页

JAVA
643
字号
                    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;        }    }    /**     * Temporarily stops a connection's delivery of incoming messages. Delivery     * can be restarted using the connection's <code>start</code> method. When     * the connection is stopped, delivery to all the connection's message     * consumers is inhibited: synchronous receives block, and messages are not     * delivered to message listeners.     * <p/>     * <P>This call blocks until receives and/or message listeners in progress     * have completed.     * <p/>     * <P>Stopping a connection has no effect on its ability to send messages. A     * call to <code>stop</code> on a connection that has already been stopped     * is ignored.     *     * @throws JMSException if the JMS provider fails to stop message delivery     *                      due to some internal error.     */    public synchronized void stop() throws JMSException {        ensureOpen();        setModified();        if (!_stopped) {            // stop the associated sessions            synchronized (_sessions) {                Iterator iterator = _sessions.iterator();                while (iterator.hasNext()) {                    JmsSession session = (JmsSession) iterator.next();                    session.stop();                }            }            // set the state of the connection to stopped            _stopped = true;        }    }    /**     * Closes the connection.     * <P>When this method is invoked, it should not return until message     * processing has been shut down in an orderly fashion. This means that all     * message listeners that may have been running have returned, and that all     * pending receives have returned. A close terminates all pending message     * receives on the connection's sessions' consumers. The receives may return     * with a message or with null, depending on whether there was a message     * available at the time of the close. If one or more of the connection's     * sessions' message listeners is processing a message at the time when     * connection <code>close</code> is invoked, all the facilities of the     * connection and its sessions must remain available to those listeners     * until they return control to the JMS provider.     * <p/>     * <P>Closing a connection causes any of its sessions' transactions in     * progress to be rolled back. In the case where a session's work is     * coordinated by an external transaction manager, a session's     * <code>commit</code> and <code>rollback</code> methods are not used and     * the result of a closed session's work is determined later by the     * transaction manager.     * <p/>     * Closing a connection does NOT force an acknowledgment of     * client-acknowledged sessions.     * <p/>     * <P>Invoking the <code>acknowledge</code> method of a received message     * from a closed connection's session must throw an     * <code>IllegalStateException</code>.     * Closing a closed connection must NOT throw an exception.     *     * @throws JMSException if the JMS provider fails to close the connection     *                      due to some internal error.     */    public synchronized void close() throws JMSException {        if (!_closed) {            // before we close we should stop the connection and any            // associated sessions            stop();            // close the sessions            JmsSession[] sessions = null;            synchronized (_sessions) {                sessions = (JmsSession[]) _sessions.toArray(new JmsSession[0]);            }            for (int i = 0; i < sessions.length; ++i) {                sessions[i].close();                // the session deregisters itself with the connection via                // removeSession()            }            // notify the server, and null the proxy            getServerConnection().close();            _connection = null;            // remove this 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;        }    }    /**     * Creates a <code>Session</code> object.     *     * @param transacted      indicates whether the session is transacted     * @param acknowledgeMode indicates whether the consumer or the client will     *                        acknowledge any messages it receives; ignored if     *                        the session is transacted. Legal values are     *                        <code>Session.AUTO_ACKNOWLEDGE</code>,     *                        <code>Session.CLIENT_ACKNOWLEDGE</code>, and     *                        <code>Session.DUPS_OK_ACKNOWLEDGE</code>.     * @return a newly created session     * @throws JMSException if the <code>Connection</code> object fails to     *                      create a session due to some internal error or lack     *                      of support for the specific transaction and     *                      acknowledgement mode.     * @see Session#AUTO_ACKNOWLEDGE     * @see Session#CLIENT_ACKNOWLEDGE     * @see Session#DUPS_OK_ACKNOWLEDGE     */    public Session createSession(boolean transacted, int acknowledgeMode)            throws JMSException {        ensureOpen();        setModified();        JmsSession session = new JmsSession(this, transacted, acknowledgeMode);        // if the connection is started then also start the session        if (!isStopped()) {            session.start();        }        // add it to the list of managed sessions for this connection        addSession(session);        return session;    }    /**     * Creates a connection consumer for this connection (optional operation).     * This is an expert facility not used by regular JMS clients.     *     * @param destination     the destination to access     * @param messageSelector only messages with properties matching the message     *                        selector expression are delivered.  A value of     *                        null or an empty string indicates that there is no     *                        message selector for the message consumer.     * @param sessionPool     the server session pool to associate with this     *                        connection consumer     * @param maxMessages     the maximum number of messages that can be     *                        assigned to a server session at one time     * @return the connection consumer     * @throws JMSException    if the <code>Connection</code> object fails to     *                         create a connection consumer due to some internal     *                         error or invalid arguments for     *                         <code>sessionPool</code> and     *                         <code>messageSelector</code>.     * @throws InvalidDestinationException if an invalid destination is     *                                     specified.     * @throws InvalidSelectorException    if the message selector is invalid.     */    public ConnectionConsumer createConnectionConsumer(            Destination destination, String messageSelector,            ServerSessionPool sessionPool, int maxMessages)            throws JMSException {        ensureOpen();        setModified();        return new JmsConnectionConsumer(this, destination, sessionPool,                                         messageSelector, maxMessages);    }    /**     * Create a durable connection consumer for this connection.     *     * @param topic            topic to access     * @param subscriptionName durable subscription name     * @param messageSelector  only messages with properties matching the     *                         message selector expression are delivered.  A     *                         value of null or an empty string indicates that     *                         there is no message selector for the message     *                         consumer.     * @param sessionPool      the server session pool to associate with this     *                         durable connection consumer     * @param maxMessages      the maximum number of messages that can be     *                         assigned to a server session at one time     * @return the durable connection consumer     * @throws JMSException    if the <code>Connection</code> object fails to     *                         create a connection consumer due to some internal     *                         error or invalid arguments for     *                         <code>sessionPool</code> and     *                         <code>messageSelector</code>.     * @throws InvalidDestinationException if an invalid destination is     *                                     specified.     * @throws InvalidSelectorException    if the message selector is invalid.     */    public ConnectionConsumer createDurableConnectionConsumer(            Topic topic, String subscriptionName, String messageSelector,            ServerSessionPool sessionPool, int maxMessages)            throws JMSException {        ensureOpen();        setModified();        return new JmsConnectionConsumer(this, topic, subscriptionName,                                         sessionPool, messageSelector,                                         maxMessages);    }    /**     * Returns the server connection.     *     * @return the server connection     * @throws JMSException if the connection is <code>null</code>     */    protected ServerConnection getServerConnection() throws JMSException {        if (_connection == null) {            throw new JMSException("Connection closed");        }        return _connection;    }    /**     * Add the specified session to the list of managed sessions.     *     * @param session session to register     */    protected void addSession(JmsSession session) {        synchronized (_sessions) {            _sessions.add(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) {        synchronized (_sessions) {            _sessions.remove(session);        }    }    /**     * Returns 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     */    protected 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");        }    }}

⌨️ 快捷键说明

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