View Javadoc

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