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

📄 jmsserverconnection.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: JmsServerConnection.java,v 1.16 2003/08/17 01:32:26 tanderson Exp $
 *
 * Date         Author  Changes
 * 04/07/2000   jima    Created
 */
package org.exolab.jms.server;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.jms.JMSException;

import org.exolab.jms.util.UUID;


/**
 * A connection is created for every client connecting to the JmsServer. A
 * connection supports multiple sessions.
 *
 * @version     $Revision: 1.16 $ $Date: 2003/08/17 01:32:26 $
 * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 * @see         JmsServerConnectionManager
 */
public class JmsServerConnection {

    /**
     * The maximum number of consecutive failed attempts to ping the client
     * before considering that the connection is inactive
     */
    private static final int MAX_PING_ATTEMPTS = 3;

    /**
     * manage all sessions allocated through this connection.
     */
    private Hashtable _sessions = null;

    /**
     * This is the client that is responsible for this connection. It should
     * map back to a client-side entity
     */
    private String _clientId = null;

    /**
     * The connection id is unique within the context of a JMS server
     */
    private String _connectionId = null;

    /**
     * Indicates if message delivery has been stopped for this connection
     */
    private boolean _stopped = true;

    /**
     * Store the number of consecutive failed attempts to ping the client
     * side of the connection
     */
    private int _consecutiveFailedPingAttempts = 0;


    /**
     * Instantiate an instance of this class for the specified client identity.
     * The JmsConnection object can only be instantiated by classes in the same
     * package (i.e ConnectionManager). The ConnectionManager is responsible
     * for allocating the client identity which is passed to the constructor
     *
     * @param       id              client identity
     */
    JmsServerConnection(String id) {
        _clientId = id;
        _sessions = new Hashtable(20);
        _connectionId = UUID.next();
        _stopped = true;
    }

    /**
     * Returns the client identifier
     *
     * @return the client identifier
     */
    public String getID() {
        return _clientId;
    }

    /**
     * Create a sesion for the specified connection. Assign the clientId to
     * the sessions. The created session is initially in the stopped mode.
     * <p>
     * The created session represents an endpoint with the JmsServer where
     * you can create consumers. producers and destinations.
     *
     * @param       ackMode         the ackmode for the session
     * @param       transacted      true if the session is transacted
     * @return      JmsSession      created session
     */
    public JmsServerSession createSession(int ackMode, boolean transacted) {
        JmsServerSession session = new JmsServerSession(this, _clientId,
            ackMode, transacted);
        session.setSessionId(UUID.next());
        _sessions.put(session.getSessionId(), session);
        if (!_stopped) {
            session.start();
        }

        return session;
    }

    /**
     * Delete the specified session. If the session does not exist then
     * fail silently.
     *
     * @param       session         session to delete
     */
    public void deleteSession(JmsServerSession session) {
        _sessions.remove(session.getSessionId());
    }

    /**
     * Return the number of sessions currently allocated to this connection.
     *
     * @return      int
     */
    public int getSessionCount() {
        return _sessions.size();
    }

    /**
     * Return an enumeration of all the sessions associated with this
     * connection
     *
     * @return      Enumeration
     */
    public Enumeration getSessions() {
        return _sessions.elements();
    }

    /**
     * Start all the sessions allocated to this connection.
     */
    public void start() {
        if (_stopped) {
            Enumeration sessions = _sessions.elements();

            while (sessions.hasMoreElements()) {
                JmsServerSession session = (JmsServerSession)
                    sessions.nextElement();
                session.start();
            }
            _stopped = false;
        }
    }

    /**
     * Stop all the sessions allocated to this collection. Do not reemove
     * the session
     */
    public void stop() {
        if (!_stopped) {
            Enumeration sessions = _sessions.elements();

            while (sessions.hasMoreElements()) {
                JmsServerSession session = (JmsServerSession)
                    sessions.nextElement();
                session.stop();
            }
            _stopped = true;
        }
    }

    /**
     * Close all the sessions allocated to this collection. This will also
     * remove the session from the allocated list of sessions
     */
    public void close() {
        Enumeration sessions = _sessions.elements();
        try {
            while (sessions.hasMoreElements()) {
                JmsServerSession session = (JmsServerSession) sessions.nextElement();
                session.close();
            }
        } catch (JMSException exception) {
            // log the message
        }

        // remove all the sessions
        _sessions.clear();
    }

    /**
     * Retrive the JmsServerSession with the given sessionId. Return null
     * if the session does not exist.
     *
     * @param sessionId The id of the required session.
     * @return JmsServerSession The requested session if it exists.
     *
     */
    public JmsServerSession getSession(String sessionId) {
        return (JmsServerSession) _sessions.get(sessionId);
    }

    /**
     * Retrieve the identity of this conection. If this method does not
     * complete successfully then throw JMSException.
     *
     * @return      String
     */
    public String getConnectionId() {
        return _connectionId;
    }

    /**
     * Determine whether the endpoint is active by trying to ping the first
     * session in the list. If there are no active sessions then assume that
     * the connection is active...(need to probably fix this up).
     *
     * @return boolean true if active; false otherwise
     */
    boolean isClientEndpointActive() {
        boolean active = true;

        JmsServerSession[] sessions;
        synchronized (_sessions) {
            // get a copy of the list of sessions to avoid
            // ConcurrentModificationExceptions
            sessions = (JmsServerSession[]) _sessions.values().toArray(
                new JmsServerSession[0]);
        }
        for (int i = 0; i < sessions.length; ++i) {
            JmsServerSession session = sessions[i];
            if (session.isClientEndpointActive()) {
                // if there is one active session then mark the connection
                // as active
                active = true;
                _consecutiveFailedPingAttempts = 0;
                break;
            } else {
                if (++_consecutiveFailedPingAttempts > MAX_PING_ATTEMPTS) {
                    active = false;
                } else {
                    active = true;
                }
            }
        }

        return active;
    }
}

⌨️ 快捷键说明

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