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