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

📄 httpjmssessionsender.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-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: HttpJmsSessionSender.java,v 1.11 2003/08/30 08:00:54 tanderson Exp $
 *
 * Date         Author  Changes
 * $Date	    jimm    Created
 */


package org.exolab.jms.server.http;

import java.util.Vector;

import javax.jms.Message;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.exolab.core.http.HttpClient;
import org.exolab.jms.client.JmsMessageListener;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.server.ClientDisconnectionException;
import org.exolab.jms.server.JmsServerSession;


/**
 * This class contains the http connection to a receiver or subscriber
 * for passing messages.
 *
 * @version     $Revision: 1.11 $ $Date: 2003/08/30 08:00:54 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 * @see         org.exolab.jms.server.JmsServerSession
 * @see         org.exolab.core.http.HttpClient
 */
public class HttpJmsSessionSender implements JmsMessageListener {

    /**
     * The session connection
     */
    private HttpJmsSessionConnection _connection;

    /**
     * The connection to the client servlet
     */
    private HttpClient _client;

    /**
     * The session instance
     */
    private JmsServerSession _session;

    /**
     * The host the client session runs on
     */
    private String _host;

    /**
     * The port the client session is listening on
     */
    private String _port;

    /**
     * Pinger used to ping subscribers and detect disconnections
     */
    private HttpJmsSessionPinger _pinger;

    /**
     * The logger
     */
    private static final Log _log = LogFactory.getLog(
        HttpJmsSessionSender.class);


    /**
     * Create a connection to the client listener for sending JmsMessages.
     *
     * @param connection the session connection
     * @param client the connection to the client.
     * @param session the session this connection belongs to
     * @param host the host the client session runs on
     * @param port the port the client session is listening on
     */
    public HttpJmsSessionSender(HttpJmsSessionConnection connection,
                                HttpClient client, JmsServerSession session,
                                String host, String port) {
        _connection = connection;
        _client = client;
        _session = session;
        _host = host;
        _port = port;
        session.setMessageListener(this);
        _pinger = new HttpJmsSessionPinger(this);
        new Thread(_pinger).start();
    }

    /**
     * Send a message to a listener.
     *
     * @param message The message to send.
     */
    public void onMessage(Message message) {
        try {
            if (_client != null) {
                Vector v = new Vector(2);
                v.add(_session.getSessionId());
                v.add((MessageImpl) message);
                _pinger.reset();
                synchronized (_client) {
                    _client.send(v, _host, _port);
                    Vector reply = (Vector) _client.receive();
                }
            }
        } catch (Exception err) {
            _log.debug("Failed to send message to client", err);
            throw new ClientDisconnectionException(err.getMessage());
        }
    }

    /**
     * Send the collection of messages to the client. This is used for async
     * message delivery.
     *
     * @param messages - collection of MessageImpl objects
     */
    public void onMessages(Vector messages) {
        try {
            if (_client != null) {
                Vector v = new Vector(2);
                v.add(_session.getSessionId());
                v.add(messages);
                _pinger.reset();
                synchronized (_client) {
                    _client.send(v, _host, _port);
                    Vector reply = (Vector) _client.receive();
                }
            }
        } catch (Exception err) {
            _log.debug("Failed to send messages to client", err);
            throw new ClientDisconnectionException(err.getMessage());
        }
    }

    /**
     * Notify the specified client that a message avaiable.
     *
     * @param clinet - the client identity
     */
    public void onMessageAvailable(long clientId) {
        try {
            if (_client != null) {
                Vector v = new Vector(2);
                v.add(_session.getSessionId());
                v.add(new Long(clientId));
                _pinger.reset();
                _client.send(v, _host, _port);
                Vector reply = (Vector) _client.receive();
            }
        } catch (Exception err) {
            _log.debug("Failed to notify client", err);
            throw new ClientDisconnectionException(err.getMessage());
        }
    }

    /**
     * Ping the client to see if its still active.
     */
    protected void ping() {
        try {
            if (_client != null) {
                Vector v = new Vector(1);
                v.add(_session.getSessionId());
                synchronized (_client) {
                    _client.send(v, _host, _port);
                    Vector reply = (Vector) _client.receive();
                }
            }
        } catch (Exception err) {
            _log.debug("Failed to ping client", err);
            throw new ClientDisconnectionException(err.getMessage());
        }
    }

    /**
     * Remove the callback from the JmsServerSession.
     */
    public void close() {
        HttpClient client = _client;
        if (client != null) {
            synchronized (client) {
                try {
                    client.send("close", _host, _port);
                } catch (Exception ignore) {
                }
            }
        }
        
        JmsServerSession session = _session;
        _session = null;
        if (session != null) {            
            session.setMessageListener(null);
            _connection.disconnect(session);
            _pinger.close();
            _pinger = null;
        }
        _host = null;
        _port = null;
        _client = null;
    }

} //-- HttpJmsSessionSender

⌨️ 快捷键说明

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