Java Code Examples for javax.cache.configuration.FactoryBuilder#factoryOf()

The following examples show how to use javax.cache.configuration.FactoryBuilder#factoryOf() . 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: TypesafeConfigurator.java    From caffeine with Apache License 2.0 6 votes vote down vote up
/** Adds the JCache specification's lazy expiration settings. */
public void addLazyExpiration() {
  Duration creation = getDurationFor("policy.lazy-expiration.creation");
  Duration update = getDurationFor("policy.lazy-expiration.update");
  Duration access = getDurationFor("policy.lazy-expiration.access");
  requireNonNull(creation, "policy.lazy-expiration.creation may not be null");

  boolean eternal = Objects.equals(creation, Duration.ETERNAL)
      && Objects.equals(update, Duration.ETERNAL)
      && Objects.equals(access, Duration.ETERNAL);
  @SuppressWarnings("NullAway")
  Factory<? extends ExpiryPolicy> factory = eternal
      ? EternalExpiryPolicy.factoryOf()
      : FactoryBuilder.factoryOf(new JCacheExpiryPolicy(creation, update, access));
  configuration.setExpiryPolicyFactory(factory);
}
 
Example 2
Source File: AdditionalCacheListenerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Override
protected MutableConfiguration<Integer, String> extraSetup(MutableConfiguration<Integer, String> cfg) {
  // cfg.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, 5)));
  cacheEntryListenerServer = new CacheEntryListenerServer<>(10011, Integer.class, String.class);
  try {
    cacheEntryListenerServer.open();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  listener = new RecordingListener<>();
  cacheEntryListenerServer.addCacheEventListener(listener);
  CacheEntryListenerClient<Integer, String> clientListener =
    new CacheEntryListenerClient<>(cacheEntryListenerServer.getInetAddress(), cacheEntryListenerServer.getPort());
  boolean _isSynchronous = false;
  listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(
    FactoryBuilder.factoryOf(clientListener), null, true, _isSynchronous);
  return cfg.addCacheEntryListenerConfiguration(listenerConfiguration);
}
 
Example 3
Source File: CacheListenerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Override
protected MutableConfiguration<Long, String> extraSetup(MutableConfiguration<Long, String> configuration) {
  cacheEntryListenerServer = new CacheEntryListenerServer<Long, String>(10011, Long.class, String.class);
  try {
    cacheEntryListenerServer.open();
  } catch (IOException e) {
    e.printStackTrace();
  }

  //establish and open a CacheEntryListenerServer to handle cache
  //cache entry events from a CacheEntryListenerClient
  listener = new MyCacheEntryListener<Long, String>();
  cacheEntryListenerServer.addCacheEventListener(listener);

  //establish a CacheEntryListenerClient that a Cache can use for CacheEntryListening
  //(via the CacheEntryListenerServer)
  CacheEntryListenerClient<Long, String> clientListener =
    new CacheEntryListenerClient<>(cacheEntryListenerServer.getInetAddress(), cacheEntryListenerServer.getPort());
  listenerConfiguration = new MutableCacheEntryListenerConfiguration<Long, String>(FactoryBuilder.factoryOf(clientListener), null, true, true);
  return configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
}
 
Example 4
Source File: CacheListenerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void  testDeregistration() {

  assertEquals(1, getConfigurationCacheEntryListenerConfigurationSize(cache));

  MyCacheEntryListener secondListener = new MyCacheEntryListener<Long, String>();
  MutableCacheEntryListenerConfiguration<Long,
      String> secondListenerConfiguration = new
      MutableCacheEntryListenerConfiguration(FactoryBuilder.factoryOf(secondListener), null, false, true);
  cache.registerCacheEntryListener(secondListenerConfiguration);

  assertEquals(2, getConfigurationCacheEntryListenerConfigurationSize(cache));
  cache.deregisterCacheEntryListener(secondListenerConfiguration);

  assertEquals(1, getConfigurationCacheEntryListenerConfigurationSize(cache));

  //no effect if called after it has been removed
  cache.deregisterCacheEntryListener(secondListenerConfiguration);
  assertEquals(1, getConfigurationCacheEntryListenerConfigurationSize(cache));

  //Deregister the listener registered at configuration time
  cache.deregisterCacheEntryListener(listenerConfiguration);
  assertEquals(0, getConfigurationCacheEntryListenerConfigurationSize(cache));
}
 
Example 5
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiration() throws InterruptedException, IllegalArgumentException, URISyntaxException, FailedToStartRedisException, IOException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();

    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
    config.setStoreByValue(true);
    
    URI configUri = getClass().getResource("redisson-jcache.json").toURI();
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null)
            .createCache("test", config);

    CountDownLatch latch = new CountDownLatch(1);
    
    String key = "123";
    ExpiredListener clientListener = new ExpiredListener(latch, key, "90");
    MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = 
            new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(clientListener), null, true, true);
    cache.registerCacheEntryListener(listenerConfiguration);

    cache.put(key, "90");
    Assert.assertNotNull(cache.get(key));
    
    latch.await();
    
    Assert.assertNull(cache.get(key));
    
    cache.close();
    runner.stop();
}
 
