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

The following examples show how to use org.ehcache.PersistentCacheManager#close() . 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: BasicLifeCyclePassiveReplicationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDestroyCacheManager() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> configBuilder = newCacheManagerBuilder().with(cluster(CLUSTER.getConnectionURI().resolve("/destroy-CM"))
    .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManager1 = configBuilder.build(true);
  PersistentCacheManager cacheManager2 = configBuilder.build(true);

  cacheManager2.close();

  try {
    cacheManager2.destroy();
    fail("Exception expected");
  } catch (Exception e) {
    e.printStackTrace();
  }

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

  cacheManager1.createCache("test", newCacheConfigurationBuilder(Long.class, String.class, heap(10).with(clusteredDedicated(10, MB))));
}
 
Example 2
Source File: Tiering.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void threeTiersCacheManager() throws Exception {
  // tag::threeTiersCacheManager[]
  PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
    .with(CacheManagerBuilder.persistence(new File(getStoragePath(), "myData")))
    .withCache("threeTieredCache",
      CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
          .heap(10, EntryUnit.ENTRIES)
          .offheap(1, MemoryUnit.MB)
          .disk(20, MemoryUnit.MB, true)
      )
    ).build(true);
  // end::threeTiersCacheManager[]

  persistentCacheManager.close();
}
 
Example 3
Source File: Tiering.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void diskSegments() throws Exception {
  // tag::diskSegments[]
  String storagePath = getStoragePath();
  PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
    .with(CacheManagerBuilder.persistence(new File(storagePath, "myData")))
    .withCache("less-segments",
      CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.MB))
      .withService(new OffHeapDiskStoreConfiguration(2)) // <1>
    )
    .build(true);

  persistentCacheManager.close();
  // end::diskSegments[]
}
 
Example 4
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void threeTiersCacheManager() throws Exception {
  // tag::threeTiersCacheManager[]
  PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .with(CacheManagerBuilder.persistence(new File(getStoragePath(), "myData"))) // <1>
      .withCache("threeTieredCache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .heap(10, EntryUnit.ENTRIES) // <2>
                  .offheap(1, MemoryUnit.MB) // <3>
                  .disk(20, MemoryUnit.MB, true) // <4>
              )
      ).build(true);

  Cache<Long, String> threeTieredCache = persistentCacheManager.getCache("threeTieredCache", Long.class, String.class);
  threeTieredCache.put(1L, "stillAvailableAfterRestart"); // <5>

  persistentCacheManager.close();
  // end::threeTiersCacheManager[]
}
 
Example 5
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 6
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationBeforeCacheManagerCloseWithCaches() 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.close();

}
 
Example 7
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 8
Source File: CacheManagerDestroyTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDestroyCacheManagerDoesNotAffectsExistingCacheWithExistingClientsConnected() throws CachePersistenceException {

  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);
  try (PersistentCacheManager persistentCacheManager2 = cacheManagerBuilder.build(true)) {

    persistentCacheManager1.close();
    try {
      persistentCacheManager1.destroy();
      fail("StateTransitionException expected");
    } catch (StateTransitionException e) {
      assertThat(e.getMessage(), is("Couldn't acquire cluster-wide maintenance lease"));
    }

    Cache<Long, String> cache = persistentCacheManager2.getCache("test", Long.class, String.class);

    cache.put(1L, "One");

    assertThat(cache.get(1L), is("One"));
  }
}
 
Example 9
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void clusteredCacheManagerWithServerSideConfigExample() throws Exception {
  // tag::clusteredCacheManagerWithServerSideConfigExample[]
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application")).autoCreateOnReconnect(server -> server
              .defaultServerResource("primary-server-resource") // <1>
              .resourcePool("resource-pool-a", 8, MemoryUnit.MB, "secondary-server-resource") // <2>
              .resourcePool("resource-pool-b", 10, MemoryUnit.MB))) // <3>
          .withCache("clustered-cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, // <4>
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB)))) // <5>
          .withCache("shared-cache-1", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredShared("resource-pool-a")))) // <6>
          .withCache("shared-cache-2", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredShared("resource-pool-a")))); // <7>
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true); // <8>

  cacheManager.close();
  // end::clusteredCacheManagerWithServerSideConfigExample[]
}
 
