Search in sources :

Example 1 with Factory

use of javax.cache.configuration.Factory in project ignite by apache.

the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.

/**
 * @param ccfg Cache configuration.
 * @param isClient Client.
 * @throws Exception If failed.
 */
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    QueryCursor<?> cur = null;
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    final CountDownLatch latch = new CountDownLatch(10);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    TestLocalListener localLsnr = new TestLocalListener() {

        @Override
        public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();
                log.info("Received event: " + evt);
            }
        }
    };
    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
    qry.setLocalListener(localLsnr);
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
    IgniteCache<Integer, Integer> cache = null;
    try {
        if (isClient)
            cache = grid(NODES - 1).cache(ccfg.getName());
        else
            cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
        cur = cache.query(qry);
        cache.registerCacheEntryListener(lsnrCfg);
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertTrue(latch.await(3, TimeUnit.SECONDS));
    } finally {
        if (cur != null)
            cur.close();
        if (cache != null)
            cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 2 with Factory

use of javax.cache.configuration.Factory in project Payara by payara.

the class CompleteConfigurationProxy method proxyLoader.

private Factory<CacheLoader<K, V>> proxyLoader(final Factory<CacheLoader<K, V>> fact) {
    return new Factory<CacheLoader<K, V>>() {

        @Override
        public CacheLoader<K, V> create() {
            @Cleanup Context ctx = ctxUtil.pushContext();
            final CacheLoader<K, V> loader = fact.create();
            return new CacheLoaderImpl(loader);
        }

        class CacheLoaderImpl implements CacheLoader<K, V> {

            public CacheLoaderImpl(CacheLoader<K, V> loader) {
                this.loader = loader;
            }

            @Override
            public V load(K k) throws CacheLoaderException {
                @Cleanup Context context = ctxUtil.pushRequestContext();
                return loader.load(k);
            }

            @Override
            public Map<K, V> loadAll(Iterable<? extends K> itrbl) throws CacheLoaderException {
                @Cleanup Context context = ctxUtil.pushRequestContext();
                return loader.loadAll(itrbl);
            }

            private final CacheLoader<K, V> loader;
        }

        private static final long serialVersionUID = 1L;
    };
}
Also used : Context(org.glassfish.internal.api.JavaEEContextUtil.Context) CacheLoaderException(javax.cache.integration.CacheLoaderException) Factory(javax.cache.configuration.Factory) Cleanup(lombok.Cleanup) Map(java.util.Map)

Example 3 with Factory

use of javax.cache.configuration.Factory in project ignite by apache.

the class UsingContinuousQueries method remoteFilterExample.

public static void remoteFilterExample() {
    try (Ignite ignite = Ignition.start()) {
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
        // tag::remoteFilter[]
        ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
        qry.setLocalListener(events -> events.forEach(event -> System.out.format("Entry: key=[%s] value=[%s]\n", event.getKey(), event.getValue())));
        qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {

            @Override
            public CacheEntryEventFilter<Integer, String> create() {
                return new CacheEntryEventFilter<Integer, String>() {

                    @Override
                    public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
                        System.out.format("the value for key [%s] was updated from [%s] to [%s]\n", e.getKey(), e.getOldValue(), e.getValue());
                        return true;
                    }
                };
            }
        });
        // end::remoteFilter[]
        cache.query(qry);
        cache.put(1, "1");
    }
}
Also used : Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) Ignition(org.apache.ignite.Ignition) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter)

Example 4 with Factory

use of javax.cache.configuration.Factory in project ignite by apache.

the class AbstractDataTypesCoverageTest method parameters.

/**
 * @return Test parameters.
 */
