View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import java.io.Serializable;
4   
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import net.sf.ehcache.CacheException;
9   import net.sf.ehcache.Ehcache;
10  import net.sf.ehcache.Element;
11  
12  /**
13   * An {@link RMISynchronousCacheReplicator} that works around https://jira.terracotta.org/jira/browse/EHC-683.
14   *
15   * @since 2.0
16   */
17  public class RMISynchronousCacheReplicator extends net.sf.ehcache.distribution.RMISynchronousCacheReplicator
18  {
19      // Log as the superclass to keep all replication messages within the same logger name
20      private static final Logger LOG =
21              LoggerFactory.getLogger(net.sf.ehcache.distribution.RMISynchronousCacheReplicator.class);
22  
23      /**
24       * Constructor
25       *
26       * @param replicatePuts whether to replicate puts
27       * @param replicatePutsViaCopy whether to replicate puts by copying the serialized value
28       * @param replicateUpdates whether to replicate updates
29       * @param replicateUpdatesViaCopy whether to replicate updates by copying the serialized value
30       * @param replicateRemovals whether to replicate removals
31       */
32      public RMISynchronousCacheReplicator(final boolean replicatePuts, final boolean replicatePutsViaCopy,
33              final boolean replicateUpdates, final boolean replicateUpdatesViaCopy, final boolean replicateRemovals)
34      {
35          super(replicatePuts, replicatePutsViaCopy, replicateUpdates, replicateUpdatesViaCopy, replicateRemovals);
36      }
37  
38      // Overridden to fix https://jira.terracotta.org/jira/browse/EHC-683
39      @Override
40      public void notifyElementPut(final Ehcache cache, final Element element) throws CacheException {
41          if (notAlive() || !replicatePuts) {
42              return;
43          }
44  
45          if (replicatePutsViaCopy) {
46              replicateViaCopy(cache, element);
47          }
48          else {
49              replicateViaKeyInvalidation(cache, element);
50          }
51      }
52  
53      private void replicateViaCopy(final Ehcache cache, final Element element)
54      {
55          if (element.isSerializable()) {
56              replicatePutNotification(cache, element);
57              return;
58          }
59          if (!element.isKeySerializable()) {
60              logUnserializableKey(element);
61          }
62          if (LOG.isWarnEnabled() && !(element.getObjectValue() instanceof Serializable)) {
63              LOG.error("Value class {} is not Serializable => cannot be replicated",
64                      element.getObjectValue().getClass().getName());
65          }
66      }
67  
68      private void replicateViaKeyInvalidation(final Ehcache cache, final Element element)
69      {
70          if (element.isKeySerializable()) {
71              replicateRemovalNotification(cache, (Serializable) element.getObjectKey());
72              return;
73          }
74          logUnserializableKey(element);
75      }
76  
77      private void logUnserializableKey(final Element element)
78      {
79          if (LOG.isWarnEnabled()) {
80              LOG.error("Key class {} is not Serializable => cannot be replicated",
81                      element.getObjectKey().getClass().getName());
82          }
83      }
84  }