use of org.apache.ignite.examples.model.Person 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);
}
}
}
use of org.apache.ignite.examples.model.Person in project ignite by apache.
the class CacheHibernateStoreExample method executeTransaction.
/**
* Executes transaction with read/write-through to persistent store.
*
* @param cache Cache to execute transaction on.
*/
private static void executeTransaction(IgniteCache<Long, Person> cache) {
try (Transaction tx = Ignition.ignite().transactions().txStart()) {
Person val = cache.get(id);
System.out.println("Read value: " + val);
val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
System.out.println("Overwrote old value: " + val);
val = cache.get(id);
System.out.println("Read value: " + val);
tx.commit();
}
System.out.println("Read value after commit: " + cache.get(id));
}
use of org.apache.ignite.examples.model.Person in project ignite by apache.
the class CacheHibernateStoreExample 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 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);
}
}
}
use of org.apache.ignite.examples.model.Person in project ignite by apache.
the class SpringDataExample method populateRepository.
/**
* Fills the repository in with sample data.
*/
private static void populateRepository() {
TreeMap<Long, Person> persons = new TreeMap<>();
persons.put(1L, new Person(1L, 2000L, "John", "Smith", 15000, "Worked for Apple"));
persons.put(2L, new Person(2L, 2000L, "Brad", "Pitt", 16000, "Worked for Oracle"));
persons.put(3L, new Person(3L, 1000L, "Mark", "Tomson", 10000, "Worked for Sun"));
persons.put(4L, new Person(4L, 2000L, "Erick", "Smith", 13000, "Worked for Apple"));
persons.put(5L, new Person(5L, 1000L, "John", "Rozenberg", 25000, "Worked for RedHat"));
persons.put(6L, new Person(6L, 2000L, "Denis", "Won", 35000, "Worked for CBS"));
persons.put(7L, new Person(7L, 1000L, "Abdula", "Adis", 45000, "Worked for NBC"));
persons.put(8L, new Person(8L, 2000L, "Roman", "Ive", 15000, "Worked for Sun"));
// Adding data into the repository.
repo.save(persons);
System.out.println("\n>>> Added " + repo.count() + " Persons into the repository.");
}
use of org.apache.ignite.examples.model.Person in project ignite by apache.
the class SpringDataExample method findPersons.
/**
* Gets a list of Persons using standard read operations.
*/
private static void findPersons() {
// Getting Person with specific ID.
Person person = repo.findOne(2L);
System.out.println("\n>>> Found Person [id=" + 2L + ", val=" + person + "]");
// Getting a list of Persons.
ArrayList<Long> ids = new ArrayList<>();
for (long i = 0; i < 5; i++) ids.add(i);
Iterator<Person> persons = repo.findAll(ids).iterator();
System.out.println("\n>>> Persons list for specific ids: ");
while (persons.hasNext()) System.out.println(" >>> " + persons.next());
}
Aggregations