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

📄 intravmjmssessionstub.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: IntravmJmsSessionStub.java,v 1.25 2003/08/07 13:32:52 tanderson Exp $
 *
 * Date         Author  Changes
 * 04/18/2000    jima    Created
 */
package org.exolab.jms.client.intravm;

import java.util.Iterator;
import java.util.Vector;

import javax.jms.JMSException;
import javax.jms.Message;
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.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;
import org.exolab.jms.server.JmsServerSession;


/**
 * The client stub implementation for the intra-JVM JMS server.
 * <p>
 * This delegates directly to a {@link org.exolab.jms.server.JmsServerSession
 * JmsServerSession} instance.
 *
 * @version     $Revision: 1.25 $ $Date: 2003/08/07 13:32:52 $
 * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 */
public class IntravmJmsSessionStub
    implements JmsSessionStubIfc, JmsMessageListener {

    /**
     * This is the message listener for JMS messages. The MessageListener is
     * then responsible for delivering it to the actual consumer. The listener
     * must be set by the client session after construction using
     * {@link #setMessageListener}
     */
    private JmsMessageListener _listener = null;

    /**
     * This is a remote reference to the session which is initialised at
     * construction.
     */
    private JmsServerSession _delegate = null;

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


    /**
     * Create an instance of this class using the specified session stub.
     * A {@link JmsMessageListener} must be registered after construction
     * using {@link #setMessageListener} in order for client session to
     * receive messages.
     *
     * @param       session             the server session to delegate to
     * @throws      JMSException
     */
    IntravmJmsSessionStub(JmsServerSession session) throws JMSException {
        if (session != null) {
            _delegate = session;
        } else {
            throw new JMSException(
                "Cannot create session stub with a null session");
        }
    }

    // implementation of JmsSessionStubIfc.getClientId
    public String getClientId() throws JMSException {
        return _delegate.getClientId();
    }

    // implementation of JmsSessionStubIfc.getSessionId
    public String getSessionId() throws JMSException {
        return _delegate.getSessionId();
    }

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

    // implementation of JmsSessionStubIfc.close
    public void close() throws JMSException {
        _delegate.close();
    }

    // implementation of JmsSessionStubIfc.acknowledgeMessage
    public void acknowledgeMessage(long clientId, String messageId)
        throws JMSException {
        // Send back the ack to the consumer endpoint that forwarded the
        // message.
        _delegate.acknowledgeMessage(clientId, messageId);
    }

    // implementation of JmsSessionStubIfc.sendMessage
    public void sendMessage(Message message) throws JMSException {
        // As the server resides in the same VM as the client, the message
        // must be copied to allow the client to modify the original after
        // it is sent. The limitation of this is that for transacted sessions,
        // the message will ultimately be copied twice.
        Message copy = null;
        try {
            copy = (Message) ((MessageImpl) message).clone();
        } catch (CloneNotSupportedException exception) {
            JMSException error = new JMSException(exception.getMessage());
            error.setLinkedException(exception);
            throw error;
        }
        _delegate.sendMessage(copy);
    }

    // implementation of JmsSessionStubIfc.sendMessages
    public void sendMessages(Vector messages) throws JMSException {
        // messages are already cloned
        _delegate.sendMessages(messages);
    }

    // implementation of JmsSessionStubIfc.receiveMessage
    public Message receiveMessage(long clientId, long wait)
        throws JMSException {
        // As the server resides in the same VM as the client, any returned
        // message must be copied so that if the client modifies it, the
        // server instance is not affected.
        Message message = _delegate.receiveMessage(clientId, wait);
        Message copy = null;
        if (message != null) {
            try {
                copy = (Message) ((MessageImpl) message).clone();
            } catch (CloneNotSupportedException exception) {
                JMSException error = new JMSException(exception.getMessage());
                error.setLinkedException(exception);
                throw error;
            }
        }
        return copy;
    }

    // implementation of JmsSessionStubIfc.receiveMessages
    public Vector receiveMessages(long clientId, int count)
        throws JMSException {
        // As the server resides in the same VM as the client, any returned
        // messages must be copied so that if the client modifies it, the
        // server instance is not affected.
        Vector messages = _delegate.receiveMessages(clientId, count);
        Vector copied = new Vector();
        if (messages.size() > 0) {
            for (int index = 0; index < messages.size(); index++) {
                Message copy = null;
                Message message = (MessageImpl) messages.elementAt(index);
                try {
                    copy = (MessageImpl) ((MessageImpl) message).clone();
                    copied.addElement(copy);
                } catch (CloneNotSupportedException exception) {
                    JMSException error = new JMSException(exception.getMessage());
                    error.setLinkedException(exception);
                    throw error;
                }
            }
        }

        return copied;
    }

    // implementation of JmsSessionStubIfc.createQueue
    public void createQueue(JmsQueue queue) throws JMSException {
        _delegate.createQueue(queue);
    }

    // implementation of JmsSessionStubIfc.createTopic
    public void createTopic(JmsTopic topic) throws JMSException {
        _delegate.createTopic(topic);
    }

    // implementation of JmsSessionStubIfc.createReceiver
    public void createReceiver(JmsQueue queue, long clientId, String selector)
        throws JMSException {
        _delegate.createReceiver(queue, clientId, selector);
    }

    // implementation of JmsSessionStubIfc.createSender
    public void createSender(JmsQueue queue) throws JMSException {
        _delegate.createSender(queue);
    }

    // implementation of JmsSessionStubIfc.createBrowser
    public void createBrowser(JmsQueue queue, long clientId, String selector)
        throws JMSException {
        _delegate.createBrowser(queue, clientId, selector);
    }

    // implementation of JmsSessionStubIfc.deleteReceiver
    public void deleteReceiver(long clientId) throws JMSException {
        _delegate.deleteReceiver(clientId);
    }

    // implementation of JmsSessionStubIfc.deleteBrowser
    public void deleteBrowser(long clientId) throws JMSException {
        _delegate.deleteBrowser(clientId);
    }

    // implementation of JmsSessionStubIfc.createSubscriber
    public void createSubscriber(JmsTopic topic, String name, long clientId,
                                 String selector, boolean noLocal)
        throws JMSException {
        _delegate.createSubscriber(topic, name, clientId, selector, noLocal);
    }

    // implementation of JmsSessionStubIfc.createPublisher
    public void createPublisher(JmsTopic topic) throws JMSException {
        _delegate.createPublisher(topic);
    }

    // implementation of JmsSessionStubIfc.deleteSubscriber
    public void deleteSubscriber(long clientId) throws JMSException {
        _delegate.deleteSubscriber(clientId);
    }

    // implementation of JmsSessionStubIfc.unsubscribe
    public void unsubscribe(String name) throws JMSException {
        _delegate.unsubscribe(name);
    }

    // implementation of JmsSessionStubIfc.stopMessageDelivery
    public void stopMessageDelivery() throws JMSException {
        _delegate.stopMessageDelivery();
    }

    // implementation of JmsSessionStubIfc.startMessageDelivery
    public void startMessageDelivery() throws JMSException {
        _delegate.startMessageDelivery();
    }

    // implementation of JmsSessionStubIfc.setMessageListener
    public void setMessageListener(JmsMessageListener listener) {
        _listener = listener;
    }

    // implementation of JmsSessionStubIfc.enableAsynchronousDelivery
    public void enableAsynchronousDelivery(long clientId, String id,
                                           boolean enable)
        throws JMSException {
        _delegate.enableAsynchronousDelivery(clientId, id, enable);
    }

    // implementation of JmsSessionStubIfc.recover
    public void recover() throws JMSException {
        _delegate.recover();
    }

    // implementation of JmsSessionStubIfc.commit
    public void commit() throws JMSException {
        _delegate.commit();
    }

    // implementation of JmsSessionStubIfc.rollback
    public void rollback() throws JMSException {
        _delegate.rollback();
    }

    // implementation of JmsSessionStubIfc.commit
    public void commit(Xid xid, boolean onePhase)
        throws XAException {
        _delegate.commit(xid, onePhase);
    }

    // implementation of JmsSessionStubIfc.end
    public void end(Xid xid, int flags)
        throws XAException {
        _delegate.end(xid, flags);
    }

    // implementation of JmsSessionStubIfc.forget
    public void forget(Xid xid)
        throws XAException {
        _delegate.forget(xid);
    }

    // implementation of JmsSessionStubIfc.getResourceManagerId
    public String getResourceManagerId() throws XAException {
        return _delegate.getResourceManagerId();
    }

    // implementation of JmsSessionStubIfc.getTransactionTimeout
    public int getTransactionTimeout()
        throws XAException {
        return _delegate.getTransactionTimeout();
    }

    // implementation of JmsSessionStubIfc.prepare
    public int prepare(Xid xid)
        throws XAException {
        return _delegate.prepare(xid);
    }

    // implementation of JmsSessionStubIfc.recover
    public Xid[] recover(int flag)
        throws XAException {
        return _delegate.recover(flag);
    }

    // implementation of JmsSessionStubIfc.rollback
    public void rollback(Xid xid)
        throws XAException {
        _delegate.rollback(xid);
    }

    // implementation of JmsSessionStubIfc.setTransactionTimeout
    public boolean setTransactionTimeout(int seconds)
        throws XAException {
        return _delegate.setTransactionTimeout(seconds);
    }

    // implementation of JmsSessionStubIfc.start
    public void start(Xid xid, int flags)
        throws XAException {
        _delegate.start(xid, flags);
    }

    // implementation of JmsMessageListener.onMessage
    public void onMessage(Message message) {
        // As the server resides in the same VM as the client, the message
        // must be copied to allow the client to modify it after it is
        // received.
        if (message != null) {
            // ignore null messages generated by
            // JmsServerSession.isClientEndpointActive()
            Message copy = null;
            try {
                copy = (Message) ((MessageImpl) message).clone();
            } catch (CloneNotSupportedException exception) {
                _log.error(exception);
            }
            _listener.onMessage(copy);
        }
    }

    // implementation of JmsMessageListener.onMessages
    public void onMessages(Vector messages) {
        // As the server resides in the same VM as the client, the messages
        // must be copied to allow the client to modify them after they are
        // received.
        Vector copy = new Vector();
        try {
            Iterator iter = messages.iterator();
            while (iter.hasNext()) {
                MessageImpl message = (MessageImpl) iter.next();
                copy.add(message.clone());
            }
        } catch (CloneNotSupportedException exception) {
            _log.error(exception);
        }
        _listener.onMessages(copy);
    }

    // implementation of JmsMessageListener.onMessageAvailable
    public void onMessageAvailable(long clientId) {
        _listener.onMessageAvailable(clientId);
    }

} //-- IntravmJmsSessionStub

⌨️ 快捷键说明

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