Skip to content

Commit 152b276

Browse files
committed
read.me and the diagram is added
1 parent dce767c commit 152b276

6 files changed

Lines changed: 93 additions & 7 deletions

File tree

event-queue/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
layout: pattern
3+
title: Event Queue
4+
folder: event-queue
5+
permalink: /patterns/event-queue/
6+
categories: Concurrency
7+
tags:
8+
- Java
9+
- Difficulty Intermediate
10+
- Queue
11+
---
12+
13+
## Intent
14+
Event Queue is a good pattern if You have a limited accesibility resource (for example:
15+
Audio or Database), but You need to handle all the requests that want to use that.
16+
It puts all the requests in a queue and process them asynchronously.
17+
Gives the resource for the event when it is the next in the queue and in same time
18+
removes it from the queue.
19+
20+
![alt text](./etc/model.png "Event Queue")
21+
22+
## Applicability
23+
Use the Event Queue pattern when
24+
25+
* You have a limited accesibility resource and the asynchronous process is acceptable to reach that
26+
27+
## Credits
28+
29+
* [Mihály Kuprivecz - Event Queue]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@startuml
2+
package com.iluwatar.event.queue {
3+
class App {
4+
+ App()
5+
+ getAudioStream(filePath : String) : AudioInputStream {static}
6+
+ main(args : String[]) {static}
7+
}
8+
class Audio {
9+
- MAX_PENDING : int {static}
10+
- headIndex : int {static}
11+
- pendingAudio : PlayMessage[] {static}
12+
- tailIndex : int {static}
13+
- updateThread : Thread {static}
14+
+ Audio()
15+
+ init() {static}
16+
+ playSound(stream : AudioInputStream, volume : float) {static}
17+
+ stopService() {static}
18+
+ update() {static}
19+
}
20+
class PlayMessage {
21+
~ stream : AudioInputStream
22+
~ volume : float
23+
+ PlayMessage()
24+
}
25+
}
26+
@enduml

event-queue/etc/model.png

10.6 KB
Loading

event-queue/model.png

10.6 KB
Loading

event-queue/model.ucls

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.event.queue.Audio" project="event-queue"
5+
file="/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="285" y="179"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="com.iluwatar.event.queue.PlayMessage" project="event-queue"
14+
file="/event-queue/src/main/java/com/iluwatar/event/queue/PlayMessage.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="633" y="179"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<association id="3">
23+
<end type="SOURCE" refId="1" navigable="false">
24+
<attribute id="4" name="pendingAudio"/>
25+
<multiplicity id="5" minimum="0" maximum="2147483647"/>
26+
</end>
27+
<end type="TARGET" refId="2" navigable="true"/>
28+
<display labels="true" multiplicity="true"/>
29+
</association>
30+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
31+
sort-features="false" accessors="true" visibility="true">
32+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
33+
<operations public="true" package="true" protected="true" private="true" static="true"/>
34+
</classifier-display>
35+
<association-display labels="true" multiplicity="true"/>
36+
</class-diagram>

event-queue/src/main/java/com/iluwatar/event/queue/Audio.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ public class Audio {
4747

4848
private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
4949

50-
public static boolean isServiceRunning() {
51-
return updateThread.isAlive();
52-
}
53-
5450
/**
5551
* This method stops the Update Method's thread.
5652
*/
@@ -117,15 +113,14 @@ public static void update() {
117113
try {
118114
clip = AudioSystem.getClip();
119115
clip.open(pendingAudio[headIndex].stream);
116+
clip.start();
117+
headIndex++;
120118
} catch (LineUnavailableException e) {
121119
System.err.println("Error occoured while loading the audio: The line is unavailable");
122120
e.printStackTrace();
123121
} catch (IOException e) {
124122
System.err.println("Input/Output error while loading the audio");
125123
e.printStackTrace();
126124
}
127-
clip.start();
128-
129-
headIndex++;
130125
}
131126
}

0 commit comments

Comments
 (0)