1 package com.atlassian.cache.ehcache;
2
3 import java.util.Properties;
4
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 import net.sf.ehcache.distribution.CacheReplicator;
9 import net.sf.ehcache.distribution.RMIAsynchronousCacheReplicator;
10 import net.sf.ehcache.event.CacheEventListener;
11 import net.sf.ehcache.event.CacheEventListenerFactory;
12 import net.sf.ehcache.util.PropertyUtil;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class RMICacheReplicatorFactory extends CacheEventListenerFactory {
32
33
34
35
36
37 protected static final int DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS = 1000;
38
39
40
41
42 protected static final int DEFAULT_ASYNCHRONOUS_REPLICATION_MAXIMUM_BATCH_SIZE = 1000;
43
44 private static final Logger LOG = LoggerFactory.getLogger(RMICacheReplicatorFactory.class.getName());
45 private static final String REPLICATE_PUTS = "replicatePuts";
46 private static final String REPLICATE_PUTS_VIA_COPY = "replicatePutsViaCopy";
47 private static final String REPLICATE_UPDATES = "replicateUpdates";
48 private static final String REPLICATE_UPDATES_VIA_COPY = "replicateUpdatesViaCopy";
49 private static final String REPLICATE_REMOVALS = "replicateRemovals";
50 static final String REPLICATE_ASYNCHRONOUSLY = "replicateAsynchronously";
51 private static final String ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS = "asynchronousReplicationIntervalMillis";
52 private static final String ASYNCHRONOUS_REPLICATION_MAXIMUM_BATCH_SIZE = "asynchronousReplicationMaximumBatchSize";
53 private static final int MINIMUM_REASONABLE_INTERVAL = 10;
54
55 private static boolean extractBoolean(final String key, final Properties properties)
56 {
57 String booleanString = PropertyUtil.extractAndLogProperty(key, properties);
58 return booleanString == null || PropertyUtil.parseBoolean(booleanString);
59 }
60
61
62
63
64
65
66
67 private static int extractReplicationIntervalMilis(Properties properties)
68 {
69 int asynchronousReplicationIntervalMillis;
70 String asynchronousReplicationIntervalMillisString =
71 PropertyUtil.extractAndLogProperty(ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS, properties);
72 if (asynchronousReplicationIntervalMillisString != null)
73 {
74 try
75 {
76 int asynchronousReplicationIntervalMillisCandidate =
77 Integer.parseInt(asynchronousReplicationIntervalMillisString);
78 if (asynchronousReplicationIntervalMillisCandidate < MINIMUM_REASONABLE_INTERVAL)
79 {
80 LOG.debug("Trying to set the asynchronousReplicationIntervalMillis to an unreasonable number." +
81 " Using the default instead.");
82 asynchronousReplicationIntervalMillis = DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS;
83 } else {
84 asynchronousReplicationIntervalMillis = asynchronousReplicationIntervalMillisCandidate;
85 }
86 }
87 catch (NumberFormatException e)
88 {
89 LOG.warn("Number format exception trying to set asynchronousReplicationIntervalMillis. " +
90 "Using the default instead. String value was: '" + asynchronousReplicationIntervalMillisString + "'");
91 asynchronousReplicationIntervalMillis = DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS;
92 }
93 }
94 else
95 {
96 asynchronousReplicationIntervalMillis = DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS;
97 }
98 return asynchronousReplicationIntervalMillis;
99 }
100
101
102
103
104
105
106
107 private static int extractMaximumBatchSize(final Properties properties)
108 {
109 String maximumBatchSizeString =
110 PropertyUtil.extractAndLogProperty(ASYNCHRONOUS_REPLICATION_MAXIMUM_BATCH_SIZE, properties);
111 if (maximumBatchSizeString == null)
112 {
113 return DEFAULT_ASYNCHRONOUS_REPLICATION_MAXIMUM_BATCH_SIZE;
114 }
115 else
116 {
117 try
118 {
119 return Integer.parseInt(maximumBatchSizeString);
120 }
121 catch (NumberFormatException e)
122 {
123 LOG.warn("Number format exception trying to set maximumBatchSize. " +
124 "Using the default instead. String value was: '" + maximumBatchSizeString + "'");
125 return DEFAULT_ASYNCHRONOUS_REPLICATION_MAXIMUM_BATCH_SIZE;
126 }
127 }
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public CacheEventListener createCacheEventListener(final Properties properties)
160 {
161 final boolean replicatePuts = extractBoolean(REPLICATE_PUTS, properties);
162 final boolean replicatePutsViaCopy = extractBoolean(REPLICATE_PUTS_VIA_COPY, properties);
163 final boolean replicateUpdates = extractBoolean(REPLICATE_UPDATES, properties);
164 final boolean replicateUpdatesViaCopy = extractBoolean(REPLICATE_UPDATES_VIA_COPY, properties);
165 final boolean replicateRemovals = extractBoolean(REPLICATE_REMOVALS, properties);
166 final boolean replicateAsynchronously = extractBoolean(REPLICATE_ASYNCHRONOUSLY, properties);
167 final int replicationIntervalMillis = extractReplicationIntervalMilis(properties);
168 final int maximumBatchSize = extractMaximumBatchSize(properties);
169
170 if (replicateAsynchronously)
171 {
172 return new RMIAsynchronousCacheReplicator(
173 replicatePuts,
174 replicatePutsViaCopy,
175 replicateUpdates,
176 replicateUpdatesViaCopy,
177 replicateRemovals,
178 replicationIntervalMillis,
179 maximumBatchSize);
180 }
181
182 return new RMISynchronousCacheReplicator(
183 replicatePuts,
184 replicatePutsViaCopy,
185 replicateUpdates,
186 replicateUpdatesViaCopy,
187 replicateRemovals);
188 }
189 }