diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java index cae18a4ae..aa2985adc 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java @@ -105,7 +105,7 @@ public abstract class AbstractDataCache extends AbstractConcurrentEventManager public void initialize(DataCacheManager manager) { if (_schedule != null && !"".equals(_schedule)) { - DataCacheScheduler scheduler = manager.getDataCacheScheduler(); + ClearableScheduler scheduler = manager.getClearableScheduler(); if (scheduler != null) scheduler.scheduleEviction(this, _schedule); } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheScheduler.java b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ClearableScheduler.java similarity index 84% rename from openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheScheduler.java rename to openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ClearableScheduler.java index 6bee40660..82e343b6e 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheScheduler.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ClearableScheduler.java @@ -21,18 +21,17 @@ package org.apache.openjpa.datacache; import java.security.AccessController; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; -import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.StringTokenizer; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import org.apache.openjpa.conf.OpenJPAConfiguration; import org.apache.openjpa.lib.log.Log; +import org.apache.openjpa.lib.util.Clearable; import org.apache.openjpa.lib.util.J2DoPrivHelper; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.util.InvalidStateException; @@ -41,40 +40,38 @@ import org.apache.openjpa.util.UserException; import serp.util.Strings; /** - * Cron-style cache eviction. Understands schedules based on cron format: - * minute hour mday month wday + * Cron-style clearable eviction. Understands schedules based on cron format: + *
  • minute hour mday month wday
  • + *
  • +minute
  • * For example: * 15,30 6,19 2,10 1 2 * Would run at 15 and 30 past the 6AM and 7PM, on the 2nd and 10th * of January when its a Monday. * - * @author Steve Kim */ -public class DataCacheScheduler - implements Runnable { +public class ClearableScheduler implements Runnable { - private static final Localizer _loc = Localizer.forPackage - (DataCacheScheduler.class); + private static final Localizer _loc = Localizer.forPackage(ClearableScheduler.class); - private Map _caches = new ConcurrentHashMap(); + private Map _clearables = new ConcurrentHashMap(); private boolean _stop = false; private int _interval = 1; private Log _log; private Thread _thread; - public DataCacheScheduler(OpenJPAConfiguration conf) { + public ClearableScheduler(OpenJPAConfiguration conf) { _log = conf.getLogFactory().getLog(OpenJPAConfiguration.LOG_DATACACHE); } /** - * The interval time in minutes between cache checks. Defaults to 1. + * The interval time in minutes between scheduler checks. Defaults to 1. */ public int getInterval() { return _interval; } /** - * The interval time in minutes between cache checks. Defaults to 1. + * The interval time in minutes between scheduler checks. Defaults to 1. */ public void setInterval(int interval) { _interval = interval; @@ -92,15 +89,15 @@ public class DataCacheScheduler } /** - * Schedule the given cache for eviction. Starts the scheduling thread + * Schedule the given Clearable for clear to be called. Starts the scheduling thread * if not started. */ - public synchronized void scheduleEviction(DataCache cache, String times) { + public synchronized void scheduleEviction(Clearable clearable, String times) { if (times == null) return; Schedule schedule = new Schedule(times); - _caches.put(cache, schedule); + _clearables.put(clearable, schedule); _stop = false; if (_thread == null) { _thread = @@ -114,11 +111,11 @@ public class DataCacheScheduler } /** - * Remove the given cache from scheduling. + * Remove the given Clearable from scheduling. */ - public synchronized void removeFromSchedule(DataCache cache) { - _caches.remove(cache); - if (_caches.size() == 0) + public synchronized void removeFromSchedule(Clearable clearable) { + _clearables.remove(clearable); + if (_clearables.size() == 0) stop(); } @@ -133,18 +130,13 @@ public class DataCacheScheduler Thread.sleep(_interval * 60 * 1000); Date now = new Date(); - DataCache cache; - Schedule schedule; - Map.Entry entry; - for (Iterator i = _caches.entrySet().iterator(); i.hasNext();) { - entry = (Map.Entry) i.next(); - cache = (DataCache) entry.getKey(); - schedule = (Schedule) entry.getValue(); + for(Entry entry : _clearables.entrySet()){ + Clearable clearable = entry.getKey(); + Schedule schedule = entry.getValue(); if (schedule.matches(lastRun, now)) { if (_log.isTraceEnabled()) - _log.trace(_loc.get("scheduler-clear", - cache.getName(), fom.format(now))); - evict(cache); + _log.trace(_loc.get("scheduler-clear", clearable, fom.format(now))); + evict(clearable); } } lastRun = now; @@ -161,7 +153,7 @@ public class DataCacheScheduler } } - protected void evict(DataCache cache) { + protected void evict(Clearable cache) { cache.clear(); } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java index 979db3f14..f9fc48db8 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCache.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.openjpa.lib.util.Clearable; import org.apache.openjpa.lib.util.Closeable; /** @@ -40,7 +41,7 @@ import org.apache.openjpa.lib.util.Closeable; * @author Pinaki Poddar */ public interface DataCache - extends Closeable { + extends Closeable, Clearable { /** * The name of the default data cache: default diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java index 679cf1446..1eed5c181 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java @@ -77,7 +77,7 @@ public interface DataCacheManager { /** * Return the runnable which schedules evictions. */ - public DataCacheScheduler getDataCacheScheduler(); + public ClearableScheduler getClearableScheduler(); /** * Select the cache where the given managed proxy instance should be cached. diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java index d3f4c0cc2..99a2100d6 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java @@ -31,7 +31,7 @@ import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.util.ImplHelper; /** - * Default data cache manager provides handle to utilities {@linkplain PCDataGenerator}, {@linkplain DataCacheScheduler} + * Default data cache manager provides handle to utilities {@linkplain PCDataGenerator}, {@linkplain ClearableScheduler} * and {@linkplain CacheDistributionPolicy} for the cache operation. This implementation also determines whether a * managed type is eligible to cache. * @@ -46,7 +46,7 @@ public class DataCacheManagerImpl private DataCache _cache = null; private QueryCache _queryCache = null; private DataCachePCDataGenerator _pcGenerator = null; - private DataCacheScheduler _scheduler = null; + private ClearableScheduler _scheduler = null; private CacheDistributionPolicy _policy = new DefaultCacheDistributionPolicy(); private Map _cacheable = new HashMap(); @@ -64,7 +64,7 @@ public class DataCacheManagerImpl // create helpers before initializing caches if (conf.getDynamicDataStructs()) _pcGenerator = new DataCachePCDataGenerator(conf); - _scheduler = new DataCacheScheduler(conf); + _scheduler = new ClearableScheduler(conf); _policy = conf.getCacheDistributionPolicyInstance(); @@ -104,7 +104,7 @@ public class DataCacheManagerImpl return _pcGenerator; } - public DataCacheScheduler getDataCacheScheduler() { + public ClearableScheduler getClearableScheduler() { return _scheduler; } diff --git a/openjpa-kernel/src/main/resources/org/apache/openjpa/datacache/localizer.properties b/openjpa-kernel/src/main/resources/org/apache/openjpa/datacache/localizer.properties index 3fed57f6c..23c040a0c 100644 --- a/openjpa-kernel/src/main/resources/org/apache/openjpa/datacache/localizer.properties +++ b/openjpa-kernel/src/main/resources/org/apache/openjpa/datacache/localizer.properties @@ -63,12 +63,12 @@ query-cache-key-removed: Key "{0}" has been removed from the query cache. reg-mbean: Registered MBean ({0}). cant-reg-mbean: Unable to register Datastore Cache {0} MBean. query-cache-name: Query Cache -scheduler-start: Starting cache scheduler thread "{0}"... -scheduler-name: OpenJPA Cache Scheduler -scheduler-interval: Cache schedule thread will check every "{0}" min. -scheduler-clear: Scheduled cache eviction at time "{1}" for cache:"{0}" -scheduler-stop: Stopping scheduled cache eviction thread. -scheduler-fail: Cache scheduler thread unexpectedly interrupted. Stopping \ +scheduler-start: Starting clearable scheduler thread "{0}"... +scheduler-name: OpenJPA Clearable Scheduler +scheduler-interval: Clearable schedule thread will check every "{0}" min. +scheduler-clear: Scheduled clearable eviction at time "{1}" for clearable:"{0}" +scheduler-stop: Stopping scheduled clearable eviction thread. +scheduler-fail: Clearable scheduler thread unexpectedly interrupted. Stopping \ scheduler. bad-schedule: Invalid schedule string: "{0}" not-number: Token "{0}" is not a valid number or wildcard (*) for schedule. diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Clearable.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Clearable.java new file mode 100644 index 000000000..165337302 --- /dev/null +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Clearable.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.lib.util; + +/** + * Generic interface for components that can be cleared so that helpers can treat them in a generic + * way. + */ +public interface Clearable { + + public void clear(); + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestClearableScheduler.java similarity index 96% rename from openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java rename to openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestClearableScheduler.java index eb540f3f8..4709bc2df 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestDataCacheScheduler.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestClearableScheduler.java @@ -26,12 +26,12 @@ import java.util.List; import org.apache.openjpa.conf.OpenJPAConfiguration; import org.apache.openjpa.datacache.ConcurrentDataCache; import org.apache.openjpa.datacache.DataCacheManager; -import org.apache.openjpa.datacache.DataCacheScheduler; +import org.apache.openjpa.datacache.ClearableScheduler; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.datacache.common.apps.ScheduledEviction; import org.apache.openjpa.persistence.test.SingleEMFTestCase; -public class TestDataCacheScheduler extends SingleEMFTestCase { +public class TestClearableScheduler extends SingleEMFTestCase { private static String getMinutesString() { StringBuffer buf = new StringBuffer(); @@ -54,7 +54,7 @@ public class TestDataCacheScheduler extends SingleEMFTestCase { public void testBasic() throws Exception { OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAConfiguration conf = ((OpenJPAEntityManagerSPI) em).getConfiguration(); - DataCacheScheduler scheduler = new DataCacheScheduler(conf); + ClearableScheduler scheduler = new ClearableScheduler(conf); // Make the scheduler run every 1 minute scheduler.setInterval(1); DummyCache cache1 = new DummyCache();