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.ConnectionContext;
022    import org.apache.activemq.broker.region.policy.PolicyEntry;
023    import org.apache.activemq.command.ActiveMQDestination;
024    import org.apache.activemq.command.ActiveMQTempTopic;
025    import org.apache.activemq.command.ConsumerInfo;
026    import org.apache.activemq.thread.TaskRunnerFactory;
027    import org.apache.activemq.usage.SystemUsage;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    /**
032     * 
033     */
034    public class TempTopicRegion extends AbstractTempRegion {
035    
036        private static final Logger LOG = LoggerFactory.getLogger(TempTopicRegion.class);
037    
038        public TempTopicRegion(RegionBroker broker, DestinationStatistics destinationStatistics, SystemUsage memoryManager, TaskRunnerFactory taskRunnerFactory,
039                               DestinationFactory destinationFactory) {
040            super(broker, destinationStatistics, memoryManager, taskRunnerFactory, destinationFactory);
041        }
042    
043        protected Subscription createSubscription(ConnectionContext context, ConsumerInfo info) throws JMSException {
044            if (info.isDurable()) {
045                throw new JMSException("A durable subscription cannot be created for a temporary topic.");
046            }
047            try {
048                TopicSubscription answer = new TopicSubscription(broker, context, info, usageManager);
049                // lets configure the subscription depending on the destination
050                ActiveMQDestination destination = info.getDestination();
051                if (destination != null && broker.getDestinationPolicy() != null) {
052                    PolicyEntry entry = broker.getDestinationPolicy().getEntryFor(destination);
053                    if (entry != null) {
054                        entry.configure(broker, usageManager, answer);
055                    }
056                }
057                answer.init();
058                return answer;
059            } catch (Exception e) {
060                LOG.error("Failed to create TopicSubscription ", e);
061                JMSException jmsEx = new JMSException("Couldn't create TopicSubscription");
062                jmsEx.setLinkedException(e);
063                throw jmsEx;
064            }
065        }
066    
067        public String toString() {
068            return "TempTopicRegion: destinations=" + destinations.size() + ", subscriptions=" + subscriptions.size() + ", memory=" + usageManager.getMemoryUsage().getPercentUsage() + "%";
069        }
070    
071        public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) throws Exception {
072    
073            // Force a timeout value so that we don't get an error that
074            // there is still an active sub. Temp destination may be removed
075            // while a network sub is still active which is valid.
076            if (timeout == 0) {
077                timeout = 1;
078            }
079    
080            super.removeDestination(context, destination, timeout);
081        }
082    
083    }