View Javadoc

1   package com.atlassian.cache.ehcache.replication.rmi;
2   
3   import javax.annotation.Nonnull;
4   import javax.annotation.ParametersAreNonnullByDefault;
5   
6   import com.atlassian.cache.CacheSettings;
7   import com.atlassian.cache.ehcache.replication.EhCacheReplicatorConfigFactory;
8   
9   import net.sf.ehcache.config.CacheConfiguration;
10  
11  /**
12   * An implementation of {@link EhCacheReplicatorConfigFactory} that configures RMI replication.
13   *
14   * @since 2.6.0
15   */
16  @ParametersAreNonnullByDefault
17  public class RMICacheReplicatorConfigFactory implements EhCacheReplicatorConfigFactory
18  {
19      // These are all the flags supported by RMICacheReplicatorFactory
20      private static final String CACHE_PROPERTIES =
21              "replicateAsynchronously=%s," +
22                      "replicatePuts=%s," +
23                      "replicatePutsViaCopy=%s," +
24                      "replicateUpdates=%s," +
25                      "replicateUpdatesViaCopy=%s," +
26                      "replicateRemovals=true";
27  
28      @Override
29      @Nonnull
30      public CacheConfiguration.CacheEventListenerFactoryConfiguration createCacheReplicatorConfiguration(final CacheSettings settings, final boolean selfLoadingCache)
31      {
32          final boolean replicateAsynchronously = settings.getReplicateAsynchronously(false);
33          final boolean replicateViaCopy = settings.getReplicateViaCopy(false);
34  
35          // If the cache is self loading there is no need to replicate puts or updates as the loader takes care of
36          // populating the cache as needed.
37          // Else if the cache is replicating via copy then puts and updates should be replicated.
38          // Else replicating puts is dangerous as it may lead to invalidations ping ponging between nodes. Whether to
39          // replicate updates is ambiguous, but setting it may help to prevent stale caches while being less vulnerable
40          // to the invalidation storm that comes with simply replicating puts.
41          final boolean replicatePuts = !selfLoadingCache && replicateViaCopy;
42          final boolean replicateUpdates = !selfLoadingCache;
43  
44          final String cacheProperties =
45                  String.format(CACHE_PROPERTIES, replicateAsynchronously, replicatePuts, replicateViaCopy, replicateUpdates, replicateViaCopy);
46          return new CacheConfiguration.CacheEventListenerFactoryConfiguration()
47                  .className(RMICacheReplicatorFactory.class.getName())
48                  .properties(cacheProperties);
49      }
50  }