Java Code Examples for org.ehcache.PersistentCacheManager#init()

The following examples show how to use org.ehcache.PersistentCacheManager#init() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void clusteredCacheManagerWithDynamicallyAddedCacheExample() throws Exception {
  // tag::clusteredCacheManagerWithDynamicallyAddedCacheExample[]
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
          = CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
                  .autoCreateOnReconnect(server -> server.defaultServerResource("primary-server-resource")
                    .resourcePool("resource-pool-a", 8, MemoryUnit.MB)));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                    .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))).build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);

  } finally {
    cacheManager.close();
  }
  // end::clusteredCacheManagerWithDynamicallyAddedCacheExample[]
}
 
Example 2
Source File: CacheManagerDestroyTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseCacheManagerSingleClient() {
  CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilder = clusteredCacheManagerBuilder
      .withCache("test", newCacheConfigurationBuilder(Long.class, String.class,
          ResourcePoolsBuilder.newResourcePoolsBuilder()
              .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))));

  PersistentCacheManager persistentCacheManager1 = cacheManagerBuilder.build(true);

  persistentCacheManager1.close();

  persistentCacheManager1.init();

  Cache<Long, String> cache = persistentCacheManager1.getCache("test", Long.class, String.class);
  cache.put(1L, "One");

  assertThat(cache.get(1L), is("One"));

  persistentCacheManager1.close();
}
 
Example 3
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if {@link CacheManager#close()} blocks if the client/server connection is disconnected.
 */
@Test
public void testTerminationBeforeCacheManagerClose() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
          .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  CLUSTER.get().getClusterControl().terminateAllServers();

  new TimeLimitedTask<Void>(CLUSTER.input().plusSeconds(10)) {
    @Override
    Void runTask() throws Exception {
      cacheManager.close();
      return null;
    }
  }.run();

  // TODO: Add assertion for successful CacheManager.init() following cluster restart (https://github.com/Terracotta-OSS/galvan/issues/30)
}
 
Example 4
Source File: StatefulSerializerWithStateRepositoryTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testStatefulSerializerWithDiskStateRepository() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> cmBuilder = newCacheManagerBuilder().with(persistence(temporaryFolder.newFolder()
      .getAbsolutePath()))
      .withCache("myCache", newCacheConfigurationBuilder(Long.class, Person.class, heap(10).disk(50, MemoryUnit.MB, true))
          .withValueSerializer(CompactJavaSerializer.asTypedSerializer()));
  PersistentCacheManager cacheManager = cmBuilder.build(true);

  Cache<Long, Person> myCache = cacheManager.getCache("myCache", Long.class, Person.class);

  myCache.put(42L, new Person("John", 42));
  myCache.put(35L, new Person("Marie", 35));

  cacheManager.close();

  cacheManager.init();

  myCache = cacheManager.getCache("myCache", Long.class, Person.class);

  assertThat(myCache.get(42L).getName(), is("John"));
}
 
Example 5
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("There are no timeout on the create cache right now. It waits until the server comes back")
public void testTerminationBeforeCacheCreate() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  CLUSTER.get().getClusterControl().terminateAllServers();

  assertExceptionOccurred(IllegalStateException.class,
    new TimeLimitedTask<Cache<Long, String>>(ofSeconds(10)) {
      @Override
      Cache<Long, String> runTask() throws Exception {
        return cacheManager.createCache("simple-cache",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                ResourcePoolsBuilder.newResourcePoolsBuilder()
                    .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
      }
    })
    .withRootCauseInstanceOf(TimeoutException.class);
}
 
Example 6
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationBeforeCacheRemove() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
          .withCache("simple-cache",
              CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                  ResourcePoolsBuilder.newResourcePoolsBuilder()
                      .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  CLUSTER.get().getClusterControl().terminateAllServers();

  cacheManager.removeCache("simple-cache");
}
 
Example 7
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("Works but by sending a really low level exception. Need to be fixed to get the expected CachePersistenceException")
public void testTerminationBeforeCacheManagerDestroyCache() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
          .withCache("simple-cache",
              CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                  ResourcePoolsBuilder.newResourcePoolsBuilder()
                      .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  cacheManager.removeCache("simple-cache");

  CLUSTER.get().getClusterControl().terminateAllServers();

  assertExceptionOccurred(CachePersistenceException.class,
    new TimeLimitedTask<Void>(ofSeconds(10)) {
      @Override
      Void runTask() throws Exception {
        cacheManager.destroyCache("simple-cache");
        return null;
      }
    });
}
 
Example 8
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenClear() throws Exception {
  StatisticsService statisticsService = new DefaultStatisticsService();
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .using(statisticsService)
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .timeouts(TimeoutsBuilder.timeouts().write(Duration.of(1, ChronoUnit.SECONDS)).build())
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  CLUSTER.get().getClusterControl().terminateAllServers();

  // The resilience strategy will pick it up and not exception is thrown
  new TimeLimitedTask<Void>(ofSeconds(10)) {
      @Override
      Void runTask() {
        cache.clear();
        return null;
      }
    }.run();
}
 
Example 9
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenRemove() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .timeouts(TimeoutsBuilder.timeouts().write(Duration.of(1, ChronoUnit.SECONDS)).build())
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  CLUSTER.get().getClusterControl().terminateAllServers();

  new TimeLimitedTask<Void>(ofSeconds(10)) {
    @Override
    Void runTask() throws Exception {
      cache.remove(2L);
      return null;
    }
  }.run();
}
 
Example 10
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenPutIfAbsent() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .timeouts(TimeoutsBuilder.timeouts().write(Duration.of(1, ChronoUnit.SECONDS)).build())
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  CLUSTER.get().getClusterControl().terminateAllServers();

  // The resilience strategy will pick it up and not exception is thrown
  new TimeLimitedTask<String>(ofSeconds(10)) {
    @Override
    String runTask() throws Exception {
      return cache.putIfAbsent(2L, "dos");
    }
  }.run();
}
 
Example 11
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenPut() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .timeouts(TimeoutsBuilder.timeouts().write(Duration.of(1, ChronoUnit.SECONDS)).build())
                  .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  CLUSTER.get().getClusterControl().terminateAllServers();

  // The resilience strategy will pick it up and not exception is thrown
  new TimeLimitedTask<Void>(ofSeconds(10)) {
    @Override
    Void runTask() throws Exception {
      cache.put(2L, "dos");
      return null;
    }
  }.run();
}
 
Example 12
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Ignore("ClusteredStore.iterator() is not implemented")
@Test
public void testTerminationThenIterator() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
            .timeouts(TimeoutsBuilder.timeouts().read(Duration.of(1, ChronoUnit.SECONDS)).build())
              .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  CLUSTER.get().getClusterControl().terminateAllServers();

  Iterator<Cache.Entry<Long, String>> value = new TimeLimitedTask<Iterator<Cache.Entry<Long,String>>>(ofSeconds(5)) {
    @Override
    Iterator<Cache.Entry<Long, String>> runTask() {
      return cache.iterator();
    }
  }.run();

  assertThat(value.hasNext()).isFalse();
}
 
Example 13
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenContainsKey() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
              .timeouts(TimeoutsBuilder.timeouts().read(Duration.of(1, ChronoUnit.SECONDS)).build())
              .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  assertThat(cache.containsKey(2L)).isTrue();

  CLUSTER.get().getClusterControl().terminateAllServers();

  boolean value = new TimeLimitedTask<Boolean>(ofSeconds(5)) {
    @Override
    Boolean runTask() throws Exception {
      return cache.containsKey(2L);
    }
  }.run();

  assertThat(value).isFalse();
}
 
Example 14
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenGet() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
              .timeouts(TimeoutsBuilder.timeouts().read(Duration.of(1, ChronoUnit.SECONDS)).build())
              .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  assertThat(cache.get(2L)).isNotNull();

  CLUSTER.get().getClusterControl().terminateAllServers();

  String value = new TimeLimitedTask<String>(ofSeconds(5)) {
    @Override
    String runTask() throws Exception {
      return cache.get(2L);
    }
  }.run();

  assertThat(value).isNull();
}
 
Example 15
Source File: LeaseTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void leaseExpiry() throws Exception {
  URI connectionURI = TCPProxyUtil.getProxyURI(CLUSTER.get().getConnectionURI(), proxies);

  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(connectionURI.resolve("/crud-cm"))
      .timeouts(TimeoutsBuilder.timeouts().connection(Duration.ofSeconds(20)))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
    resourcePoolsBuilder).build();

  Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);
  cache.put(1L, "The one");
  cache.put(2L, "The two");
  cache.put(3L, "The three");
  assertThat(cache.get(1L), equalTo("The one"));
  assertThat(cache.get(2L), equalTo("The two"));
  assertThat(cache.get(3L), equalTo("The three"));

  long delay = CLUSTER.input().plusSeconds(1L).toMillis();
  setDelay(delay, proxies);
  try {
    Thread.sleep(delay);
  } finally {
    setDelay(0L, proxies);
  }

  assertThat(() -> cache.get(1L), within(Duration.ofSeconds(60)).is("The one"));
  assertThat(cache.get(2L), equalTo("The two"));
  assertThat(cache.get(3L), equalTo("The three"));

}
 
