From a420312a404483fd48488832975767d19a4fc5a9 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 24 Aug 2006 14:01:30 +0000 Subject: [PATCH] avoid the possibility that 2 threads could attempt to add a destination at the same time causing it to be created twice and avoid issues such as duplicate registration in JMX git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@434397 13f79535-47bb-0310-9956-ffa450edef68 --- .../broker/region/AbstractRegion.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractRegion.java b/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractRegion.java index 7e517fb8c1..c0ae11478e 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractRegion.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractRegion.java @@ -77,24 +77,32 @@ abstract public class AbstractRegion implements Region { public Destination addDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception { log.debug("Adding destination: "+destination); - Destination dest = createDestination(context, destination); - // intercept if there is a valid interceptor defined - DestinationInterceptor destinationInterceptor = broker.getDestinationInterceptor(); - if (destinationInterceptor != null) { - dest = destinationInterceptor.intercept(dest); - } - - dest.start(); - synchronized(destinationsMutex){ - destinations.put(destination,dest); - destinationMap.put(destination,dest); - - // Add all consumers that are interested in the destination. - for (Iterator iter = subscriptions.values().iterator(); iter.hasNext();) { - Subscription sub = (Subscription) iter.next(); - if( sub.matches(destination) ) { - dest.addSubscription(context, sub); + synchronized (destinationsMutex) { + Destination dest = (Destination) destinations.get(destination); + if (dest != null) { + log.warn("Attempt to add destination which is already created: " + destination); + } + else { + dest = createDestination(context, destination); + + // intercept if there is a valid interceptor defined + DestinationInterceptor destinationInterceptor = broker.getDestinationInterceptor(); + if (destinationInterceptor != null) { + dest = destinationInterceptor.intercept(dest); + } + + dest.start(); + + destinations.put(destination, dest); + destinationMap.put(destination, dest); + + // Add all consumers that are interested in the destination. + for (Iterator iter = subscriptions.values().iterator(); iter.hasNext();) { + Subscription sub = (Subscription) iter.next(); + if (sub.matches(destination)) { + dest.addSubscription(context, sub); + } } } return dest;