Example 10
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 11
Source File: TestEhcache.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void diskManagedTest() {
    String path = "";
    PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .with(CacheManagerBuilder.persistence(path + File.separator + "myData"))
            .withCache("persistent-cache",
                    CacheConfigurationBuilder
                            .newCacheConfigurationBuilder(Long.class,
                                    String.class, ResourcePoolsBuilder.newResourcePoolsBuilder()
                                            .heap(10, EntryUnit.ENTRIES).disk(10, MemoryUnit.MB, true)))
            .build(true);

    persistentCacheManager.close();
}
 
Example 12
Source File: CacheManagerDestroyTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDestroyCacheManagerWithSingleClient() throws CachePersistenceException {
  PersistentCacheManager persistentCacheManager = clusteredCacheManagerBuilder.build(true);

  persistentCacheManager.close();
  persistentCacheManager.destroy();

  assertThat(persistentCacheManager.getStatus(), is(Status.UNINITIALIZED));
}
 
Example 13
Source File: ClusteredCacheDestroyTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDestroyCacheWithCacheManagerStopped() throws CachePersistenceException {
  PersistentCacheManager persistentCacheManager = clusteredCacheManagerBuilder.build(true);
  persistentCacheManager.close();
  persistentCacheManager.destroyCache(CLUSTERED_CACHE);
  assertThat(persistentCacheManager.getStatus(), is(Status.UNINITIALIZED));
}
 
Example 14
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void clusteredCacheTieredExample() throws Exception {
  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 {
    // tag::clusteredCacheTieredExample[]
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
            .heap(2, MemoryUnit.MB) // <1>
            .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB))) // <2>
        .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG))
        .build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache-tiered", config);
    cache.put(42L, "All you need to know!");

    // end::clusteredCacheTieredExample[]
  } finally {
    cacheManager.close();
  }
}
 
Example 15
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 16
Source File: EhcacheManagerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanDestroyAndClose() throws Exception {
  CacheConfiguration<Long, String> cacheConfiguration = new TestCacheConfig<>(Long.class, String.class);

  Store.Provider storeProvider = mock(Store.Provider.class);
  when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1);
  Store store = mock(Store.class);
  CacheEventDispatcherFactory cacheEventNotificationListenerServiceProvider = mock(CacheEventDispatcherFactory.class);

  when(storeProvider.createStore(any(Store.Configuration.class), ArgumentMatchers.<ServiceConfiguration>any())).thenReturn(store);
  when(store.getConfigurationChangeListeners()).thenReturn(new ArrayList<>());
  when(cacheEventNotificationListenerServiceProvider.createCacheEventDispatcher(store)).thenReturn(mock(CacheEventDispatcher.class));

  Map<String, CacheConfiguration<?, ?>> caches = newCacheMap();
  caches.put("aCache", cacheConfiguration);
  DefaultConfiguration config = new DefaultConfiguration(caches, null);
  PersistentCacheManager cacheManager = new EhcacheManager(config, Arrays.asList(
      storeProvider,
      mock(CacheLoaderWriterProvider.class),
      mock(WriteBehindProvider.class),
      cacheEventNotificationListenerServiceProvider,
      mock(CacheEventListenerProvider.class),
      mock(LocalPersistenceService.class),
      mock(ResilienceStrategyProvider.class)));
  cacheManager.init();

  cacheManager.close();
  cacheManager.init();
  cacheManager.close();
  cacheManager.destroy();
  cacheManager.init();
  cacheManager.close();
}
 
Example 17
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 18
Source File: PersistentCacheManagerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDestroyCache_Uninitialized_DestroyExistingCache() throws CachePersistenceException {
  PersistentCacheManager manager = buildCacheManagerWithCache(true);

  manager.close();
  manager.destroyCache(TEST_CACHE_ALIAS);

  assertThat(rootDirectory, not(isLocked()));
  assertThat(rootDirectory, not(containsCacheDirectory(TEST_CACHE_ALIAS)));
}
 
Example 19
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 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[]
  }