Java Code Examples for org.apache.ignite.configuration.CacheConfiguration#setCacheStoreSessionListenerFactories()

The following examples show how to use org.apache.ignite.configuration.CacheConfiguration#setCacheStoreSessionListenerFactories() . 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: GridCacheReplicatedPreloadSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param cacheCfg Configuration.
 */
private void loadExternalClassesToCfg(CacheConfiguration cacheCfg) {
    try {
        Object sf = getExternalClassLoader().
            loadClass("org.apache.ignite.tests.p2p.CacheDeploymentTestStoreFactory").newInstance();

        cacheCfg.setCacheStoreFactory((Factory)sf);

        Object sslf = getExternalClassLoader().
            loadClass("org.apache.ignite.tests.p2p.CacheDeploymentStoreSessionListenerFactory").newInstance();

        cacheCfg.setCacheStoreSessionListenerFactories((Factory)sslf);

        Object cpc = getExternalClassLoader().
            loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCachePluginConfiguration").newInstance();

        cacheCfg.setPluginConfigurations((CachePluginConfiguration)cpc);

        Object akm = getExternalClassLoader().
            loadClass("org.apache.ignite.tests.p2p.CacheDeploymentAffinityKeyMapper").newInstance();

        cacheCfg.setAffinityMapper((AffinityKeyMapper)akm);

        Object pred = getExternalClassLoader().
            loadClass("org.apache.ignite.tests.p2p.CacheDeploymentAlwaysTruePredicate2").newInstance();

        cacheCfg.setNodeFilter((IgnitePredicate)pred);
    }
    catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: CacheStoreSessionListenerAbstractSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param name Cache name.
 * @param atomicity Atomicity mode.
 * @return Cache configuration.
 */
private CacheConfiguration<Integer, Integer> cacheConfiguration(String name, CacheAtomicityMode atomicity) {
    CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>();

    cfg.setName(name);
    cfg.setAtomicityMode(atomicity);
    cfg.setCacheStoreFactory(storeFactory());
    cfg.setCacheStoreSessionListenerFactories(sessionListenerFactory());
    cfg.setReadThrough(true);
    cfg.setWriteThrough(true);
    cfg.setLoadPreviousValue(true);

    return cfg;
}
 
Example 3
Source File: H2CacheStoreStrategy.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public void updateCacheConfiguration(CacheConfiguration<Object, Object> cfg) {
    cfg.setCacheStoreSessionListenerFactories(new H2CacheStoreSessionListenerFactory(port));
}
 
Example 4
Source File: CacheStoreSessionListenerWriteBehindEnabledTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override protected CacheConfiguration cacheConfiguration(String igniteInstanceName) throws Exception {
    MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.CACHE_STORE);

    CacheConfiguration cacheCfg = super.cacheConfiguration(igniteInstanceName);

    cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(EmptyCacheStore.class));

    cacheCfg.setCacheStoreSessionListenerFactories(new CacheStoreSessionFactory());

    cacheCfg.setReadThrough(true);
    cacheCfg.setWriteThrough(true);

    cacheCfg.setWriteBehindEnabled(true);
    cacheCfg.setWriteBehindBatchSize(CNT * 2);
    cacheCfg.setWriteBehindFlushFrequency(WRITE_BEHIND_FLUSH_FREQUENCY);

    cacheCfg.setBackups(0);

    return cacheCfg;
}
 
