001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.broker.region;
018    
019    import javax.jms.JMSException;
020    
021    import org.apache.activemq.broker.BrokerService;
022    import org.apache.activemq.broker.ConnectionContext;
023    import org.apache.activemq.broker.region.policy.PolicyEntry;
024    import org.apache.activemq.command.ActiveMQDestination;
025    import org.apache.activemq.command.ConsumerInfo;
026    import org.apache.activemq.command.MessageDispatchNotification;
027    import org.apache.activemq.thread.TaskRunnerFactory;
028    import org.apache.activemq.usage.SystemUsage;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     * 
034     */
035    public class TempQueueRegion extends AbstractTempRegion {
036        private static final Logger LOG = LoggerFactory.getLogger(TempQueueRegion.class);
037        private final BrokerService brokerService;
038        
039        public TempQueueRegion(RegionBroker broker, BrokerService brokerService, DestinationStatistics destinationStatistics, SystemUsage memoryManager, TaskRunnerFactory taskRunnerFactory,
040                               DestinationFactory destinationFactory) {
041            super(broker, destinationStatistics, memoryManager, taskRunnerFactory, destinationFactory);
042            // We should allow the following to be configurable via a Destination
043            // Policy
044            // setAutoCreateDestinations(false);
045            this.brokerService = brokerService;
046        }
047    
048        protected Destination doCreateDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception {  
049            TempQueue result = new TempQueue(brokerService, destination, null, destinationStatistics, taskRunnerFactory);
050            brokerService.getDestinationPolicy();
051            configureQueue(result, destination);
052            result.initialize();
053            return result;
054        }
055    
056        protected Subscription createSubscription(ConnectionContext context, ConsumerInfo info) throws JMSException {
057            if (info.isBrowser()) {
058                return new QueueBrowserSubscription(broker,usageManager,context, info);
059            } else {
060                return new QueueSubscription(broker,usageManager,context, info);
061            }
062        }
063    
064        public String toString() {
065            return "TempQueueRegion: destinations=" + destinations.size() + ", subscriptions=" + subscriptions.size() + ", memory=" + usageManager.getMemoryUsage().getPercentUsage() + "%";
066        }
067    
068        public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) throws Exception {
069    
070            // Force a timeout value so that we don't get an error that
071            // there is still an active sub. Temp destination may be removed
072            // while a network sub is still active which is valid.
073            if (timeout == 0) {
074                timeout = 1;
075            }
076    
077            super.removeDestination(context, destination, timeout);
078        }
079        
080        /*
081         * For a Queue, dispatch order is imperative to match acks, so the dispatch is deferred till 
082         * the notification to ensure that the subscription chosen by the master is used.
083         * 
084         * (non-Javadoc)
085         * @see org.apache.activemq.broker.region.AbstractRegion#processDispatchNotification(org.apache.activemq.command.MessageDispatchNotification)
086         */
087        public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
088            processDispatchNotificationViaDestination(messageDispatchNotification);
089        }
090    
091        protected void configureQueue(Queue queue, ActiveMQDestination destination) {
092            if (broker == null) {
093                throw new IllegalStateException("broker property is not set");
094            }
095            if (broker.getDestinationPolicy() != null) {
096                PolicyEntry entry = broker.getDestinationPolicy().getEntryFor(destination);
097                if (entry != null) {
098                    entry.configure(broker,queue);
099                }
100            }
101        }
102    
103    }