我正在向客户发送价格(10000+),但下面的代码有循环,导致客户等待计算的过程出现延迟。
PriceVisibleForCustomer = 价格 + CustomerMargin
价格 - 每 300 毫秒更改一次 - 从中央商店发送,与客户实例无关
CustomerMargn - 由客户协议/部门/管理员决定等产生的一些正负值。在客户 http 会话期间它不会改变,我可以将它保存在内存中
客户 - 他在登录后参与流程,他应该看到 8 种产品的价格快速变化。
也许我需要更多技术?我有 Spring 3/4、Java、Weblogic,我甚至可以为这个任务创建单独的 webapp 来提供计算价格。
我考虑过 Java 中的线程,但是 10000 多个客户意味着线程太多,不是吗?如何更改此代码?也许我应该改变架构,但如何?
/**
* 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
}
}