View Javadoc

1   package com.atlassian.cache.ehcache.replication.rmi;
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          {
43              return;
44          }
45  
46          if (replicatePutsViaCopy)
47          {
48              replicateViaCopy(cache, element);
49          }
50          else
51          {
52              replicateViaKeyInvalidation(cache, element);
53          }
54      }
55  
56      private void replicateViaCopy(final Ehcache cache, final Element element)
57      {
58          if (element.isSerializable())
59          {
60              replicatePutNotification(cache, element);
61              return;
62          }
63          if (!element.isKeySerializable())
64          {
65              logUnserializableKey(element);
66          }
67          if (LOG.isWarnEnabled() && !(element.getObjectValue() instanceof Serializable))
68          {
69              LOG.error("Value class {} is not Serializable => cannot be replicated",
70                      element.getObjectValue().getClass().getName());
71          }
72      }
73  
74      private void replicateViaKeyInvalidation(final Ehcache cache, final Element element)
75      {
76          if (element.isKeySerializable())
77          {
78              replicateRemovalNotification(cache, (Serializable) element.getObjectKey());
79              return;
80          }
81          logUnserializableKey(element);
82      }
83  
84      private void logUnserializableKey(final Element element)
85      {
86          if (LOG.isWarnEnabled())
87          {
88              LOG.error("Key class {} is not Serializable => cannot be replicated",
89                      element.getObjectKey().getClass().getName());
90          }
91      }
92  }