Example 16
Source File: CacheManagerDestroyTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDestroyCreate() throws Exception {
  PersistentCacheManager cacheManager = newCacheManagerBuilder().with(cluster(CLUSTER_URI)
    .autoCreate(c -> c.defaultServerResource("primary-server-resource")))
    .withCache("my-cache", newCacheConfigurationBuilder(Long.class, String.class, heap(10).with(ClusteredResourcePoolBuilder
      .clusteredDedicated(2, MemoryUnit.MB))))
    .build(true);

  cacheManager.close();
  cacheManager.destroy();

  cacheManager.init();

  cacheManager.close();
}
 
Example 17
Source File: CacheManagerLifecycleEhcacheIntegrationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCreatedCacheManagerUsingXml() throws Exception {
  URL xml = CacheManagerLifecycleEhcacheIntegrationTest.class.getResource("/configs/clustered.xml");
  URL substitutedXml = substitute(xml, "cluster-uri", CLUSTER.getConnectionURI().toString());
  PersistentCacheManager manager = (PersistentCacheManager) newCacheManager(new XmlConfiguration(substitutedXml));
  assertEntityNotExists(ClusterTierManagerClientEntity.class, "testAutoCreatedCacheManagerUsingXml");
  manager.init();
  try {
    assertEntityExists(ClusterTierManagerClientEntity.class, "testAutoCreatedCacheManagerUsingXml");
  } finally {
    manager.close();
  }
}
 
Example 18
Source File: CacheManagerLifecycleEhcacheIntegrationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCreatedCacheManager() throws Exception {
  assertEntityNotExists(ClusterTierManagerClientEntity.class, "testAutoCreatedCacheManager");
  PersistentCacheManager manager = newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/testAutoCreatedCacheManager")).autoCreate(c -> c).build())
          .build();
  assertEntityNotExists(ClusterTierManagerClientEntity.class, "testAutoCreatedCacheManager");
  manager.init();
  try {
    assertEntityExists(ClusterTierManagerClientEntity.class, "testAutoCreatedCacheManager");
  } finally {
    manager.close();
  }

}
 
Example 19
Source File: PassiveSyncTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 150000)
public void testSync() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
    = CacheManagerBuilder.newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/op-sync"))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  final PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 1, MemoryUnit.MB))).build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);

    for (long i = -5; i < 5; i++) {
      cache.put(i, "value" + i);
    }

    CLUSTER.getClusterControl().startOneServer();
    CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
    CLUSTER.getClusterControl().terminateActive();


    assertThat(() -> cache.get(0L), within(Duration.ofSeconds(130)).matches(notNullValue()));
    for (long i = -5; i < 5; i++) {
      assertThat(cache.get(i), equalTo("value" + i));
    }
  } finally {
    cacheManager.close();
  }
}
 
Example 20
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void unknownClusteredCacheExample()
{
  // tag::unspecifiedClusteredCacheExample[]

  CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilderAutoCreate = CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
            .autoCreateOnReconnect(server -> server  // <1>
              .resourcePool("resource-pool", 8, MemoryUnit.MB, "primary-server-resource")));

  PersistentCacheManager cacheManager1 = cacheManagerBuilderAutoCreate.build(false);
  cacheManager1.init();
  try {
    CacheConfiguration<Long, String> cacheConfigDedicated = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB)))  // <2>
      .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG))
      .build();

    Cache<Long, String> cacheDedicated = cacheManager1.createCache("my-dedicated-cache", cacheConfigDedicated);  // <3>

    CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilderExpecting = CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
        .expecting(server -> server  // <4>
          .resourcePool("resource-pool", 8, MemoryUnit.MB, "primary-server-resource")));

    PersistentCacheManager cacheManager2 = cacheManagerBuilderExpecting.build(false);
    cacheManager2.init();
    try {
      CacheConfiguration<Long, String> cacheConfigUnspecified = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
          .with(ClusteredResourcePoolBuilder.clustered()))  // <5>
        .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG))
        .build();

      Cache<Long, String> cacheUnspecified = cacheManager2.createCache("my-dedicated-cache", cacheConfigUnspecified); // <6>
    } finally {
      cacheManager2.close();
    }
  } finally {
    cacheManager1.close();
  }
  // end::unspecifiedClusteredCacheExample[]
  }