CachePersistenceAdapter xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.store.cache;
    19
    20  import java.io.IOException;
    21  import java.util.Map;
    22
    23  import javax.jms.JMSException;
    24
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.codehaus.activemq.service.impl.PersistenceAdapterSupport;
    28  import org.codehaus.activemq.store.MessageStore;
    29  import org.codehaus.activemq.store.PersistenceAdapter;
    30  import org.codehaus.activemq.store.PreparedTransactionStore;
    31  import org.codehaus.activemq.store.TopicMessageStore;
    32
    33  /***
    34   * Implements a {@link PersistenceAdapter} designed to
    35   * to speed up access to recently added messages by using
    36   * a MessageCache .
    37   *
    38   * @version $Revision: 1.2 $
    39   */
    40  public abstract class CachePersistenceAdapter extends PersistenceAdapterSupport {
    41
    42      private static final Log log = LogFactory.getLog(CachePersistenceAdapter.class);
    43      private PersistenceAdapter longTermPersistence;
    44
    45      public CachePersistenceAdapter() {
    46      }
    47
    48      public CachePersistenceAdapter(PersistenceAdapter longTermPersistence) throws IOException {
    49  		this.longTermPersistence = longTermPersistence;
    50      }
    51
    52      public Map getInitialDestinations() {
    53          return longTermPersistence.getInitialDestinations();
    54      }
    55
    56      public MessageStore createQueueMessageStore(String destinationName) throws JMSException {
    57          MessageStore longtermStore = longTermPersistence.createQueueMessageStore(destinationName);
    58          CacheMessageStore store = new CacheMessageStore(this, longtermStore, createMessageCache(destinationName));
    59          return store;
    60      }
    61
    62  	public TopicMessageStore createTopicMessageStore(String destinationName) throws JMSException {
    63      	TopicMessageStore longtermStore = longTermPersistence.createTopicMessageStore(destinationName);
    64          CacheTopicMessageStore store = new CacheTopicMessageStore(this, longtermStore, new SimpleMessageCache());
    65          return store;
    66      }
    67
    68      public PreparedTransactionStore createPreparedTransactionStore() throws JMSException {
    69          return longTermPersistence.createPreparedTransactionStore();
    70      }
    71
    72      public void beginTransaction() throws JMSException {
    73      	longTermPersistence.beginTransaction();
    74      }
    75
    76      public void commitTransaction() throws JMSException {
    77      	longTermPersistence.commitTransaction();
    78      }
    79
    80      public void rollbackTransaction() {
    81      	longTermPersistence.rollbackTransaction();
    82      }
    83
    84      public void start() throws JMSException {
    85          longTermPersistence.start();
    86      }
    87
    88  	public void stop() throws JMSException {
    89          longTermPersistence.stop();
    90      }
    91
    92      // Properties
    93      //-------------------------------------------------------------------------
    94      public PersistenceAdapter getLongTermPersistence() {
    95          return longTermPersistence;
    96      }
    97
    98      public void setLongTermPersistence(PersistenceAdapter longTermPersistence) {
    99          this.longTermPersistence = longTermPersistence;
    100     }
    101
    102     /***
    103      * Subclasses should override this method to change the type
    104      * of MessageCache that is used to cache messages.
    105      *
    106      * @param destinationName
    107      * @return
    108      */
    109 	abstract protected MessageCache createMessageCache(String destinationName);
    110
    111 }