From c451951148fe380030bc2089bd5f2613a64e4382 Mon Sep 17 00:00:00 2001 From: Robert Davies Date: Thu, 13 Mar 2008 11:36:43 +0000 Subject: [PATCH] Added properties for caching of temp destinations git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@636724 13f79535-47bb-0310-9956-ffa450edef68 --- .../broker/region/AbstractTempRegion.java | 97 +++++++++++-------- 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractTempRegion.java b/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractTempRegion.java index 041b62e1e1..896a209ab8 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractTempRegion.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/region/AbstractTempRegion.java @@ -31,14 +31,18 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** - * + * */ public abstract class AbstractTempRegion extends AbstractRegion { - private static int TIME_BEFORE_PURGE = 60000; private static final Log LOG = LogFactory.getLog(TempQueueRegion.class); - private Map cachedDestinations = new HashMap(); - private final Timer purgeTimer; - private final TimerTask purgeTask; + + private Map cachedDestinations = new HashMap(); + private final boolean doCacheTempDestinations; + private final int purgeTime; + private Timer purgeTimer; + private TimerTask purgeTask; + + /** * @param broker * @param destinationStatistics @@ -52,56 +56,70 @@ public abstract class AbstractTempRegion extends AbstractRegion { DestinationFactory destinationFactory) { super(broker, destinationStatistics, memoryManager, taskRunnerFactory, destinationFactory); - this.purgeTimer = new Timer(true); - this.purgeTask = new TimerTask() { - public void run() { - doPurge(); - } - - }; - this.purgeTimer.schedule(purgeTask, TIME_BEFORE_PURGE,TIME_BEFORE_PURGE); - } + this.doCacheTempDestinations=broker.getBrokerService().isCacheTempDestinations(); + this.purgeTime = broker.getBrokerService().getTimeBeforePurgeTempDestinations(); + if (this.doCacheTempDestinations) { + this.purgeTimer = new Timer(true); + this.purgeTask = new TimerTask() { + public void run() { + doPurge(); + } + }; + } + this.purgeTimer.schedule(purgeTask, purgeTime, purgeTime); + } + public void stop() throws Exception { super.stop(); if (purgeTimer != null) { purgeTimer.cancel(); } } - - protected abstract Destination doCreateDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception; - protected synchronized Destination createDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception { - Destination result = cachedDestinations.remove(new CachedDestination(destination)); - if (result==null) { + protected abstract Destination doCreateDestination( + ConnectionContext context, ActiveMQDestination destination) + throws Exception; + + protected synchronized Destination createDestination( + ConnectionContext context, ActiveMQDestination destination) + throws Exception { + Destination result = cachedDestinations.remove(new CachedDestination( + destination)); + if (result == null) { result = doCreateDestination(context, destination); } return result; } - - protected final synchronized void dispose(ConnectionContext context,Destination dest) throws Exception { - //add to cache - cachedDestinations.put(new CachedDestination(dest.getActiveMQDestination()), dest); + + protected final synchronized void dispose(ConnectionContext context, + Destination dest) throws Exception { + // add to cache + if (this.doCacheTempDestinations) { + cachedDestinations.put(new CachedDestination(dest + .getActiveMQDestination()), dest); + } } - + private void doDispose(Destination dest) { ConnectionContext context = new ConnectionContext(); try { dest.dispose(context); dest.stop(); } catch (Exception e) { - LOG.warn("Failed to dispose of " + dest,e); + LOG.warn("Failed to dispose of " + dest, e); } - + } - + private synchronized void doPurge() { long currentTime = System.currentTimeMillis(); if (cachedDestinations.size() > 0) { - Set tmp = new HashSet(cachedDestinations.keySet()); - for(CachedDestination key: tmp) { - if ((key.timeStamp + TIME_BEFORE_PURGE) < currentTime) { + Set tmp = new HashSet( + cachedDestinations.keySet()); + for (CachedDestination key : tmp) { + if ((key.timeStamp + purgeTime) < currentTime) { Destination dest = cachedDestinations.remove(key); if (dest != null) { doDispose(dest); @@ -110,20 +128,21 @@ public abstract class AbstractTempRegion extends AbstractRegion { } } } - - static class CachedDestination{ + + static class CachedDestination { long timeStamp; + ActiveMQDestination destination; - - CachedDestination(ActiveMQDestination destination){ - this.destination=destination; - this.timeStamp=System.currentTimeMillis(); + + CachedDestination(ActiveMQDestination destination) { + this.destination = destination; + this.timeStamp = System.currentTimeMillis(); } - + public int hashCode() { return destination.hashCode(); } - + public boolean equals(Object o) { if (o instanceof CachedDestination) { CachedDestination other = (CachedDestination) o; @@ -131,7 +150,7 @@ public abstract class AbstractTempRegion extends AbstractRegion { } return false; } - + } }