Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MemoryMessageIdRepository |
|
| 1.4;1.4 |
1 | /** |
|
2 | * |
|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 | * contributor license agreements. See the NOTICE file distributed with |
|
5 | * this work for additional information regarding copyright ownership. |
|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 | * (the "License"); you may not use this file except in compliance with |
|
8 | * the License. You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | package org.apache.camel.processor.idempotent; |
|
19 | ||
20 | import org.apache.camel.util.LRUCache; |
|
21 | ||
22 | import java.util.Set; |
|
23 | import java.util.HashSet; |
|
24 | import java.util.Map; |
|
25 | import java.util.HashMap; |
|
26 | import java.util.LinkedHashMap; |
|
27 | ||
28 | /** |
|
29 | * A memory based implementation of {@link MessageIdRepository}. |
|
30 | * Care should be taken to use a suitable underlying {@link Map} to avoid this class being a memory leak |
|
31 | * |
|
32 | * @version $Revision: 1.1 $ |
|
33 | */ |
|
34 | public class MemoryMessageIdRepository implements MessageIdRepository { |
|
35 | private Map cache; |
|
36 | ||
37 | /** |
|
38 | * Creates a new MemoryMessageIdRepository with a memory based respository. <b>Warning</b> this |
|
39 | * method should only really be used for testing as it will involve keeping all message IDs in RAM. |
|
40 | */ |
|
41 | public static MessageIdRepository memoryMessageIdRepository() { |
|
42 | 0 | return memoryMessageIdRepository(new HashMap()); |
43 | } |
|
44 | ||
45 | /** |
|
46 | * Creates a new MemoryMessageIdRepository with a memory based respository. <b>Warning</b> this |
|
47 | * method should only really be used for testing as it will involve keeping all message IDs in RAM. |
|
48 | */ |
|
49 | public static MessageIdRepository memoryMessageIdRepository(int cacheSize) { |
|
50 | 2 | return memoryMessageIdRepository(new LRUCache(cacheSize)); |
51 | } |
|
52 | ||
53 | /** |
|
54 | * Creates a new MemoryMessageIdRepository using the given {@link Map} to use to store the |
|
55 | * processed Message ID objects. Warning be cafeful of the implementation of Map you use as |
|
56 | * if you are not careful it could be a memory leak. |
|
57 | */ |
|
58 | public static MessageIdRepository memoryMessageIdRepository(Map cache) { |
|
59 | 2 | return new MemoryMessageIdRepository(cache); |
60 | } |
|
61 | ||
62 | 2 | public MemoryMessageIdRepository(Map set) { |
63 | 2 | this.cache = set; |
64 | 2 | } |
65 | ||
66 | public boolean contains(String messageId) { |
|
67 | 6 | synchronized (cache) { |
68 | 6 | if (cache.containsKey(messageId)) { |
69 | 3 | return true; |
70 | } |
|
71 | else { |
|
72 | 3 | cache.put(messageId, messageId); |
73 | 3 | return false; |
74 | } |
|
75 | 0 | } |
76 | } |
|
77 | } |