Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Do you know similar case with some example for this scenario? How could I implement it in Java / Spring 3/4 application?

I will simplify the scenario so it would be easy to understand. Firstly I am consuming REST / webservice with simple data: id of the product and its price. These data are changing in real time. Each customer logged into application will see different prices (base prices from the endpoint calculated using some margins and segments). Each customer must see these data in possible shortest time, so I could have a map in memory that has simple data for logged customers and their margins so I wouldnt have to query the database. I cant just do a simple loop checking this map because there will be thousands of customers logged in so the last one in the loop would have to wait even more than one second. I also cannot create new thread in Java for every customer logged in can I? Wouldnt it be too many threads?

I am planning to use Spring4 and maybe websockets abstraction in Spring 4 but I don't know how to implement and quarantee low latency for thousands of customers that have different calculations?

PriceVisibleForCustomer = Price + CustomerMargin

Price - changing every 300ms
CustomerMargn - some plus or minus amount that is resulting from customer agreement/segment/administrator decision etc. It doesnt change during customer http session
Customer - he takes part in the process after he logs in, he should see rapidly changing prices of 8 products.

Java
/**
     * Sends prices to customer. This method is called very often (300ms) as prices are changing in real time.
     * Customer should see prices also each 300ms
     * @param productId - id of a product that prices will be calculated
     * @param productIdToPriceMap
     * @param customerIdToMarginMap - this map is changed every time customer logs in or logs out
     */
    private static void sendPricesToCustomers(Long productId,
            Map<Long, BigDecimal> productIdToPriceMap,
            Map<Long, BigDecimal> customerIdToMarginMap) {

        //This loop is blocking last customer from receiving price until all other customers wil have theri prices calculated. I could create threads, 10000+ customers will be logged in, I cant create so much threads... can I?
        for (Long customerId: customerIdToMarginMap.keySet()){
            BigDecimal customerMargin = customerIdToMarginMap.get(customerId);
            BigDecimal priceResult = productIdToPriceMap.get(productId).add(customerMargin);
            //send priceResult to websocket
        }

    }
Posted
Updated 18-Jul-15 2:43am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900