当前位置:网站首页>Interviewers often ask: how to set up a "message queue" and "delayed message queue"?
Interviewers often ask: how to set up a "message queue" and "delayed message queue"?
2022-07-27 13:37:00 【InfoQ】
background
The key analysis
- A message that you are familiar with ?
- How to implement message queuing manually ?
Knowledge expansion
- Java and Spring
- .NET
- Ruby
- Python
- PHP
- JavaScript and Node
- Objective-C and Swift
- Rust
- Scala
- Go
- producer : The creator of the message , Responsible for creating and pushing data to the message server .
- consumer : The receiver of the message , Used to process data and confirm messages .
- agent : That is to say RabbitMQ

- Support persistence ,RabbitMQ Support disk persistence function , It ensures that messages are not lost ;
- High concurrency ,RabbitMQ Used Erlang development language ,Erlang It's a language developed for telephone switches , It is naturally equipped with high and luminous ring and high availability features ;
- Support distributed cluster , Precisely because Erlang The realization of language , therefore RabbitMQ Cluster deployment is also very simple , Just start each node and use --link Add the node to the cluster , also RabbitMQ Support automatic primary selection and automatic disaster recovery ;
- Support for multiple languages , such as Java、.NET、PHP、Python、JavaScript、Ruby、Go etc. ;
- Support message confirmation , Support message consumption confirmation (ack) It ensures that each message can be consumed normally ;
- It supports many plug-ins , For example, web console message management plug-in 、 Message delay plug-ins, etc ,RabbitMQ There are many plug-ins and they are very convenient to use .
- direct( Default type ) Pattern , This mode is one-to-one , That is, a message will only be sent to one consumer ;
- headers Pattern , Allows you to match messages header Instead of routing keys (RoutingKey), besides headers and direct
- The use of is exactly the same , But because headers The matching performance is very poor , Hardly used ;
- fanout Pattern , For multicast , A message will be distributed to all subscribers ;
- topic Pattern , Subscribe to the theme mode , Wildcards are allowed (#、*) Match one or more messages , I can use “cn.mq.#” Matching to multiple prefixes is “cn.mq.xxx” The news of , For example, you can match “cn.mq.rabbit”、“cn.mq.kafka” Wait for news .
- deque (Deque) yes Queue So are subclasses of Queue Supplementary category of , Both the head and tail support element insertion and acquisition ;
- Blocking queue refers to the operation of elements ( Add or remove ), If it doesn't work , Will block waiting for execution , For example, when adding elements , If the queue element is full , The queue will block and wait until there is an empty space before inserting ;
- Non blocking queues , As opposed to blocking queues , It will directly return the result of the operation , Instead of blocking wait operations , Double ended queues are also non blocking queues .
import java.util.LinkedList;import java.util.Queue;public class CustomQueue { // Define message queues private static Queue<String> queue = new LinkedList<>(); public static void main(String[] args) { producer(); // Call producer consumer(); // Call consumer } // producer public static void producer() { // Add message queue.add("first message."); queue.add("second message."); queue.add("third message."); } // consumer public static void consumer() { while (!queue.isEmpty()) { // News consumption System.out.println(queue.poll()); } }}first message.second message.third message.import lombok.Getter;import lombok.Setter;import java.text.DateFormat;import java.util.Date;import java.util.concurrent.DelayQueue;import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;/** * Custom delay queue */public class CustomDelayQueue { // Delayed message queuing private static DelayQueue delayQueue = new DelayQueue(); public static void main(String[] args) throws InterruptedException { producer(); // Call producer consumer(); // Call consumer } // producer public static void producer() { // Add message delayQueue.put(new MyDelay(1000, " news 1")); delayQueue.put(new MyDelay(3000, " news 2")); } // consumer public static void consumer() throws InterruptedException { System.out.println(" Start execution time :" + DateFormat.getDateTimeInstance().format(new Date())); while (!delayQueue.isEmpty()) { System.out.println(delayQueue.take()); } System.out.println(" End execution time :" + DateFormat.getDateTimeInstance().format(new Date())); } /** * Custom delay queue */ static class MyDelay implements Delayed { // Delay deadline ( Company : millisecond ) long delayTime = System.currentTimeMillis(); // With the help of lombok Realization @Getter @Setter private String msg; /** * initialization * @param delayTime Set the delay execution time * @param msg Messages executed */ public MyDelay(long delayTime, String msg) { this.delayTime = (this.delayTime + delayTime); this.msg = msg; } // Get the time left @Override public long getDelay(TimeUnit unit) { return unit.convert(delayTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } // The elements in the queue are sorted by @Override public int compareTo(Delayed o) { if (this.getDelay(TimeUnit.MILLISECONDS) > o.getDelay(TimeUnit.MILLISECONDS)) { return 1; } else if (this.getDelay(TimeUnit.MILLISECONDS) < o.getDelay(TimeUnit.MILLISECONDS)) { return -1; } else { return 0; } } @Override public String toString() { return this.msg; } }} Start execution time :2020-4-2 16:17:28 news 1 news 2 End execution time :2020-4-2 16:17:31summary
边栏推荐
猜你喜欢

Li Hang, director of ByteDance AI Lab: past, present and future of language model

3D laser slam:aloam---ceres optimization part and code analysis

如何调试JNI程序

Interface testing practical tutorial 01: interface testing environment construction

Realize the disk partition and file system mount of the newly added hard disk

SCI thesis writing

Tencent cloud and the China Federation of industry released the research results of industrial AI quality inspection standardization to accelerate the intelligent transformation of manufacturing indus

实现新增加硬盘的磁盘分区和文件系统挂载

Have you understood these 30 questions of enabling financial risk control plus points

Will saffron become a safe and effective natural therapy for patients with arthritis?
随机推荐
@Simple use of conditional
W3school navigation bar exercise
Verilog's system tasks - $fopen, $fclose and $fddisplay, $fwrite, $fstrobe, $fmonitor
Fiddler抓包工具+夜神模拟器
基于frp实现内网穿透——借助公网服务器实现ssh远程连接内网服务器
libevent 之 evconnlistener_new_bind
MTK6765编译环境搭建
Height collapse and BFC
How to pass parameters in JNI program
使用碳刷的注意事项有哪些
Bank case | ZABBIX cross version upgrade guide, isn't 4.2-6.0 popular?
责任链模式在转转精准估价中的应用
力扣 1480. 一维数组的动态和 383. 赎金信412. Fizz Buzz
如何调试JNI程序
SQL GROUP BY语句
面试官常问:如何手撸一个“消息队列”和“延迟消息队列”?
Evconnlistener of libevent_ new_ bind
js基础知识整理之 —— 数组
From the perspective of it, the CIO of B2B industry talks about how to change from "cost center" to "growth center"?
Training in the second week of summer vacation on July 24, 2022