Java Code Examples for javax.cache.configuration.Factory#create()

The following examples show how to use javax.cache.configuration.Factory#create() . 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: GridCacheUtils.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and starts store session listeners.
 *
 * @param ctx Kernal context.
 * @param factories Factories.
 * @return Listeners.
 * @throws IgniteCheckedException In case of error.
 */
public static Collection<CacheStoreSessionListener> startStoreSessionListeners(GridKernalContext ctx,
    Factory<CacheStoreSessionListener>[] factories) throws IgniteCheckedException {
    if (factories == null)
        return null;

    Collection<CacheStoreSessionListener> lsnrs = new ArrayList<>(factories.length);

    for (Factory<CacheStoreSessionListener> factory : factories) {
        CacheStoreSessionListener lsnr = factory.create();

        if (lsnr != null) {
            ctx.resource().injectGeneric(lsnr);

            if (lsnr instanceof LifecycleAware)
                ((LifecycleAware)lsnr).start();

            lsnrs.add(lsnr);
        }
    }

    return lsnrs;
}
 
Example 2
Source File: ConfigurationMerger.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfiguration<K, V> config) {
  Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory();
  @SuppressWarnings("unchecked")
  Factory<CacheWriter<K, V>> cacheWriterFactory = (Factory<CacheWriter<K, V>>) (Object) config.getCacheWriterFactory();

  if (config.isReadThrough() && cacheLoaderFactory == null) {
    throw new IllegalArgumentException("read-through enabled without a CacheLoader factory provided");
  }
  if (config.isWriteThrough() && cacheWriterFactory == null) {
    throw new IllegalArgumentException("write-through enabled without a CacheWriter factory provided");
  }

  CacheLoader<K, V> cacheLoader = cacheLoaderFactory == null ? null : cacheLoaderFactory.create();
  CacheWriter<K, V> cacheWriter;
  try {
    cacheWriter = cacheWriterFactory == null ? null : cacheWriterFactory.create();
  } catch (Throwable t) {
    throw closeAllAfter(new CacheException(t), cacheLoader);
  }

  if (cacheLoader == null && cacheWriter == null) {
    return null;
  } else {
    return new Eh107CacheLoaderWriter<>(cacheLoader, config.isReadThrough(), cacheWriter, config.isWriteThrough());
  }
}
 
Example 3
Source File: JCache.java    From redisson with Apache License 2.0 6 votes vote down vote up
public JCache(JCacheManager cacheManager, Redisson redisson, String name, JCacheConfiguration<K, V> config, boolean hasOwnRedisson) {
    super(redisson.getConfig().getCodec(), redisson.getCommandExecutor(), name);
    
    this.hasOwnRedisson = hasOwnRedisson;
    this.redisson = redisson;
    
    Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory();
    if (cacheLoaderFactory != null) {
        cacheLoader = cacheLoaderFactory.create();
    }
    Factory<CacheWriter<? super K, ? super V>> cacheWriterFactory = config.getCacheWriterFactory();
    if (config.getCacheWriterFactory() != null) {
        cacheWriter = (CacheWriter<K, V>) cacheWriterFactory.create();
    }
    
    this.cacheManager = cacheManager;
    this.config = config;
    
    redisson.getEvictionScheduler().scheduleJCache(getName(), getTimeoutSetName(), getExpiredChannelName());
    
    for (CacheEntryListenerConfiguration<K, V> listenerConfig : config.getCacheEntryListenerConfigurations()) {
        registerCacheEntryListener(listenerConfig, false);
    }
}
 
Example 4
Source File: JCSListener.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
public JCSListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
{
    oldValue = cacheEntryListenerConfiguration.isOldValueRequired();
    synchronous = cacheEntryListenerConfiguration.isSynchronous();

    final Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = cacheEntryListenerConfiguration
            .getCacheEntryEventFilterFactory();
    if (filterFactory == null)
    {
        filter = NoFilter.INSTANCE;
    }
    else
    {
        filter = filterFactory.create();
    }

    delegate = cacheEntryListenerConfiguration.getCacheEntryListenerFactory().create();
    remove = CacheEntryRemovedListener.class.isInstance(delegate);
    expire = CacheEntryExpiredListener.class.isInstance(delegate);
    update = CacheEntryUpdatedListener.class.isInstance(delegate);
    create = CacheEntryCreatedListener.class.isInstance(delegate);
}
 
Example 5
Source File: GridCacheAbstractSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param igniteInstanceName Ignite instance name.
 * @return Cache configuration.
 * @throws Exception In case of error.
 */
@SuppressWarnings("unchecked")
protected CacheConfiguration cacheConfiguration(String igniteInstanceName) throws Exception {
    CacheConfiguration cfg = defaultCacheConfiguration();

    if (storeStgy != null) {
        Factory<? extends CacheStore<Object, Object>> storeFactory = storeStgy.getStoreFactory();

        CacheStore<?, ?> store = storeFactory.create();

        if (store != null) {
            cfg.setCacheStoreFactory(storeFactory);
            cfg.setReadThrough(true);
            cfg.setWriteThrough(true);
            cfg.setLoadPreviousValue(true);
            storeStgy.updateCacheConfiguration(cfg);
        }
    }

    cfg.setCacheMode(cacheMode());
    cfg.setAtomicityMode(atomicityMode());
    cfg.setWriteSynchronizationMode(writeSynchronization());
    cfg.setNearConfiguration(nearConfiguration());
    cfg.setOnheapCacheEnabled(onheapCacheEnabled());

    Class<?>[] idxTypes = indexedTypes();

    if (!F.isEmpty(idxTypes))
        cfg.setIndexedTypes(idxTypes);

    if (cacheMode() == PARTITIONED)
        cfg.setBackups(backups());

    return cfg;
}
 
Example 6
Source File: ListenerEntry.java    From triava with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a ListenerEntry from the factories in CacheEntryListenerConfiguration.
 * Both CacheEntryEventFilter and CacheEntryListener are created.
 * The {@link #dispatchMode} regulates how events get dispatched, for example synchronous, asynchronous batched or timed
 * 
 * @param config The CacheEntryListenerConfiguration
 * @param tcache The cache which events should be listened to 
 * @param dispatchMode How events are dispatched to listeners
 */
ListenerEntry(CacheEntryListenerConfiguration<K, V> config, Cache<K,V> tcache, DispatchMode dispatchMode)
{
	this.config = config;
	this.tcache = tcache;
	this.dispatchMode = dispatchMode;

	CacheEventManager<K,V> em = null;
	Factory<CacheEntryListener<? super K, ? super V>> listenerFactory = config.getCacheEntryListenerFactory();
	if (listenerFactory != null)
	{
		listener = listenerFactory.create();
		if (listener != null)
		{
			Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = config
					.getCacheEntryEventFilterFactory();
			if (filterFactory != null)
				filter = filterFactory.create();
			
			em = new ListenerCacheEventManager<K,V>();
		}
	}

	eventManager = em;


       /**
        * Initialize dispatchQueue and corresponding Thread. This has to be done even for the synchronous
        * DispatchMode#SYNC, as it can be forced to operate asynchronously for internal operations like
        * expiration and eviction.
        */
       this.dispatchQueue = new ArrayBlockingQueue<TCacheEntryEventCollection<K, V>>(1024);
       /**
        * Future directions: Starting the listener in the constructor is problematic. If this class would be
        * subclassed, the Thread would start too early. Right now it cannot happen, as this class is final.
        * Second, we possibly want a Thread restart mechanism anyhow, like we have with the expiration and
        * eviction threads. For the latter, there should be a dedicated "BackgroundThreadController<T>" class
        * that controls/restarts background threads.
        */
       dispatchThread = ensureListenerThreadIsRunning();
   }
 
Example 7
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWithoutEhcacheExplicitDependencyAndNoCodeChanges() throws Exception {
  CacheManager manager = cachingProvider.getCacheManager(
      getClass().getResource("/org/ehcache/docs/ehcache-jsr107-template-override.xml").toURI(),
      getClass().getClassLoader());

  // tag::jsr107SupplementWithTemplatesExample[]
  MutableConfiguration<Long, Client> mutableConfiguration = new MutableConfiguration<>();
  mutableConfiguration.setTypes(Long.class, Client.class); // <1>

  Cache<Long, Client> anyCache = manager.createCache("anyCache", mutableConfiguration); // <2>

  CacheRuntimeConfiguration<Long, Client> ehcacheConfig = (CacheRuntimeConfiguration<Long, Client>)anyCache.getConfiguration(
      Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class); // <3>
  ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(); // <4>

  Cache<Long, Client> anotherCache = manager.createCache("byRefCache", mutableConfiguration);
  assertFalse(anotherCache.getConfiguration(Configuration.class).isStoreByValue()); // <5>

  MutableConfiguration<String, Client> otherConfiguration = new MutableConfiguration<>();
  otherConfiguration.setTypes(String.class, Client.class);
  otherConfiguration.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // <6>

  Cache<String, Client> foosCache = manager.createCache("foos", otherConfiguration);// <7>
  CacheRuntimeConfiguration<Long, Client> foosEhcacheConfig = (CacheRuntimeConfiguration<Long, Client>)foosCache.getConfiguration(
      Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class);
  Client client1 = new Client("client1", 1);
  foosEhcacheConfig.getExpiryPolicy().getExpiryForCreation(42L, client1).toMinutes(); // <8>

  CompleteConfiguration<String, String> foosConfig = foosCache.getConfiguration(CompleteConfiguration.class);

  try {
    final Factory<ExpiryPolicy> expiryPolicyFactory = foosConfig.getExpiryPolicyFactory();
    ExpiryPolicy expiryPolicy = expiryPolicyFactory.create(); // <9>
    throw new AssertionError("Expected UnsupportedOperationException");
  } catch (UnsupportedOperationException e) {
    // Expected
  }
  // end::jsr107SupplementWithTemplatesExample[]
  assertThat(ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(), is(20L));
  assertThat(foosEhcacheConfig.getExpiryPolicy().getExpiryForCreation(42L, client1),
      is(java.time.Duration.ofMinutes(2)));
}