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

📄 ipcjmssessionstub.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 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: IpcJmsSessionStub.java,v 1.20 2003/08/25 03:30:33 tanderson Exp $
 *
 * Date         Author  Changes
 * $Date        jimm    Created
 */
package org.exolab.jms.client.mipc;

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.ipc.IpcIfc;
import org.exolab.core.mipc.ObjectChannel;
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.message.MessageImpl;


/**
 * The client side stub implementing the JmsServerSession. All session
 * requests are passed on to the server. This class also an IPC server
 * connection when a listener os subscriber is set up.
 *
 * <P>Note: There is only one receive connection per client, all
 * JmsMessages for any queue/topic that the client is interested in
 * receiving are multiplexed on this single connection. The connection
 * is not bi-directional, that is no replies are sent back using this
 * connection. It is assumed that the underlying IPC protocol will confirm
 * delivery to the client. After that it is the clients responsibility to
 * ensure correct message processing. Durable messages are acked on a
 * separate port, reliable messages, are not.
 *
 * @version     $Revision: 1.20 $ $Date: 2003/08/25 03:30:33 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 * @see         org.exolab.jms.server.mipc.IpcJmsSessionConnection
 * @see         org.exolab.core.ipc.Server
 */
public class IpcJmsSessionStub implements JmsSessionStubIfc {

    /**
     * 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 message dispatcher
     */
    private IpcJmsMessageListener _listener;

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


    /**
     * A new session has been established with these ids.
     *
     * @param connection the 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.
     * @param listener the message dispatcher
     */
    public IpcJmsSessionStub(IpcIfc connection, String clientId,
                             String connectionId, String sessionId,
                             IpcJmsMessageListener listener) {
        _connection = connection;
        _clientId = clientId;
        _connectionId = connectionId;
        _sessionId = sessionId;
        _listener = listener;
    }


    /**
     * Get the client Id
     *
     * @return         String  The client id
     * @exception      JMSException On error
     *
     */
    public String getClientId() throws JMSException {
        return _clientId;
    }


    /**
     * Get the sessionId
     *
     * @return         String  The session id of this session
     * @exception      JMSException On error
     *
     */
    public String getSessionId() throws JMSException {
        return _sessionId;
    }

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

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

        _listener.closeSession(_sessionId);
    }


    /**
     * Extract the destination and messageId for the message and send back
     * an ack.
     *
     * @param       clientId        the identity ofthe client
     * @param       messageId       the message identity to ack
     * @exception   JMSException
     */
    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);
            checkReply("acknowledgeMessage");
        }
    }


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

    // implementation of JmsSessionStubIfc.sendMessages
    public void sendMessages(Vector messages) throws JMSException {
        Vector v = pack("sendMessages", 1);
        v.add(messages);
        synchronized (_connection) {
            send(v);
            // 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);
            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);
            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);
            }
        }

        return messages;
    }

    /**
     * Create a new Queue.
     *
     * @param         queue  The queue to create.
     * @exception     JMSException On error
     *
     */
    public void createQueue(JmsQueue queue) throws JMSException {
        Vector v = pack("createQueue", 1);
        v.add(queue);
        synchronized (_connection) {
            send(v);
            checkReply("createQueue");
        }
    }


    /**
     * Create a new topic
     *
     * @param         topic  The topic to create.
     * @exception     JMSException On error
     *
     */
    public void createTopic(JmsTopic topic) throws JMSException {
        Vector v = pack("createTopic", 1);
        v.add(topic);
        synchronized (_connection) {
            send(v);
            checkReply("createTopic");
        }
    }


    /**
     * Create a receiver. Get the IP address of the machine the consumer runs
     * on, and the port it is listening too, and pass this to the server, so
     * it can make a new dedicated connection for sending all messages to
     * this client.
     *
     * @param         queue  The queue to listen to
     * @param         clientId The session allocated identifier
     * @param         selector The selector to filter messages (may be null)
     * @exception     JMSException On error
     *
     */
    public void createReceiver(JmsQueue queue, long clientId, String selector)
        throws JMSException {
        _listener.start();
        Vector v = pack("createReceiver", 5);
        v.add(queue);
        v.add(new Long(clientId));
        v.add(selector);
        v.add(((ObjectChannel) _connection).getConnection().getHost());
        v.add(String.valueOf
            (((ObjectChannel) _connection).getConnection().getPort()));
        v.add("n");
        synchronized (_connection) {
            send(v);
            checkReply("createReceiver");
        }
    }


    /**
     * Create a queue sender
     *
     * @param         queue  The queue to send messages to
     * @exception     JMSException On error
     *
     */
    public void createSender(JmsQueue queue) throws JMSException {
        Vector v = pack("createSender", 1);
        v.add(queue);
        synchronized (_connection) {
            send(v);
            checkReply("createSender");
        }
    }

    /**
     * Create a queue browser for this session. This allows clients to browse
     * a queue without removing any messages.
     * <p>
     *
     * You cannot create more than one queue browser for the same queue
     * in a single session.
     *
     * @param       queue               queue to browse
     * @param       clientId            idenity of the client
     * @param       selector            message selector. This may be null
     * @exception   JMSException
     */
    public void createBrowser(JmsQueue queue, long clientId, String selector)
        throws JMSException {
        _listener.start();
        Vector v = pack("createBrowser", 3);
        v.add(queue);
        v.add(new Long(clientId));
        v.add(selector);
        v.add(((ObjectChannel) _connection).getConnection().getHost());
        v.add(String.valueOf
            (((ObjectChannel) _connection).getConnection().getPort()));
        v.add("n");
        synchronized (_connection) {
            send(v);
            checkReply("createBrowser");
        }
    }

    /**
     * Delete the receiver for this queue.
     *
     * @param         clientId  The id of the client to delete
     * @exception     JMSException On error
     *
     */
    public void deleteReceiver(long clientId) throws JMSException {
        Vector v = pack("deleteReceiver", 1);
        v.add(new Long(clientId));
        synchronized (_connection) {
            send(v);
            checkReply("deleteReceiver");
        }
    }


    /**
     * Delete the queue browser associated with the specified queue from
     * the session.
     * If the corresponding queue does not exist or it cannot be deleted,
     * then throw a JMSException
     *
     * @param       clientId            identity of the browser
     * @exception   JMSException
     */
    public void deleteBrowser(long clientId)
        throws JMSException {
        Vector v = pack("deleteBrowser", 1);
        v.add(new Long(clientId));
        synchronized (_connection) {
            send(v);
            checkReply("deleteBrowser");
        }
    }

    /**
     * Create a new topic subscriber
     *
     * @param         topic  The topic to subscribe to
     * @param         name The subscribers name
     * @param         client The client identity
     * @param         selector The selector to filter messages (may be null)
     * @exception     JMSException On error
     *
     */
    public void createSubscriber(JmsTopic topic, String name, long clientId,
                                 String selector, boolean noLocal)
        throws JMSException {
        _listener.start();
        Vector v = pack("createSubscriber", 5);
        v.add(topic);

⌨️ 快捷键说明

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