Example 5
Source File: CacheStoreSessionListenerLifecycleSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPartialOverride() throws Exception {
    try {
        Ignite ignite = startGrid();

        for (int i = 0; i < 2; i++) {
            String name = "cache-" + i;

            CacheConfiguration cacheCfg = cacheConfiguration(name);

            cacheCfg.setAtomicityMode(TRANSACTIONAL);

            if (i == 0) {
                cacheCfg.setCacheStoreSessionListenerFactories(
                    new SessionListenerFactory(name + " 1"),
                    new SessionListenerFactory(name + " 2")
                );
            }

            ignite.createCache(cacheCfg);
        }

        ignite.cache("cache-0").put(1, 1);
        ignite.cache("cache-1").put(1, 1);

        try (Transaction tx = ignite.transactions().txStart()) {
            ignite.cache("cache-0").put(2, 2);
            ignite.cache("cache-0").put(3, 3);
            ignite.cache("cache-1").put(2, 2);
            ignite.cache("cache-1").put(3, 3);

            tx.commit();
        }
    }
    finally {
        stopGrid();
    }

    assertEqualsCollections(Arrays.asList(
        "Shared 1 START",
        "Shared 2 START",
        "cache-0 1 START",
        "cache-0 2 START",

        // Put to cache-0.
        "cache-0 1 SESSION START cache-0",
        "cache-0 2 SESSION START cache-0",
        "cache-0 1 SESSION END cache-0",
        "cache-0 2 SESSION END cache-0",

        // Put to cache-1.
        "Shared 1 SESSION START cache-1",
        "Shared 2 SESSION START cache-1",
        "Shared 1 SESSION END cache-1",
        "Shared 2 SESSION END cache-1",

        // Transaction.
        "cache-0 1 SESSION START cache-0",
        "cache-0 2 SESSION START cache-0",
        "Shared 1 SESSION START cache-1",
        "Shared 2 SESSION START cache-1",
        "cache-0 1 SESSION END cache-0",
        "cache-0 2 SESSION END cache-0",
        "Shared 1 SESSION END cache-1",
        "Shared 2 SESSION END cache-1",

        "cache-0 1 STOP",
        "cache-0 2 STOP",
        "Shared 1 STOP",
        "Shared 2 STOP"
    ), evts);
}
 
Example 6
Source File: CacheStoreSessionListenerLifecycleSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testOverride() throws Exception {
    try {
        Ignite ignite = startGrid();

        for (int i = 0; i < 2; i++) {
            String name = "cache-" + i;

            CacheConfiguration cacheCfg = cacheConfiguration(name);

            cacheCfg.setCacheStoreSessionListenerFactories(new SessionListenerFactory(name + " 1"), new SessionListenerFactory(name + " 2"));

            ignite.createCache(cacheCfg);
        }

        ignite.cache("cache-0").put(1, 1);
        ignite.cache("cache-1").put(1, 1);

        try (Transaction tx = ignite.transactions().txStart()) {
            ignite.cache("cache-0").put(2, 2);
            ignite.cache("cache-0").put(3, 3);
            ignite.cache("cache-1").put(2, 2);
            ignite.cache("cache-1").put(3, 3);

            tx.commit();
        }

        // Force cache shutdown order
        ignite.cache("cache-0").destroy();
    }
    finally {
        stopGrid();
    }

    assertEqualsCollections(Arrays.asList(
        "Shared 1 START",
        "Shared 2 START",
        "cache-0 1 START",
        "cache-0 2 START",
        "cache-1 1 START",
        "cache-1 2 START",

        // Put to cache-0.
        "cache-0 1 SESSION START cache-0",
        "cache-0 2 SESSION START cache-0",
        "cache-0 1 SESSION END cache-0",
        "cache-0 2 SESSION END cache-0",

        // Put to cache-1.
        "cache-1 1 SESSION START cache-1",
        "cache-1 2 SESSION START cache-1",
        "cache-1 1 SESSION END cache-1",
        "cache-1 2 SESSION END cache-1",

        // Transaction.
        "cache-0 1 SESSION START cache-0",
        "cache-0 2 SESSION START cache-0",
        "cache-1 1 SESSION START cache-1",
        "cache-1 2 SESSION START cache-1",
        "cache-0 1 SESSION END cache-0",
        "cache-0 2 SESSION END cache-0",
        "cache-1 1 SESSION END cache-1",
        "cache-1 2 SESSION END cache-1",

        "cache-0 1 STOP",
        "cache-0 2 STOP",
        "cache-1 1 STOP",
        "cache-1 2 STOP",
        "Shared 1 STOP",
        "Shared 2 STOP"
    ), evts);
}
 
