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

📄 httpjmssessionstub.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * 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 2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: HttpJmsSessionStub.java,v 1.16 2003/08/25 03:35:45 tanderson Exp $
 *
 * Date             Author      Changes
 * 12 Oct 2001      mourikis    Created
 */
package org.exolab.jms.client.http;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

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

import org.exolab.core.http.HttpClient;
import org.exolab.core.ipc.IpcIfc;
import org.exolab.core.ipc.NotifierIfc;
import org.exolab.core.ipc.Server;
import org.exolab.jms.client.JmsMessageListener;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsSessionStubIfc;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.jndi.JndiConstants;
import org.exolab.jms.message.MessageImpl;


/**
 * The client side stub implementing the JmsServerSession. All session
 * requests are passed on to the server.
 *
 * @version     $Revision: 1.16 $ $Date: 2003/08/25 03:35:45 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 * @see         org.exolab.core.http.HttpClient
 */
public class HttpJmsSessionStub implements JmsSessionStubIfc, NotifierIfc {

    /**
     * A reference to the client connection
     */
    private IpcIfc _connection = null;

    /**
     * The client connection id
     */
    private String _clientId;

    /**
     * The destination connection id
     */
    private String _connectionId;

    /**
     * The session id
     */
    private String _sessionId;

    /**
     * The tcp connection for receiving messages
     */
    private Server _msgReceiver = null;

    /**
     *  The listener
     */
    private JmsMessageListener _listener = null;

    /**
     * The url of the client's servlet. This gets passed to the server to
     * allow the server to connect back via client's servlet.
     * This host and port of the URL default to that of the server's web
     * This can be overriden by specifying the system property
     * <em>JndiConstants.HTTP_CLIENT_URL_PROPERTY</em>
     */
    private String _url;

    /**
     * The host of the machine running the client. Defaults to the local
     * host address. This can be overriden by specifying the system property
     * <em>JndiConstants.HTTP_CLIENT_SERVER_ADDRESS_PROPERTY</em>
     * which is useful if the client machine has multiple NICS.
     */
    private String _host = "localhost";

    /**
     * The client servlet
     */
    private static final String CLIENT_SERVLET = "openjms/OpenJMSClient";

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


    /**
     * A new session has been established with these ids.
     *
     * @param connection  The http connection to the server.
     * @param clientId This clients unique id.
     * @param connectionId This objects connection identifier.
     * @param sessionId The unique session id for this object.
     */
    public HttpJmsSessionStub(IpcIfc connection, String clientId,
                              String connectionId, String sessionId) {
        _connection = connection;
        _clientId = clientId;
        _connectionId = connectionId;
        _sessionId = sessionId;
        String str = null;

        String clientHost = System.getProperty(
            JndiConstants.HTTP_CLIENT_SERVER_ADDRESS_PROPERTY);
        if (clientHost != null) {
            _host = clientHost;
        }
        if (_host.equals("localhost")) {
            try {
                _host = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException ignore) {
            }
        }

        String clientWebServerURL = System.getProperty(
            JndiConstants.HTTP_CLIENT_URL_PROPERTY);

        if (clientWebServerURL == null) {
            // default to the host and port of the server's webserver
            URL url = ((HttpClient) _connection).getURL();
            clientWebServerURL = url.getProtocol() + "://" + url.getHost() +
                ":" + url.getPort() + "/";
        }

        if (!clientWebServerURL.endsWith("/")) {
            clientWebServerURL += "/";
        }
        _url = clientWebServerURL + CLIENT_SERVLET;
    }

    /**
     * Get the client Id
     *
     * @return the client id
     */
    public String getClientId() {
        return _clientId;
    }

    /**
     * Get the sessionId
     *
     * @return the id of this session
     */
    public String getSessionId() {
        return _sessionId;
    }

    // implementation of JmsSessionStubIfc.beforeClose
    public void beforeClose() throws JMSException {
    }

    /**
     * Close this session.
     *
     * @throws JMSException if the close fails
     */
    public void close() throws JMSException {
        Vector v = pack("close", 0);
        synchronized (_connection) {
            send(v, true);
            checkReply("close");
        }

        _listener = null;
        if (_msgReceiver != null) {
            if (_log.isDebugEnabled()) {
                _log.debug("Stopping receiver on port="
                           + _msgReceiver.getPort());
            }
            _msgReceiver.stop();
            _msgReceiver = null;
        }
    }

    /**
     * Acknowledge a message
     *
     * @param clientId the identity of the client
     * @param messageId the message identity to ack
     * @throws JMSException if the message can't be acknowledged
     */
    public void acknowledgeMessage(long clientId, String messageId)
        throws JMSException {

        Vector v = pack("acknowledgeMessage", 2);
        //Send back the ack to the consumer endpoint that forwarded the
        // message.
        v.add(new Long(clientId));
        v.add(messageId);
        synchronized (_connection) {
            send(v, true);
            checkReply("acknowledgeMessage");
        }
    }

    // implementation of JmsSessionStubIfc.sendMessage
    public void sendMessage(Message message) throws JMSException {
        Vector v = pack("sendMessage", 1);
        v.add(message);
        synchronized (_connection) {
            if (((MessageImpl) message).isPersistent()) {
                send(v, true);
                checkReply("sendMessage");
            } else {
                send(v, false);
            }

        }
    }

    // implementation of JmsSessionStubIfc.sendMessages
    public void sendMessages(Vector messages) throws JMSException {
        Vector v = pack("sendMessages", 1);
        v.add(messages);
        synchronized (_connection) {
            send(v, true);
            // always check for a reply
            checkReply("sendMessages");
        }
    }

    // implementation of JmsSessionStubIfc.receiveMessage
    public Message receiveMessage(long clientId, long wait)
        throws JMSException {

        Message message = null;

        Vector v = pack("receiveMessage", 2);
        v.add(new Long(clientId));
        v.add(new Long(wait));
        synchronized (_connection) {
            send(v, true);
            Vector reply = checkReply("receiveMessage");
            Boolean result = (Boolean) reply.get(0);

            // check that the call completed before
            // extracting the message
            if (result.booleanValue()) {
                message = (Message) reply.get(1);
            }
        }

        return message;
    }

    // implementation of JmsSessionStubIfc.receiveMessages
    public Vector receiveMessages(long clientId, int count)
        throws JMSException {

        Vector messages = null;

        Vector v = pack("receiveMessages", 2);
        v.add(new Long(clientId));
        v.add(new Integer(count));
        synchronized (_connection) {
            send(v, true);
            Vector reply = checkReply("receiveMessages");
            Boolean result = (Boolean) reply.get(0);

            // check that the call completed before
            // extracting the message
            if (result.booleanValue()) {
                messages = (Vector) reply.get(1);
            }
        }

⌨️ 快捷键说明

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