Search in sources :

Example 1 with CacheSpringStoreSessionListener

use of org.apache.ignite.cache.store.spring.CacheSpringStoreSessionListener in project ignite by apache.

the class CacheSpringStoreExample method main.

/**
     * 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);
        }
    }
}
Also used : CacheSpringStoreSessionListener(org.apache.ignite.cache.store.spring.CacheSpringStoreSessionListener) Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person) CacheStoreSessionListener(org.apache.ignite.cache.store.CacheStoreSessionListener) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

Ignite (org.apache.ignite.Ignite)1 CacheStoreSessionListener (org.apache.ignite.cache.store.CacheStoreSessionListener)1 CacheSpringStoreSessionListener (org.apache.ignite.cache.store.spring.CacheSpringStoreSessionListener)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 Person (org.apache.ignite.examples.model.Person)1