Example 6
Source File: AddRemoveListenerICacheTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Prepare(global = false)
public void prepare() {
    cache = cacheManager.getCache(name);

    listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(
            FactoryBuilder.factoryOf(listener),
            FactoryBuilder.factoryOf(filter),
            false, syncEvents);
}
 
Example 7
Source File: EventListenerIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRunEvent_thenCorrect() throws InterruptedException {
    this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(this.listener), null, false, true);
    this.cache.registerCacheEntryListener(this.listenerConfiguration);

    assertEquals(false, this.listener.getCreated());

    this.cache.put("key", "value");
    assertEquals(true, this.listener.getCreated());
    assertEquals(false, this.listener.getUpdated());

    this.cache.put("key", "newValue");
    assertEquals(true, this.listener.getUpdated());
}
 
Example 8
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
protected MutableConfiguration<Integer, Integer> extraSetup(MutableConfiguration<Integer, Integer> configuration) {
  listener = new CacheTestSupport.MyCacheEntryListener<Integer, Integer>();

  //establish a CacheEntryListenerClient that a Cache can use for CacheEntryListening
  //(via the CacheEntryListenerServer)

  listenerConfiguration =
    new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(cacheEntryListerClient), null, true, true);
  cacheEntryListenerServer.addCacheEventListener(listener);
  return configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
}
 
Example 9
Source File: CacheListenerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void  testDynamicRegistration() {

  assertEquals(1, getConfigurationCacheEntryListenerConfigurationSize(cache));

  MyCacheEntryListener secondListener = new MyCacheEntryListener<Long, String>();
  MutableCacheEntryListenerConfiguration<Long,
      String> listenerConfiguration = new
      MutableCacheEntryListenerConfiguration(FactoryBuilder.factoryOf(secondListener), null, false, true);
  cache.registerCacheEntryListener(listenerConfiguration);

  assertEquals(2,getConfigurationCacheEntryListenerConfigurationSize(cache));

  CompleteConfiguration<Long, String> cacheConfig = (CompleteConfiguration)cache.getConfiguration(CompleteConfiguration.class);
  for (CacheEntryListenerConfiguration<Long, String> config : cacheConfig.getCacheEntryListenerConfigurations()) {
    config.hashCode();
    config.isOldValueRequired();
    config.isSynchronous();
  }

  //Can only register the same configuration once
  try {
    cache.registerCacheEntryListener(listenerConfiguration);
    fail();
  } catch (IllegalArgumentException e) {
    //expected
  }
}
 
Example 10
Source File: CacheContinuousWithTransformerReplicatedSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param addEvtFilter Add event filter to ContinuousQueryWithTransformer flag.
 * @param expTransCnt Expected transformed event count.
 * @param keepBinary Keep binary flag.
 * @param async Flag to use transformed event listener with {@link IgniteAsyncCallback}.
 * @throws Exception If failed.
 */
private void runContinuousQueryWithTransformer(boolean addEvtFilter, int expTransCnt, boolean keepBinary,
    boolean async)
    throws Exception {
    Ignite ignite = gridToRunQuery();

    IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);

    if (keepBinary)
        cache = cache.withKeepBinary();

    populateData(cache, JOHN_CONNOR);

    CountDownLatch transUpdCntLatch = new CountDownLatch(expTransCnt);

    AtomicInteger transCnt = new AtomicInteger(0);

    EventListener<String> transLsnr = async ?
        new LocalEventListenerAsync(transCnt, transUpdCntLatch) :
        new LocalEventListener(transCnt, transUpdCntLatch);

    Factory<? extends CacheEntryEventFilter> rmtFilterFactory = null;

    if (addEvtFilter)
        rmtFilterFactory = FactoryBuilder.factoryOf(new RemoteCacheEntryEventFilter());

    Factory<? extends IgniteClosure> factory = FactoryBuilder.factoryOf(new RemoteTransformer(keepBinary));

    ContinuousQueryWithTransformer<Integer, Employee, String> qry = new ContinuousQueryWithTransformer<>();

    qry.setInitialQuery(new ScanQuery<Integer, Employee>());
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Employee>>)rmtFilterFactory);
    qry.setRemoteTransformerFactory(
        (Factory<? extends IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, String>>)factory);
    qry.setLocalListener(transLsnr);

    try (QueryCursor<Cache.Entry<Integer, Employee>> cur = cache.query(qry)) {
        for (Cache.Entry<Integer, Employee> e : cur) {
            assertNotNull(e);

            if (keepBinary) {
                assertTrue(((BinaryObject)e.getValue())
                    .field("name").toString().startsWith(JOHN_CONNOR));
            }
            else {
                assertTrue(e.getValue().name.startsWith(JOHN_CONNOR));
            }
        }

        populateData(cache, SARAH_CONNOR);

        assertTrue("Receive all expected events",
            transUpdCntLatch.await(DFLT_LATCH_TIMEOUT, MILLISECONDS));
        assertEquals("Count of updated records equal to expected", expTransCnt, transCnt.get());

    }
}
 
Example 11
Source File: CacheContinuousQueryFactoryAsyncFilterRandomOperationTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override protected Factory<? extends CacheEntryEventFilter<QueryTestKey, QueryTestValue>> noOpFilterFactory() {
    return FactoryBuilder.factoryOf(NoopAsyncFilter.class);
}
 
Example 12
Source File: MapCacheStoreStrategy.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public Factory<? extends CacheStore<Object, Object>> getStoreFactory() {
    return FactoryBuilder.factoryOf(MapCacheStore.class);
}
 
Example 13
Source File: IgniteCacheGroupsTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param cacheMode Cache mode.
 * @param atomicityMode Atomicity mode.
 * @throws Exception If failed.
 */
private void loadCache(CacheMode cacheMode, CacheAtomicityMode atomicityMode) throws Exception {
    int keys = 100;

    boolean loc = cacheMode == LOCAL;

    Map<Integer, Integer> data1 = generateDataMap(keys);
    Map<Integer, Integer> data2 = generateDataMap(keys);

    Factory<? extends CacheStore<Integer, Integer>> fctr1 =
        FactoryBuilder.factoryOf(new MapBasedStore<>(data1));

    Factory<? extends CacheStore<Integer, Integer>> fctr2 =
        FactoryBuilder.factoryOf(new MapBasedStore<>(data2));

    CacheConfiguration ccfg1 = cacheConfiguration(GROUP1, CACHE1, cacheMode, atomicityMode, 1, false)
        .setCacheStoreFactory(fctr1);

    CacheConfiguration ccfg2 = cacheConfiguration(GROUP1, CACHE2, cacheMode, atomicityMode, 1, false)
        .setCacheStoreFactory(fctr2);

    Ignite node = startGrids(loc ? 1 : 4);

    node.createCaches(F.asList(ccfg1, ccfg2));

    IgniteCache<Integer, Integer> cache1 = node.cache(CACHE1);
    IgniteCache<Integer, Integer> cache2 = node.cache(CACHE2);

    cache1.loadCache(null);

    checkCacheData(data1, CACHE1);

    assertEquals(0, cache2.size());

    cache2.loadCache(null);

    checkCacheData(data2, CACHE2);
}