001
014
015 package com.liferay.portal.cache.ehcache;
016
017 import com.liferay.portal.kernel.util.JavaDetector;
018 import com.liferay.portal.kernel.util.ReflectionUtil;
019 import com.liferay.portal.util.PropsValues;
020
021 import java.lang.reflect.Field;
022
023 import java.util.concurrent.BlockingQueue;
024 import java.util.concurrent.DelayQueue;
025 import java.util.concurrent.RunnableScheduledFuture;
026 import java.util.concurrent.ScheduledThreadPoolExecutor;
027 import java.util.concurrent.ThreadPoolExecutor;
028
029 import net.sf.ehcache.CacheManager;
030 import net.sf.ehcache.config.Configuration;
031
032
035 public class CacheManagerUtil {
036
037 public static CacheManager createCacheManager(Configuration configuration) {
038 CacheManager cacheManager = new CacheManager(configuration);
039
040 try {
041 ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
042 (ScheduledThreadPoolExecutor)_statisticsExecutorField.get(
043 cacheManager);
044
045 BlockingQueue<Runnable> blockingQueue = null;
046
047
048
049
050 if (JavaDetector.isJDK6()) {
051 blockingQueue = (BlockingQueue<Runnable>)_workQueueField.get(
052 scheduledThreadPoolExecutor);;
053
054 _workQueueField.set(
055 scheduledThreadPoolExecutor,
056 new DelayQueue<RunnableScheduledFuture<?>>() {
057
058 @Override
059 public int remainingCapacity() {
060 return 0;
061 }
062
063 });
064 }
065
066 scheduledThreadPoolExecutor.setCorePoolSize(
067 PropsValues.EHCACHE_CACHE_MANAGER_STATISTICS_THREAD_POOL_SIZE);
068
069 if (JavaDetector.isJDK6()) {
070 while (
071 scheduledThreadPoolExecutor.getPoolSize() >
072 PropsValues.
073 EHCACHE_CACHE_MANAGER_STATISTICS_THREAD_POOL_SIZE);
074
075 _workQueueField.set(scheduledThreadPoolExecutor, blockingQueue);
076 }
077 }
078 catch (Exception e) {
079 throw new RuntimeException(e);
080 }
081
082 return cacheManager;
083 }
084
085 private static Field _statisticsExecutorField;
086 private static Field _workQueueField;
087
088 static {
089 try {
090 _statisticsExecutorField = ReflectionUtil.getDeclaredField(
091 CacheManager.class, "statisticsExecutor");
092
093 if (JavaDetector.isJDK6()) {
094 _workQueueField = ReflectionUtil.getDeclaredField(
095 ThreadPoolExecutor.class, "workQueue");
096 }
097 }
098 catch (Exception e) {
099 throw new ExceptionInInitializerError(e);
100 }
101 }
102
103 }