当前位置:网站首页>How to use buffer queue to realize high concurrent order business (glory Collection Edition)
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
2022-07-25 20:32:00 【Dragon back ride Shi】

Preface
Mainly in the project ( Small and medium-sized projects ) There is a payment order business ( Just deal with VIP, There's no inventory involved ), At present, the number of users has not come up , There is no problem at present , But think of if the number of users becomes larger , The number of concurrent orders becomes larger , There may be a series of problems , In my spare time , Did this demo Test related questions .
The possible problems are as follows :
The order is repeated
High and low , Slower performance
Solution :ThreadPoolExecutor Thread pool + Queue queue
1. First of all springBoot The framework of the project is as follows :

2. The classes involved in the business test process , as follows
BusinessThread class
package com.springboot.demo.Threads;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")//spring Multiple cases
public class BusinessThread implements Runnable{
private String acceptStr;
public BusinessThread(String acceptStr) {
this.acceptStr = acceptStr;
}
public String getAcceptStr() {
return acceptStr;
}
public void setAcceptStr(String acceptStr) {
this.acceptStr = acceptStr;
}
@Override
public void run() {
// Business operations
System.out.println(" Multithreading has processed the order insertion system , The order number :"+acceptStr);
// Thread blocking
/*try {
Thread.sleep(1000);
System.out.println(" Multithreading has processed the order insertion system , The order number :"+acceptStr);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
}- TestThreadPoolManager class
package com.springboot.demo.Threads;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.*;
@Component
public class TestThreadPoolManager implements BeanFactoryAware {
// For from IOC Take the object inside
private BeanFactory factory; // If you realize Runnable The class of is through spring Of application.xml File Injection , It can be done by factory.getBean() obtain , It's just a mention here
// Minimum number of threads maintained by thread pool
private final static int CORE_POOL_SIZE = 2;
// Maximum number of thread pool maintenance threads
private final static int MAX_POOL_SIZE = 10;
// The thread pool maintains the free time allowed for threads
private final static int KEEP_ALIVE_TIME = 0;
// The size of the buffer queue used by the thread pool
private final static int WORK_QUEUE_SIZE = 50;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
factory = beanFactory;
}
/**
* For orders stored in the queue , Prevent duplicate submissions , In the real world , You can use redis Instead of Duplicate verification
*/
Map<String, Object> cacheMap = new ConcurrentHashMap<>();
/**
* Buffer queue for orders , When the thread pool is full , Then put the order in this buffer queue
*/
Queue<Object> msgQueue = new LinkedBlockingQueue<Object>();
/**
* When the thread pool is full , Execute the following code , Put the order in the buffer queue
*/
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// Order added to buffer queue
msgQueue.offer(((BusinessThread) r).getAcceptStr());
System.out.println(" The system task is too busy , Give this order to ( Scheduling thread pool ) One by one processing , The order number :" + ((BusinessThread) r).getAcceptStr());
}
};
/** Creating a thread pool */
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE), this.handler);
/** Add tasks to the order thread pool */
public void addOrders(String orderId){
System.out.println(" This order is ready to be added to the thread pool , The order number :" + orderId);
// Verify that the currently entered order already exists
if (cacheMap.get(orderId) == null) {
cacheMap.put(orderId, new Object());
BusinessThread businessThread = new BusinessThread(orderId);
threadPool.execute(businessThread);
}
}
/**
* Scheduled tasks of thread pool ----> be called ( Scheduling thread pool ). This thread pool supports The need to perform tasks regularly and periodically .
*/
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
/**
* Check ( Scheduling thread pool ), Once a second , Check whether there is a buffer queue for the order Order records , Then rejoin the thread pool
*/
final ScheduledFuture scheduledFuture = scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// Determine whether the buffer queue has records
if(!msgQueue.isEmpty()){
// When the queue capacity of thread pool is less than WORK_QUEUE_SIZE, Then start buffering the queued orders Add to Thread pool
if (threadPool.getQueue().size() < WORK_QUEUE_SIZE) {
String orderId = (String) msgQueue.poll();
BusinessThread businessThread = new BusinessThread(orderId);
threadPool.execute(businessThread);
System.out.println("( Scheduling thread pool ) Order business appears in buffer queue , Re add to thread pool , The order number :"+orderId);
}
}
}
}, 0, 1, TimeUnit.SECONDS);
/** Get message buffer queue */
public Queue<Object> getMsgQueue() {
return msgQueue;
}
/** Terminate order thread pool + Scheduling thread pool */
public void shutdown() {
//true Indicates that if a scheduled task is executing , To suspend immediately ,false Then wait for the task to finish before stopping
System.out.println(" Terminate order thread pool + Scheduling thread pool :"+scheduledFuture.cancel(false));
scheduler.shutdown();
threadPool.shutdown();
}
}- TestController class
package com.springboot.demo;
import com.springboot.demo.Threads.TestThreadPoolManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Queue;
import java.util.UUID;
/**
* Created by Administrator on 2018/5/9.
*/
@RestController
public class TestController {
@Autowired
TestThreadPoolManager testThreadPoolManager;
/**
* Test simulation order request entrance
* @param id
* @return
*/
@GetMapping("/start/{id}")
public String start(@PathVariable Long id) {
// Simulated random number
String orderNo = System.currentTimeMillis() + UUID.randomUUID().toString();
testThreadPoolManager.addOrders(orderNo);
return "Test ThreadPoolExecutor start";
}
/**
* Out of Service
* @param id
* @return
*/
@GetMapping("/end/{id}")
public String end(@PathVariable Long id) {
testThreadPoolManager.shutdown();
Queue q = testThreadPoolManager.getMsgQueue();
System.out.println(" Thread service is turned off , And the number of unprocessed messages :" + q.size());
return "Test ThreadPoolExecutor start";
}
}3. Use JMeter Simulate concurrent order requests

