View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.loader;
18  
19  
20  import net.sf.ehcache.CacheException;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.util.*;
25  
26  
27  /***
28   * A cache loader that introduces a configurable delay when used
29   *
30   * @author Ludovic Orban
31   * @version $Id: DelayingLoader.html 13146 2011-08-01 17:12:39Z oletizi $
32   */
33  public class DelayingLoader extends CountingCacheLoader {
34  
35      private static final Logger LOG = LoggerFactory.getLogger(DelayingLoader.class.getName());
36  
37      private int loadCounter;
38      private int loadAllCounter;
39      private Random random = new Random();
40      private String name = "ExceptionThrowingLoader";
41      private long delayMillis;
42  
43      public DelayingLoader(long delayMillis) {
44          this.delayMillis = delayMillis;
45      }
46  
47      /***
48       * loads an object. Application writers should implement this
49       * method to customize the loading of cache object. This method is called
50       * by the caching service when the requested object is not in the cache.
51       * <p/>
52       *
53       * @param key the key identifying the object being loaded
54       * @return The object that is to be stored in the cache.
55       */
56      public Object load(Object key) throws CacheException {
57          try {
58              Thread.sleep(delayMillis);
59          } catch (InterruptedException e) {
60              LOG.error("Interrupted");
61          }
62          throw new CacheException("Some exception with key " + key);
63      }
64  
65      /***
66       * loads multiple object. Application writers should implement this
67       * method to customize the loading of cache object. This method is called
68       * by the caching service when the requested object is not in the cache.
69       * <p/>
70       *
71       * @param keys a Collection of keys identifying the objects to be loaded
72       * @return A Map of objects that are to be stored in the cache.
73       */
74  
75      public Map loadAll(Collection keys) throws CacheException {
76          try {
77              Thread.sleep(delayMillis);
78          } catch (InterruptedException e) {
79              LOG.error("Interrupted");
80          }
81          throw new CacheException("Some exception with keys " + keys);
82      }
83  
84  
85      /***
86       * Load using both a key and an argument.
87       * <p/>
88       * JCache will use the load(key) method where the argument is null.
89       */
90      public Object load(Object key, Object argument) throws CacheException {
91          try {
92              Thread.sleep(random.nextInt(3) + 1);
93          } catch (InterruptedException e) {
94              LOG.error("Interrupted");
95          }
96          throw new CacheException("Some exception with key " + key);
97      }
98  
99      /***
100      * Load using both a key and an argument.
101      * <p/>
102      * JCache will use the loadAll(key) method where the argument is null.
103      */
104     public Map loadAll(Collection keys, Object argument) throws CacheException {
105         throw new CacheException("Some exception with key " + keys.toArray()[0]);
106     }
107 
108 }