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

📄 defaultmessagecache.java

📁 一个java方面的消息订阅发送的源码
💻 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: DefaultMessageCache.java,v 1.1 2005/03/18 03:58:39 tanderson Exp $
 *
 * Date         Author  Changes
 * 3/1/2001     jima    Created
 */
package org.exolab.jms.messagemgr;

import java.sql.Connection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.jms.JMSException;

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

import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.persistence.DatabaseService;
import org.exolab.jms.persistence.PersistenceAdapter;
import org.exolab.jms.persistence.SQLHelper;


/**
 * Default implementation of the {@link MessageCache} interface.
 *
 * @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.1 $ $Date: 2005/03/18 03:58:39 $
 */
final class DefaultMessageCache implements MessageCache {

    /**
     * Maintains the pool of transient messages.
     */
    private final Map _transient = Collections.synchronizedMap(
            new HashMap(1023));

    /**
     * Maintains the pool of persistent messages.
     */
    private final Map _persistent = Collections.synchronizedMap(
            new HashMap(1023));

    /**
     * Maintains the references to messages.
     */
    private final Map _references = Collections.synchronizedMap(
            new HashMap(1023));

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


    /**
     * Add a reference and its corresponding message to the cache.
     *
     * @param reference the reference to the message
     * @param message   the message
     */
    public void addMessage(MessageRef reference, MessageImpl message) {
        String messageId = reference.getMessageId();
        if (reference.isPersistent()) {
            _persistent.put(messageId, message);
        } else {
            _transient.put(messageId, message);
        }
        addMessageRef(messageId, reference);
    }

    /**
     * Adds a message reference to the cache.
     *
     * @param reference the message reference to add
     */
    public void addMessageRef(MessageRef reference) {
        addMessageRef(reference.getMessageId(), reference);
    }

    /**
     * Returns a message reference, given its identifier.
     *
     * @param messageId the message identifier
     * @return the message reference associated with <code>messageId</code>, or
     *         <code>null</code>  if none exists
     */
    public MessageRef getMessageRef(String messageId) {
        return (MessageRef) _references.get(messageId);
    }

    /**
     * Returns the message corresponding to the specified reference.
     *
     * @param reference the message reference
     * @return the associated message, or <code>null</code> if none exists
     * @throws JMSException for any error
     */
    public MessageImpl getMessage(MessageRef reference) throws JMSException {
        MessageImpl message = null;
        final String messageId = reference.getMessageId();

        if (reference.isPersistent()) {
            message = (MessageImpl) _persistent.get(messageId);

            // if the message is not cached then try and retrieve it from the
            // database and cache it.
            if (message == null) {
                // fault in at least the next message from the database
                PersistenceAdapter adapter = DatabaseService.getAdapter();
                Connection connection = null;
                try {
                    connection = DatabaseService.getConnection();
                    message = adapter.getMessage(connection, messageId);
                    connection.commit();
                } catch (Exception exception) {
                    SQLHelper.rollback(connection);
                    _log.error("Failed to retrieve message", exception);
                    throw new JMSException("Failed to retrieve message: "
                                           + exception.getMessage());
                } finally {
                    SQLHelper.close(connection);
                }

                // add the message to the persistent cache once it has been
                // retrieved from the datastore
                if (message != null) {
                    _persistent.put(messageId, message);
                }
            }
        } else {
            message = (MessageImpl) _transient.get(messageId);
        }

        if (message != null && !message.getReadOnly()) {
            // mark the message as read-only
            message.setReadOnly(true);
        }

        return message;
    }

    /**
     * Destroys the message corresponding to the reference.
     *
     * @throws JMSException for any error
     */
    public void destroy(MessageRef reference) throws JMSException {
        if (reference.isPersistent()) {
            Connection connection = null;
            try {
                connection = DatabaseService.getConnection();
                destroy(reference, connection);
                connection.commit();
            } catch (JMSException exception) {
                SQLHelper.rollback(connection);
                throw exception;
            } catch (Exception exception) {
                SQLHelper.rollback(connection);
                _log.error("Failed to remove message", exception);
                throw new JMSException("Failed to remove message: "
                                       + exception.getMessage());
            } finally {
                SQLHelper.close(connection);
            }
        } else {
            final String messageId = reference.getMessageId();
            _transient.remove(messageId);
            _references.remove(messageId);
        }
    }

    /**
     * Destroys the message corresponding to the reference.
     *
     * @param connection the database connection to use
     * @throws JMSException for any error
     */
    public void destroy(MessageRef reference, Connection connection)
            throws JMSException {
        final String messageId = reference.getMessageId();

        if (reference.isPersistent()) {
            try {
                PersistenceAdapter adapter = DatabaseService.getAdapter();
                adapter.removeMessage(connection, messageId);
            } catch (Exception exception) {
                _log.error("Failed to remove message", exception);
                throw new JMSException("Failed to remove message: "
                                       + exception.getMessage());
            }
            _persistent.remove(messageId);
        } else {
            _transient.remove(messageId);
        }
        _references.remove(messageId);
    }

    /**
     * Clear the persistent and non-persistent message cache.
     */
    public void clear() {
        _transient.clear();
        _persistent.clear();
        _references.clear();
    }

    /**
     * Clear only the persistent messages in the cache.
     */
    public void clearPersistentMessages() {
        _persistent.clear();
    }

    /**
     * Clear only the transient messages in the cache.
     */
    public void clearTransientMessages() {
        _transient.clear();
    }

    /**
     * Return the number of messages in the transient cache.
     *
     * @return the number of messages in the transient cache
     */
    public int getTransientCount() {
        return _transient.size();
    }

    /**
     * Return the number of messages in the persistent cache.
     *
     * @return the number of messages in the persistent cache
     */
    public int getPersistentCount() {
        return _persistent.size();
    }

    /**
     * Return the number of message references in the cache.
     *
     * @return the number of message references in the cache
     */
    public int getMessageCount() {
        return _references.size();
    }

    /**
     * Add a message reference to the cache.
     *
     * @param messageId the message identifier
     * @param reference the message reference
     */
    private void addMessageRef(String messageId, MessageRef reference) {
        _references.put(messageId, reference);
    }

}

⌨️ 快捷键说明

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