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

📄 transientmessagehandle.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 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: TransientMessageHandle.java,v 1.14 2003/08/17 01:32:25 tanderson Exp $
 *
 * Date         Author  Changes
 * 3/1/2001     jima    Created
 */
package org.exolab.jms.messagemgr;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;

import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.message.MessageHandle;
import org.exolab.jms.message.MessageId;
import org.exolab.jms.message.MessageImpl;


/**
 * A message handle is used to indirectly reference a message in
 * the message manager.
 *
 * @version     $Revision: 1.14 $ $Date: 2003/08/17 01:32:25 $
 * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 */
public class TransientMessageHandle
    implements MessageHandle {

    /**
     * Used for serialization
     */
    static final long serialVersionUID = 2;

    /**
     * Stores the message identity of the message. This can always be
     * resolved through the {@link MessageManager}
     */
    private MessageId _id = null;

    /**
     * This attribute indicates that the message associated with this
     * handle has already been delivered, but not acked.
     */
    private boolean _delivered = false;

    /**
     * Holds the priority of the corresponding message
     */
    private int _priority = Message.DEFAULT_PRIORITY;

    /**
     * This is the time that the corresponding message was accepted by the
     * server. It is expressed as a long
     */
    private long _acceptedTime;

    /**
     * a transient attribute, to associate this handle with an endpoint
     */
    private transient long _clientId = -1;

    /**
     * This is the sequence number, which is assigned to it once the message
     * has been tagged by the message manager. It also allows us to overcome
     * the millisecond resolution problem of _acceptedTime
     */
    private long _sequenceNumber;

    /**
     * This is the time that associated message expires
     */
    private long _expiryTime;

    /**
     * The destination that this handle belongs too
     */
    private JmsDestination _destination;

    /**
     * If the consumer name is set then it implies that the endpoint with the
     * specified id has exclusive access to this handle. This is primarily
     * used for topics
     */
    private String _consumerName;

    /**
     * Indicates if the associated message is persistent or not
     * NOTE: this is not serialized.  @todo - clean up semantics for
     * persistent and transient message handles. A persistent message
     * handle should be solely for those messages which can be evicted,
     * and recovered
     */
    private boolean _persistent = false;


    /**
     * Default constructor required to support Serialization
     */
    public TransientMessageHandle() {
    }

    /**
     * Create a handle for a particular message. Handles are transient
     * objects.
     *
     * @param message the message for which handle is created
     * @throws JMSException if the handle  cannot be constructed
     */
    TransientMessageHandle(MessageImpl message) throws JMSException {
        _id = message.getMessageId();
        _priority = message.getJMSPriority();
        _acceptedTime = message.getAcceptedTime();
        _delivered = message.getJMSRedelivered();
        _sequenceNumber = message.getSequenceNumber();
        _expiryTime = message.getJMSExpiration();
        _destination = (JmsDestination) message.getJMSDestination();
        _clientId = message.getClientId();
        if (message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT) {
            _persistent = true;
        }
    }

    /**
     * Return the message id
     *
     * @return MessgeId
     */
    public MessageId getMessageId() {
        return _id;
    }

    /**
     * Set the message identity
     *
     * @param id - the message identity
     */
    public void setMessageId(MessageId id) {
        _id = id;
    }

    /**
     * Set the message, corresponding to this handle, has already been
     * delivered once before
     */
    public void setDelivered() {
        _delivered = true;
    }

    /**
     * Set the state of the delivered flag to the specified value
     *
     * @param boolean value
     */
    public void setDelivered(boolean value) {
        _delivered = value;
    }

    /**
     * Check whether an attempt has already been made to deliver the message
     * before.
     *
     * @return boolean - true implies redelivery attempted
     */
    public boolean getDelivered() {
        return _delivered;
    }

    /**
     * Set the message priority
     *
     * @param int - the message priority
     */
    public void setPriority(int priority) {
        _priority = priority;
    }

    /**
     * Return the priority of the underlying message
     *
     * @return int - message priority
     */
    public int getPriority() {
        return _priority;
    }

    /**
     * Set the time that the message was accepted by the server
     *
     * @param time - accepted time
     */
    public void setAcceptedTime(long time) {
        _acceptedTime = time;
    }

    /**
     * Return time that the corresponding message was accepted.
     *
     * @return long - the accepted time.
     */
    public long getAcceptedTime() {
        return _acceptedTime;
    }

    /**
     * Set the message expiry time
     *
     * @long time - time in ms since epoch
     */
    public void setExpiryTime(long time) {
        _expiryTime = time;
    }

    /**
     * Return the message expiry time
     *
     * @return long - time in ms since epoch
     */
    public long getExpiryTime() {
        return _expiryTime;
    }

    /**
     * Set the message's sequence number
     *
     * @param seq - sequence time
     */
    public void setSequenceNumber(long seq) {
        _sequenceNumber = seq;
    }

    /**
     * Return message's sequence number
     *
     * @return long - the sequence number
     */
    public long getSequenceNumber() {
        return _sequenceNumber;
    }

    /**
     * Set the destination that owns this handle
     *
     * @param destination - the destination
     */
    public void setDestination(JmsDestination destination) {
        _destination = destination;
    }

    /**
     * Return the destination for this handle
     *
     * @return JmsDestination
     */
    public JmsDestination getDestination() {
        return _destination;
    }

    /**
     * Set the client id, that owns this handle
     *
     * @param clientId - client identity
     */
    public void setClientId(long clientId) {
        _clientId = clientId;
    }

    /**
     * Retrieve the client identity associated with this handle
     *
     * @return long
     */
    public long getClientId() {
        return _clientId;
    }

    // implementation of MessageHandle.setConsumerName
    public void setConsumerName(String name) {
        _consumerName = name;
    }

    // implementation of MessageHandle.getConsumerName
    public String getConsumerName() {
        return _consumerName;
    }


    /**
     * Return the associated message or null if it is invalid
     *
     * @return  MessageImpl
     */
    public MessageImpl getMessage() {
        return (MessageImpl) MessageMgr.instance().getMessage(this);
    }

    /**
     * Return a stringified version of the handle
     *
     * @return String
     */
    public String toString() {
        return "Handle : " + _priority + ":" + getAcceptedTime() +
            ":" + getSequenceNumber() + ":" + _id;
    }

    /**
     * Clear the message handle
     */
    public void clear() {
        _id = null;
    }

    // implementation of MessageHandle.isPersistent
    public boolean isPersistent() {
        return _persistent;
    }

    /**
     * Destroy this handle
     */
    public void destroy() {
        // notify the message manager
        MessageMgr.instance().handleDestroyed(this);
    }

    // override the definition of equals
    public boolean equals(Object object) {

        if ((object != null) &&
            (object instanceof TransientMessageHandle)) {
            return _id.equals(((TransientMessageHandle) object)._id);
        }

        return false;
    }

    // override the definition of hashCode
    public int hashCode() {
        return _id.hashCode();
    }

    // implementation of Externalizable.writeExternal
    public void writeExternal(ObjectOutput stream)
        throws IOException {
        stream.writeLong(serialVersionUID);
        stream.writeObject(_id);
        stream.writeInt(_priority);
        stream.writeBoolean(_delivered);
        stream.writeLong(_acceptedTime);
        stream.writeLong(_sequenceNumber);
        stream.writeLong(_expiryTime);
        stream.writeObject(_destination);
    }

    // implementation of Externalizable.writeExternal
    public void readExternal(ObjectInput stream)
        throws IOException, ClassNotFoundException {
        long version = stream.readLong();
        if (version == serialVersionUID) {
            _id = (MessageId) stream.readObject();
            _priority = stream.readInt();
            _delivered = stream.readBoolean();
            _acceptedTime = stream.readLong();
            _sequenceNumber = stream.readLong();
            _expiryTime = stream.readLong();
            _destination = (JmsDestination) stream.readObject();
        } else if (version == 1) {
            _id = (MessageId) stream.readObject();
            _priority = stream.readInt();
            _delivered = stream.readBoolean();
            _acceptedTime = stream.readLong();
            _sequenceNumber = stream.readLong();
            _expiryTime = stream.readLong();
            _destination = null;
        } else {
            throw new IOException("TransientMessageHandle with version " +
                version + " is not supported.");
        }
    }

} //-- TransientMessageHandle

⌨️ 快捷键说明

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