KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > TempQueueRegion


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker.region;
19
20 import javax.jms.InvalidSelectorException JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import org.apache.activemq.broker.Broker;
23 import org.apache.activemq.broker.ConnectionContext;
24 import org.apache.activemq.command.ActiveMQDestination;
25 import org.apache.activemq.command.ActiveMQTempDestination;
26 import org.apache.activemq.command.ConsumerInfo;
27 import org.apache.activemq.memory.UsageManager;
28 import org.apache.activemq.thread.TaskRunnerFactory;
29
30 /**
31  *
32  * @version $Revision: 1.7 $
33  */

34 public class TempQueueRegion extends AbstractRegion {
35
36     public TempQueueRegion(RegionBroker broker,DestinationStatistics destinationStatistics, UsageManager memoryManager, TaskRunnerFactory taskRunnerFactory, DestinationFactory destinationFactory) {
37         super(broker,destinationStatistics, memoryManager, taskRunnerFactory, destinationFactory);
38         // We should allow the following to be configurable via a Destination Policy
39
// setAutoCreateDestinations(false);
40
}
41     
42     protected Destination createDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception JavaDoc {
43         final ActiveMQTempDestination tempDest = (ActiveMQTempDestination) destination;
44         return new Queue(destination, memoryManager, null, destinationStatistics, taskRunnerFactory, null) {
45             
46             public void addSubscription(ConnectionContext context,Subscription sub) throws Exception JavaDoc {
47
48                 // Only consumers on the same connection can consume from
49
// the temporary destination
50
if( !context.isNetworkConnection() && !tempDest.getConnectionId().equals( sub.getConsumerInfo().getConsumerId().getConnectionId() ) ) {
51                     throw new JMSException JavaDoc("Cannot subscribe to remote temporary destination: "+tempDest);
52                 }
53                 super.addSubscription(context, sub);
54             };
55             
56         };
57     }
58
59     protected Subscription createSubscription(ConnectionContext context, ConsumerInfo info) throws InvalidSelectorException JavaDoc {
60         if( info.isBrowser() ) {
61             return new QueueBrowserSubscription(broker,context, info);
62         } else {
63             return new QueueSubscription(broker,context, info);
64         }
65     }
66     
67     public String JavaDoc toString() {
68         return "TempQueueRegion: destinations="+destinations.size()+", subscriptions="+subscriptions.size()+", memory="+memoryManager.getPercentUsage()+"%";
69     }
70     
71     public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) throws Exception JavaDoc {
72         
73         // Force a timeout value so that we don't get an error that
74
// there is still an active sub. Temp destination may be removed
75
// while a network sub is still active which is valid.
76
if( timeout == 0 )
77             timeout = 1;
78         
79         super.removeDestination(context, destination, timeout);
80     }
81     
82 }
83
Popular Tags