Save In Memory Data In Spring Boot
Posted By : Anil Kumar | 28-May-2018
Introduction:
When we are working on an application where some type of operations is required before saving the data in the database.
We are required to hold incoming data into memory.
For this purpose, we can save data in the Concurrent hash map.
Create a Concurrent hash map to save data in memory where the key is the partition name and value is the again a hashmap
having price is the key and priority queue of orders is the value of hashmap
In this example partition is the name of orders table partition name.
A partition has a particular type of orders
Example:
import java.math.BigDecimal;
import java.util.List;
import java.util.PriorityQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.base.DAO.OrderMatchingDao;
import com.base.domain.Orders;
import com.base.util.CurrencyPairExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class Bootstrap implements ApplicationListener<ContextRefreshedEvent> {
public static ConcurrentMap<String, CurrencyPairExecution> concurrentHashMap = new ConcurrentHashMap<>();
private static final Logger logger = LoggerFactory.getLogger(Bootstrap.class);
@Autowired
OrderMatchingDao orderMatchingDao;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
createConcurrentHashMap();
}
/**
* @description : Get list of currency pair from metadata get buy/sell orderList
* from orderBook Construct CurrentHashMap per currency pair for
* buy order or sell order individually.
*
*/
private void createConcurrentHashMap() {
List<String> partitions = orderMatchingDao.findPartitions();
logger.info("number of partitions *************" + partitions.size());
if (partitions.size() > 0) {
Integer itr = Integer.MAX_VALUE;
for (String partition : partitions) {
logger.info("partition name **************" + partition);
CurrencyPairExecution currencyPairExecution = new CurrencyPairExecution();
PriorityQueue<Orders> priorityQueue = new PriorityQueue<>(new Orders());
ConcurrentMap<BigDecimal, PriorityQueue<Orders>> concurrentHashMapOrders = new ConcurrentHashMap<>();
List<Orders> orderList = orderMatchingDao.findOrdersByPartition(partition);
logger.info("order List size**************" + orderList.size());
if (!orderList.isEmpty()) {
for (Orders order : orderList) {
logger.info("order price**************" + order.getPrice());
if (!concurrentHashMapOrders.containsKey(order.getPrice())) {
PriorityQueue<Orders> priorityQueue1 = new PriorityQueue<>(new Orders());// need to // review
priorityQueue1.add(order);
logger.info("Order added in priorityQueue1 {}", order.getOrderId());
concurrentHashMapOrders.put(order.getPrice(), priorityQueue1);
} else {
priorityQueue = concurrentHashMapOrders.get(order.getPrice());
priorityQueue.add(order);
logger.info("Order added in priorityQueue {}", order.getOrderId());
concurrentHashMapOrders.put(order.getPrice(), priorityQueue);
}
}
}
// After everything is done, set all the required params to
// currencyPairExecution
currencyPairExecution.setConcurrentHashMap(concurrentHashMapOrders);
concurrentHashMap.put(partition, currencyPairExecution);
}
}
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anil Kumar
Anil is a Web Developer who specializes in creating dynamic and beautiful web projects and has good experience of working in distributed teams.