@Parameterized.Parameters(name = "atomicityMode={1}, cacheMode={2}, ttlFactory={3}, backups={4}," + " evictionFactory={5}, onheapCacheEnabled={6}, writeSyncMode={7}, persistenceEnabled={8}, useBinaryArrays={9}")
public static Collection parameters() {
    Set<Object[]> params = new HashSet<>();
    Object[] baseParamLine = { null, CacheAtomicityMode.ATOMIC, CacheMode.PARTITIONED, null, 2, null, false, CacheWriteSynchronizationMode.FULL_SYNC, false, DFLT_IGNITE_USE_BINARY_ARRAYS };
    Object[] paramLine = null;
    for (CacheAtomicityMode atomicityMode : CacheAtomicityMode.values()) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[1] = atomicityMode;
        params.add(paramLine);
    }
    for (CacheMode cacheMode : CacheMode.values()) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[2] = cacheMode;
        params.add(paramLine);
    }
    assert paramLine != null;
    if ((paramLine[1]) != CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) {
        for (Factory ttlFactory : TTL_FACTORIES) {
            paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
            paramLine[3] = ttlFactory;
            params.add(paramLine);
        }
    }
    for (int backups : new int[] { 0, 1, 2 }) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[4] = backups;
        params.add(paramLine);
    }
    for (Factory evictionFactory : EVICTION_FACTORIES) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[5] = evictionFactory;
        params.add(paramLine);
    }
    for (Boolean onheapCacheEnabled : BOOLEANS) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[6] = onheapCacheEnabled;
        params.add(paramLine);
    }
    for (CacheWriteSynchronizationMode writeSyncMode : CacheWriteSynchronizationMode.values()) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[7] = writeSyncMode;
        params.add(paramLine);
    }
    for (boolean persistenceEnabled : BOOLEANS) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[8] = persistenceEnabled;
        params.add(paramLine);
    }
    for (boolean useTypedArrays : BOOLEANS) {
        paramLine = Arrays.copyOf(baseParamLine, baseParamLine.length);
        paramLine[9] = useTypedArrays;
        params.add(paramLine);
    }
    for (Object[] pLine : params) pLine[0] = UUID.randomUUID();
    return params;
}
Also used : CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) FifoEvictionPolicyFactory(org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicyFactory) Factory(javax.cache.configuration.Factory) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) CacheMode(org.apache.ignite.cache.CacheMode) HashSet(java.util.HashSet)

Example 5 with Factory

use of javax.cache.configuration.Factory in project ignite by apache.

the class EntryVersionConsistencyReadThroughTest method createCacheConfiguration.

/**
 * @param atomicityMode Atomicity mode.
 * @return Cache configuration.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private CacheConfiguration<String, List<Double>> createCacheConfiguration(CacheAtomicityMode atomicityMode) {
    CacheConfiguration<String, List<Double>> cc = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    cc.setCacheMode(PARTITIONED);
    cc.setAtomicityMode(atomicityMode);
    cc.setWriteSynchronizationMode(FULL_SYNC);
    cc.setReadThrough(true);
    cc.setWriteThrough(true);
    Factory cacheStoreFactory = new FactoryBuilder.SingletonFactory(new DummyCacheStore());
    cc.setCacheStoreFactory(cacheStoreFactory);
    cc.setBackups(2);
    return cc;
}
Also used : Factory(javax.cache.configuration.Factory) ArrayList(java.util.ArrayList) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

Factory (javax.cache.configuration.Factory)20 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)10 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)10 IgniteCache (org.apache.ignite.IgniteCache)6 CacheEntryEvent (javax.cache.event.CacheEntryEvent)5 Ignite (org.apache.ignite.Ignite)5 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 IgniteEx (org.apache.ignite.internal.IgniteEx)5 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)5 Test (org.junit.Test)5 Map (java.util.Map)4 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Cache (javax.cache.Cache)3 CacheException (javax.cache.CacheException)3 FactoryBuilder (javax.cache.configuration.FactoryBuilder)3 IgniteException (org.apache.ignite.IgniteException)3 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 GridDeploymentRequest (org.apache.ignite.internal.managers.deployment.GridDeploymentRequest)3