@Before
  public void init() {
    CacheConfiguration<Long, String> cacheConfiguration =
        CacheConfigurationBuilder.newCacheConfigurationBuilder()
            .withResourcePools(
                ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
            .buildConfig(Long.class, String.class);

    service = new DefaultSharedManagementService();

    cacheManager1 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache1", cacheConfiguration)
            .using(service)
            .using(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM1"))
            .build(true);

    cacheManager2 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache2", cacheConfiguration)
            .withCache("aCache3", cacheConfiguration)
            .using(service)
            .using(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM2"))
            .build(true);

    // this serie of calls make sure the registry still works after a full init / close / init loop
    cacheManager1.close();
    cacheManager1.init();
    cacheManager2.close();
    cacheManager2.init();
  }
Ejemplo n.º 2
0
  @Test
  public void testStoreByValue() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);
    cacheManager.init();

    DefaultCopierConfiguration<String> copierConfiguration =
        new DefaultCopierConfiguration(SerializingCopier.class, CopierConfiguration.Type.VALUE);
    final Cache<Long, String> cache1 =
        cacheManager.createCache(
            "cache1",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .withResourcePools(
                    ResourcePoolsBuilder.newResourcePoolsBuilder().heap(1, EntryUnit.ENTRIES))
                .build());
    performAssertions(cache1, true);

    final Cache<Long, String> cache2 =
        cacheManager.createCache(
            "cache2",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .add(copierConfiguration)
                .build());
    performAssertions(cache2, false);

    final Cache<Long, String> cache3 =
        cacheManager.createCache(
            "cache3",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .build());
    performAssertions(cache3, true);

    cacheManager.close();
  }
  @Test
  public void testCopiers() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/cache-copiers.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();

    Cache<Description, Person> bar = cacheManager.getCache("bar", Description.class, Person.class);
    Description desc = new Description(1234, "foo");
    Person person = new Person("Bar", 24);
    bar.put(desc, person);
    assertEquals(person, bar.get(desc));
    assertNotSame(person, bar.get(desc));

    Cache<Long, Person> baz = cacheManager.getCache("baz", Long.class, Person.class);
    baz.put(1L, person);
    assertEquals(person, baz.get(1L));
    assertNotSame(person, baz.get(1L));

    Employee empl = new Employee(1234, "foo", 23);
    Cache<Long, Employee> bak = cacheManager.getCache("bak", Long.class, Employee.class);
    bak.put(1L, empl);
    assertSame(empl, bak.get(1L));
    cacheManager.close();
  }
  @Test
  public void testThreadPoolsUsingDefaultPool() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    try {
      Cache<String, String> cache =
          cacheManager.createCache(
              "testThreadPools",
              newCacheConfigurationBuilder()
                  .add(
                      new DefaultCacheLoaderWriterConfiguration(
                          ThreadRememberingLoaderWriter.class))
                  .add(newUnBatchedWriteBehindConfiguration())
                  .buildConfig(String.class, String.class));

      cache.put("foo", "bar");

      ThreadRememberingLoaderWriter.USED.acquireUninterruptibly();

      assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[big]"));
    } finally {
      cacheManager.close();
    }
  }
 @Test
 public void testCacheEventListener() throws Exception {
   Configuration configuration =
       new XmlConfiguration(
           this.getClass().getResource("/configs/ehcache-cacheEventListener.xml"));
   assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true));
   final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
   cacheManager.init();
   final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class);
   cache.put(10, "dog");
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.CREATED));
   cache.put(10, "cat");
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.UPDATED));
   cache.remove(10);
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.REMOVED));
   cache.put(10, "dog");
   resetValues();
   assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true));
   final Cache<Number, String> templateCache =
       cacheManager.getCache("template1", Number.class, String.class);
   templateCache.put(10, "cat");
   assertThat(TestCacheEventListener.FIRED_EVENT, nullValue());
   templateCache.put(10, "dog");
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.UPDATED));
 }
  @Test
  public void testWriteBehind()
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, SAXException,
          IOException, InterruptedException {

    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/writebehind-cache.xml"));
    assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class);
    assertThat(cache, notNullValue());
    assertThat(cache.get(1), notNullValue());
    final Number key = 42L;
    TestCacheLoaderWriter.latch = new CountDownLatch(1);
    cache.put(key, "Bye y'all!");
    TestCacheLoaderWriter.latch.await(2, TimeUnit.SECONDS);
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key));

    assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true));
    final Cache<Number, String> templateCache =
        cacheManager.getCache("template1", Number.class, String.class);
    assertThat(templateCache, notNullValue());
    assertThat(templateCache.get(1), notNullValue());
    final Number key1 = 100L;
    TestCacheLoaderWriter.latch = new CountDownLatch(2);
    templateCache.put(42L, "Howdy!");
    templateCache.put(key1, "Bye y'all!");
    TestCacheLoaderWriter.latch.await(2, TimeUnit.SECONDS);
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key1));
  }
  @Test
  public void testLoaderWriter()
      throws ClassNotFoundException, SAXException, InstantiationException, IOException,
          IllegalAccessException {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/cache-integration.xml"));
    assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class);
    assertThat(cache, notNullValue());
    assertThat(cache.get(1), notNullValue());
    final Number key = new Long(42);
    cache.put(key, "Bye y'all!");
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key));

    assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true));
    final Cache<Number, String> templateCache =
        cacheManager.getCache("template1", Number.class, String.class);
    assertThat(templateCache, notNullValue());
    assertThat(templateCache.get(1), notNullValue());
    final Number key1 = new Long(100);
    templateCache.put(key1, "Bye y'all!");
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key1));
  }
  @Test
  public void testSerializers() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/default-serializer.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();

    Cache<Long, Double> bar = cacheManager.getCache("bar", Long.class, Double.class);
    bar.put(1L, 1.0);
    assertThat(bar.get(1L), equalTo(1.0));

    Cache<String, String> baz = cacheManager.getCache("baz", String.class, String.class);
    baz.put("1", "one");
    assertThat(baz.get("1"), equalTo("one"));

    Cache<String, Object> bam =
        cacheManager.createCache(
            "bam", newCacheConfigurationBuilder().buildConfig(String.class, Object.class));
    bam.put("1", "one");
    assertThat(bam.get("1"), equalTo((Object) "one"));

    cacheManager.close();
  }
Ejemplo n.º 9
0
  public EHCache3Manager()
      throws ClassNotFoundException, SAXException, InstantiationException, IOException,
          IllegalAccessException {
    URL url = getClass().getResource("/activejdbc-ehcache.xml");
    if (url == null) {
      throw new InitException(
          "You are using "
              + getClass().getName()
              + " but failed to provide a EHCache configuration file on classpath: activejdbc-ehcache.xml");
    }

    XmlConfiguration xmlConfiguration = new XmlConfiguration(url);

    cacheTemplate =
        xmlConfiguration.newCacheConfigurationBuilderFromTemplate(
            "activejdbc", String.class, Object.class);

    if (cacheTemplate == null) {
      throw new InitException(
          "Please, provide a <cache-template name=\"activejdbc\"> element in  activejdbc-ehcache.xml file");
    }
    cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
    cacheManager.init();
  }