Example 7
Source File: CacheSpringStoreExample.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    ExamplesUtils.checkMinMemory(MIN_MEMORY);

    // To start ignite with desired configuration uncomment the appropriate line.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache store example started.");

        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(CACHE_NAME);

        // Set atomicity as transaction, since we are showing transactions in example.
        cacheCfg.setAtomicityMode(TRANSACTIONAL);

        // Configure Spring store.
        cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheSpringPersonStore.class));

        // Configure Spring session listener.
        cacheCfg.setCacheStoreSessionListenerFactories(new Factory<CacheStoreSessionListener>() {
            @Override public CacheStoreSessionListener create() {
                CacheSpringStoreSessionListener lsnr = new CacheSpringStoreSessionListener();

                lsnr.setDataSource(CacheSpringPersonStore.DATA_SRC);

                return lsnr;
            }
        });

        cacheCfg.setReadThrough(true);
        cacheCfg.setWriteThrough(true);

        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg)) {
            // Make initial cache loading from persistent store. This is a
            // distributed operation and will call CacheStore.loadCache(...)
            // method on all nodes in topology.
            loadCache(cache);

            // Start transaction and execute several cache operations with
            // read/write-through to persistent store.
            executeTransaction(cache);
        }
        finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
 
Example 8
Source File: CacheJdbcStoreExample.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    ExamplesUtils.checkMinMemory(MIN_MEMORY);

    // To start ignite with desired configuration uncomment the appropriate line.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache store example started.");

        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(CACHE_NAME);

        // Set atomicity as transaction, since we are showing transactions in example.
        cacheCfg.setAtomicityMode(TRANSACTIONAL);

        // Configure JDBC store.
        cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheJdbcPersonStore.class));

        // Configure JDBC session listener.
        cacheCfg.setCacheStoreSessionListenerFactories(new Factory<CacheStoreSessionListener>() {
            @Override public CacheStoreSessionListener create() {
                CacheJdbcStoreSessionListener lsnr = new CacheJdbcStoreSessionListener();

                lsnr.setDataSource(JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", ""));

                return lsnr;
            }
        });

        cacheCfg.setReadThrough(true);
        cacheCfg.setWriteThrough(true);

        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg)) {
            // Make initial cache loading from persistent store. This is a
            // distributed operation and will call CacheStore.loadCache(...)
            // method on all nodes in topology.
            loadCache(cache);

            // Start transaction and execute several cache operations with
            // read/write-through to persistent store.
            executeTransaction(cache);
        }
        finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
 
Example 9
Source File: CacheHibernateStoreExample.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    ExamplesUtils.checkMinMemory(MIN_MEMORY);

    // To start ignite with desired configuration uncomment the appropriate line.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache store example started.");

        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(CACHE_NAME);

        // Set atomicity as transaction, since we are showing transactions in example.
        cacheCfg.setAtomicityMode(TRANSACTIONAL);

        // Configure Hibernate store.
        cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheHibernatePersonStore.class));

        // Configure Hibernate session listener.
        cacheCfg.setCacheStoreSessionListenerFactories(new Factory<CacheStoreSessionListener>() {
            @Override public CacheStoreSessionListener create() {
                CacheHibernateStoreSessionListener lsnr = new CacheHibernateStoreSessionListener();

                lsnr.setHibernateConfigurationPath(HIBERNATE_CFG);

                return lsnr;
            }
        });

        cacheCfg.setReadThrough(true);
        cacheCfg.setWriteThrough(true);

        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg)) {
            // Make initial cache loading from persistent store. This is a
            // distributed operation and will call CacheStore.loadCache(...)
            // method on all nodes in topology.
            loadCache(cache);

            // Start transaction and execute several cache operations with
            // read/write-through to persistent store.
            executeTransaction(cache);
        }
        finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
 
Example 10
Source File: CacheStoreSessionListenerReadWriteThroughDisabledAbstractTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Override protected CacheConfiguration cacheConfiguration(String igniteInstanceName) throws Exception {
    CacheConfiguration cacheCfg = super.cacheConfiguration(igniteInstanceName);

    cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(EmptyCacheStore.class));

    cacheCfg.setCacheStoreSessionListenerFactories(new CacheStoreSessionFactory());

    cacheCfg.setReadThrough(false);
    cacheCfg.setWriteThrough(false);

    cacheCfg.setBackups(0);

    return cacheCfg;
}