4. result
Printed log description , The start order is executed directly into the system , When the thread pool is full , Then use RejectedExecutionHandler Method to add subsequent orders to Queue Buffer queue , Use ScheduledFuture Timing method ( I'm here once a second ) Check Queue queue , Add the orders in the queue to the thread pool again , Perform the Insert task later .
Some of the logs are as follows

边栏推荐
- 2022.7.24-----leetcode.1184
- 【高等数学】【3】微分中值定理与导数的应用
- 4. Server startup of source code analysis of Nacos configuration center
- Three skills of interface request merging, and the performance is directly exploded!
- [cloud native] use of Nacos taskmanager task management
- QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
- The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
- “链”接无限可能:数字资产链,精彩马上来!
- FanoutExchange交换机代码教程
- 「分享」DevExpress ASP.NET v22.1最新版本系统环境配置要求
猜你喜欢

Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework

Recommended books | essentials of industrial digital transformation: methods and Practice
![[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging](/img/e9/5751dc435cfbbefc22d84fd9ebbaea.png)
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

Why did I choose to become a network engineer after graduating from weak current for 3 months

【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签

Notes - record a cannotfinddatasourceexception: dynamic datasource can not find primary datasource problem solving

Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
![[paper reading] unpaired image to image translation using cycle consistent advantageous networks](/img/73/69651dd8ecfdddd1cae13a1d223d51.png)
[paper reading] unpaired image to image translation using cycle consistent advantageous networks

火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
随机推荐
Vivo official website app full model UI adaptation scheme
Behind every piece of information you collect, you can't live without TA
CarSim仿真快速入门(十四)—CarSim-Simulink联合仿真
Mobile web layout method
Dataframe first performs grouping operation and then combines output
Follow up of Arlo's thinking
String of sword finger offer question bank summary (II) (C language version)
【高等数学】【3】微分中值定理与导数的应用
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
Vulnhub | dc: 5 | [actual combat]
Docker 搭建 Redis Cluster集群
【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
Recommended system topic | Minet: cross domain CTR prediction
Network protocol: TCP part2
毕业从事弱电3个月,我为什么会选择转行网络工程师
智能电子界桩自然保护区远程监控解决方案
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
【高等数学】【5】定积分及应用
SecureCRT garbled code solution [easy to understand]