You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: priority-queue/README.md
+188-4
Original file line number
Diff line number
Diff line change
@@ -10,19 +10,203 @@ tags:
10
10
---
11
11
12
12
## Intent
13
-
Prioritize requests sent to services so that requests with a higher priority are received and processed more quickly than those of a lower priority. This pattern is useful in applications that offer different service level guarantees to individual clients.
13
+
14
+
Prioritize requests sent to services so that requests with a higher priority are received and
15
+
processed more quickly than those of a lower priority. This pattern is useful in applications that
16
+
offer different service level guarantees to individual clients.
14
17
15
18
## Explanation
16
-
Applications may delegate specific tasks to other services; for example, to perform background processing or to integrate with other applications or services. In the cloud, a message queue is typically used to delegate tasks to background processing. In many cases the order in which requests are received by a service is not important. However, in some cases it may be necessary to prioritize specific requests. These requests should be processed earlier than others of a lower priority that may have been sent previously by the application.
19
+
20
+
Applications may delegate specific tasks to other services; for example, to perform background
21
+
processing or to integrate with other applications or services. In the cloud, a message queue is
22
+
typically used to delegate tasks to background processing. In many cases the order in which requests
23
+
are received by a service is not important. However, in some cases it may be necessary to prioritize
24
+
specific requests. These requests should be processed earlier than others of a lower priority that
25
+
may have been sent previously by the application.
26
+
27
+
Real world example
28
+
29
+
> Imagine a video processing service with free and premium customers. The requests coming from the
30
+
> paying premium customers should be prioritized over the others.
31
+
32
+
In plain words
33
+
34
+
> Priority Queue enables processing of high priority messages first, regardless of queue size or
35
+
> message age.
36
+
37
+
Wikipedia says
38
+
39
+
> In computer science, a priority queue is an abstract data type similar to regular queue or stack
40
+
> data structure in which each element additionally has a "priority" associated with it. In a
41
+
> priority queue, an element with high priority is served before an element with low priority.
42
+
43
+
**Programmatic Example**
44
+
45
+
Looking at the video processing example from above, let's first see the `Message` structure.
46
+
47
+
```java
48
+
publicclassMessageimplementsComparable<Message> {
49
+
50
+
privatefinalString message;
51
+
privatefinalint priority; // define message priority in queue
52
+
53
+
publicMessage(Stringmessage, intpriority) {
54
+
this.message = message;
55
+
this.priority = priority;
56
+
}
57
+
58
+
@Override
59
+
publicintcompareTo(Messageo) {
60
+
return priority - o.priority;
61
+
}
62
+
...
63
+
}
64
+
```
65
+
66
+
Here's `PriorityMessageQueue` that handles storing the messages and serving them in priority
0 commit comments