Coherence Save Retrieve Remove Batch Insert Data Cache Example

In this example we will connect to the cache and retrieve all data’s, iterate all then changes one attribute value, save data and finally save all data in batch to the cache:

  • CheckAllCachesDatas.java:
package com.javahonk.cache;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tangosol.net.NamedCache;
import com.javahonk.datautil.CacheRegistry;
import com.model.trade.ExecutionModel;

public class CheckAllCachesDatas {
	
	public static void main(String[] args) {
	
		//VM Arguments
		System.setProperty("wells.ccu.clientconfig", "extend-access.xml");
		System.setProperty("javahonk.ccu.url", "http://cacheurl.com/clusterclient/clustername/server/");	
		System.setProperty("javahonk.cluster.extend.access", "true");
		System.setProperty("javahonk.ccu.serializer.config", "http://cacheurl.com/clusterclient/clustername/");
		System.setProperty("tangosol.pof.enabled", "true");
		System.setProperty("tangosol.pof.config", "custom-types-pof-config.xml");
		System.setProperty("logging.dir", "./");
		
		ApplicationContext context = new ClassPathXmlApplicationContext("all-cache-context.xml");
		
		context.getBean(CheckAllCachesDatas.class).checkAllCachesDatas();		

		((AbstractApplicationContext) context).close();
		
	}
	
	@SuppressWarnings("unchecked")
	public void checkAllCachesDatas() {
		
		Map<String, Object> datas = new HashMap<>();
		
		NamedCache cache = (NamedCache) CacheRegistry.getNamedCache("OTCExecutions");
	
		Set<ExecutionModel> keysetEx = cache.keySet();
		System.out.println(keysetEx.size());
		
		for (Object object : keysetEx) {
			
			try {
				//Retrieve data from cache
				
				ExecutionModel obj2 = (ExecutionModel)cache.get(object.toString());
				obj2.setEntity("Test");
				
				//Save data to the cache
				cache.put(object, obj2);
				
				//Remove data from cache
				cache.remove(obj2);				
								
				System.out.println(obj2);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//Store data in batch to the cache
		datas.putAll(datas);
		
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *