Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » store » jpa » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.activemq.store.jpa;
   18   
   19   import java.io.IOException;
   20   import java.util.HashSet;
   21   import java.util.List;
   22   import java.util.Map;
   23   import java.util.Set;
   24   
   25   import javax.persistence.EntityManager;
   26   import javax.persistence.Query;
   27   
   28   import org.apache.activemq.command.ActiveMQDestination;
   29   import org.apache.activemq.command.ActiveMQQueue;
   30   import org.apache.activemq.command.ActiveMQTopic;
   31   import org.apache.activemq.command.TransactionId;
   32   import org.apache.activemq.store.MessageStore;
   33   import org.apache.activemq.store.ReferenceStore;
   34   import org.apache.activemq.store.ReferenceStoreAdapter;
   35   import org.apache.activemq.store.TopicMessageStore;
   36   import org.apache.activemq.store.TopicReferenceStore;
   37   import org.apache.activemq.store.amq.AMQTx;
   38   import org.apache.activemq.util.IOExceptionSupport;
   39   
   40   /**
   41    * An implementation of {@link ReferenceStoreAdapter} that uses JPA to store
   42    * it's message references.
   43    * 
   44    * @org.apache.xbean.XBean element="jpaReferenceStoreAdapter"
   45    * @version $Revision: 1.17 $
   46    */
   47   public class JPAReferenceStoreAdapter extends JPAPersistenceAdapter implements ReferenceStoreAdapter {
   48   
   49       @Override
   50       public MessageStore createQueueMessageStore(ActiveMQQueue destination) throws IOException {
   51           throw new RuntimeException("Use createQueueReferenceStore instead.");
   52       }
   53   
   54       @Override
   55       public TopicMessageStore createTopicMessageStore(ActiveMQTopic destination) throws IOException {
   56           throw new RuntimeException("Use createTopicReferenceStore instead.");
   57       }
   58   
   59       public ReferenceStore createQueueReferenceStore(ActiveMQQueue destination) throws IOException {
   60           JPAReferenceStore rc = new JPAReferenceStore(this, destination);
   61           return rc;
   62       }
   63   
   64       public TopicReferenceStore createTopicReferenceStore(ActiveMQTopic destination) throws IOException {
   65           JPATopicReferenceStore rc = new JPATopicReferenceStore(this, destination);
   66           return rc;
   67       }
   68   
   69       public void deleteAllMessages() throws IOException {
   70           EntityManager manager = beginEntityManager(null);
   71           try {
   72               Query query = manager.createQuery("delete from StoredMessageReference m");
   73               query.executeUpdate();
   74               query = manager.createQuery("delete from StoredSubscription ss");
   75               query.executeUpdate();
   76           } catch (Throwable e) {
   77               rollbackEntityManager(null, manager);
   78               throw IOExceptionSupport.create(e);
   79           }
   80           commitEntityManager(null, manager);
   81       }
   82   
   83       public Set<ActiveMQDestination> getDestinations() {
   84           HashSet<ActiveMQDestination> rc = new HashSet<ActiveMQDestination>();
   85   
   86           EntityManager manager = beginEntityManager(null);
   87           try {
   88               Query query = manager.createQuery("select distinct m.destination from StoredMessageReference m");
   89               for (String dest : (List<String>)query.getResultList()) {
   90                   rc.add(ActiveMQDestination.createDestination(dest, ActiveMQDestination.QUEUE_TYPE));
   91               }
   92           } catch (RuntimeException e) {
   93               rollbackEntityManager(null, manager);
   94               throw e;
   95           }
   96           commitEntityManager(null, manager);
   97           return rc;
   98       }
   99   
  100       public long getLastMessageBrokerSequenceId() throws IOException {
  101           long rc = 0;
  102           EntityManager manager = beginEntityManager(null);
  103           try {
  104               Query query = manager.createQuery("select max(m.id) from StoredMessageReference m");
  105               Long t = (Long)query.getSingleResult();
  106               if (t != null) {
  107                   rc = t;
  108               }
  109           } catch (Throwable e) {
  110               rollbackEntityManager(null, manager);
  111               throw IOExceptionSupport.create(e);
  112           }
  113           commitEntityManager(null, manager);
  114           return rc;
  115       }
  116   
  117       public Set<Integer> getReferenceFileIdsInUse() throws IOException {
  118           HashSet<Integer> rc = null;
  119           EntityManager manager = beginEntityManager(null);
  120           try {
  121               Query query = manager.createQuery("select distinct m.fileId from StoredMessageReference m");
  122               rc = new HashSet<Integer>((List<Integer>)query.getResultList());
  123           } catch (Throwable e) {
  124               rollbackEntityManager(null, manager);
  125               throw IOExceptionSupport.create(e);
  126           }
  127           commitEntityManager(null, manager);
  128           return rc;
  129       }
  130   
  131       /**
  132        * @return
  133        * @see org.apache.activemq.store.ReferenceStoreAdapter#isStoreValid()
  134        */
  135       public boolean isStoreValid() {
  136           return false;
  137       }
  138   
  139       /**
  140        * @see org.apache.activemq.store.ReferenceStoreAdapter#clearMessages()
  141        */
  142       public void clearMessages() {
  143       }
  144   
  145       /**
  146        * @see org.apache.activemq.store.ReferenceStoreAdapter#recoverState()
  147        */
  148       public void recoverState() {
  149       }
  150   
  151       public Map<TransactionId, AMQTx> retrievePreparedState() throws IOException {
  152           return null;
  153       }
  154   
  155       public void savePreparedState(Map<TransactionId, AMQTx> map) throws IOException {
  156       }
  157   
  158       public long getMaxDataFileLength() {
  159           return 0;
  160       }
  161      
  162       public void setMaxDataFileLength(long maxDataFileLength) {        
  163       }
  164   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » store » jpa » [javadoc | source]