001
014
015 package com.liferay.portal.cache.bootstrap;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020
021 import java.util.Properties;
022
023 import net.sf.ehcache.CacheManager;
024 import net.sf.ehcache.Ehcache;
025 import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
026
027
031 public class EhcacheStreamBootstrapCacheLoader implements BootstrapCacheLoader {
032
033 public EhcacheStreamBootstrapCacheLoader(Properties properties) {
034 if (properties != null) {
035 _bootstrapAsynchronously = GetterUtil.getBoolean(
036 properties.getProperty("bootstrapAsynchronously"));
037 }
038 else {
039 _bootstrapAsynchronously = true;
040 }
041 }
042
043 @Override
044 public Object clone() {
045 return this;
046 }
047
048 public void doLoad(Ehcache ehcache) {
049 if (_log.isDebugEnabled()) {
050 _log.debug("Bootstraping " + ehcache.getName());
051 }
052
053 CacheManager cacheManager = ehcache.getCacheManager();
054
055 try {
056 ClusterLinkBootstrapLoaderHelperUtil.loadCachesFromCluster(
057 cacheManager.getName(), ehcache.getName());
058 }
059 catch (Exception e) {
060 if (_log.isWarnEnabled()) {
061 _log.warn("Unable to load cache data from the cluster", e);
062 }
063 }
064 }
065
066 @Override
067 public boolean isAsynchronous() {
068 return _bootstrapAsynchronously;
069 }
070
071 @Override
072 public void load(Ehcache ehcache) {
073 if (ClusterLinkBootstrapLoaderHelperUtil.isSkipped()) {
074 return;
075 }
076
077 if (_bootstrapAsynchronously) {
078 EhcacheStreamClientThread streamBootstrapThread =
079 new EhcacheStreamClientThread(ehcache);
080
081 streamBootstrapThread.start();
082 }
083 else {
084 doLoad(ehcache);
085 }
086 }
087
088 private static final Log _log = LogFactoryUtil.getLog(
089 EhcacheStreamBootstrapCacheLoader.class);
090
091 private final boolean _bootstrapAsynchronously;
092
093 private class EhcacheStreamClientThread extends Thread {
094
095 public EhcacheStreamClientThread(Ehcache ehcache) {
096 if (_log.isDebugEnabled()) {
097 _log.debug(
098 "Ehcache stream client thread for cache " +
099 ehcache.getName());
100 }
101
102 _ehcache = ehcache;
103
104 setDaemon(true);
105 setName(
106 EhcacheStreamClientThread.class.getName() + " - " +
107 ehcache.getName());
108 setPriority(Thread.NORM_PRIORITY);
109 }
110
111 @Override
112 public void run() {
113 try {
114 doLoad(_ehcache);
115 }
116 catch (Exception e) {
117 if (_log.isWarnEnabled()) {
118 _log.warn("Unable to asynchronously stream bootstrap", e);
119 }
120 }
121 }
122
123 private final Ehcache _ehcache;
124
125 }
126
127 }