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  package net.sf.ehcache.writer;
17  
18  import java.util.Collection;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import net.sf.ehcache.CacheEntry;
23  import net.sf.ehcache.CacheException;
24  import net.sf.ehcache.Element;
25  
26  public class TestCacheWriterSlow extends AbstractTestCacheWriter {
27      private final Map<Object, Element> writtenElements = new HashMap<Object, Element>();
28      private final Map<Object, Element> deletedElements = new HashMap<Object, Element>();
29  
30      public TestCacheWriterSlow() {
31      }
32  
33      public Map<Object, Element> getWrittenElements() {
34          return writtenElements;
35      }
36  
37      public Map<Object, Element> getDeletedElements() {
38          return deletedElements;
39      }
40  
41      @Override
42      public synchronized void write(Element element) throws CacheException {
43          try {
44              Thread.sleep(5000);
45          } catch (InterruptedException e) {
46              throw new CacheException(e);
47          }
48          writtenElements.put(element.getObjectKey(), element);
49      }
50  
51      @Override
52      public synchronized void writeAll(Collection<Element> elements) throws CacheException {
53          try {
54              Thread.sleep(5000);
55          } catch (InterruptedException e) {
56              throw new CacheException(e);
57          }
58          for (Element element : elements) {
59              writtenElements.put(element.getObjectKey() + "-batched", element);
60          }
61      }
62  
63      @Override
64      public synchronized void delete(CacheEntry entry) throws CacheException {
65          try {
66              Thread.sleep(5000);
67          } catch (InterruptedException e) {
68              throw new CacheException(e);
69          }
70          deletedElements.put(entry.getKey(), entry.getElement());
71      }
72  
73      @Override
74      public synchronized void deleteAll(Collection<CacheEntry> entries) throws CacheException {
75          try {
76              Thread.sleep(5000);
77          } catch (InterruptedException e) {
78              throw new CacheException(e);
79          }
80          for (CacheEntry entry : entries) {
81              deletedElements.put(entry.getKey() + "-batched", entry.getElement());
82          }
83      }
84  }