read.me and the diagram is added
This commit is contained in:
parent
dce767c1c5
commit
152b2762c3
@ -0,0 +1,29 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Event Queue
|
||||
folder: event-queue
|
||||
permalink: /patterns/event-queue/
|
||||
categories: Concurrency
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty Intermediate
|
||||
- Queue
|
||||
---
|
||||
|
||||
## Intent
|
||||
Event Queue is a good pattern if You have a limited accesibility resource (for example:
|
||||
Audio or Database), but You need to handle all the requests that want to use that.
|
||||
It puts all the requests in a queue and process them asynchronously.
|
||||
Gives the resource for the event when it is the next in the queue and in same time
|
||||
removes it from the queue.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the Event Queue pattern when
|
||||
|
||||
* You have a limited accesibility resource and the asynchronous process is acceptable to reach that
|
||||
|
||||
## Credits
|
||||
|
||||
* [Mihály Kuprivecz - Event Queue]
|
26
event-queue/etc/event-queue.urm.puml
Normal file
26
event-queue/etc/event-queue.urm.puml
Normal file
@ -0,0 +1,26 @@
|
||||
@startuml
|
||||
package com.iluwatar.event.queue {
|
||||
class App {
|
||||
+ App()
|
||||
+ getAudioStream(filePath : String) : AudioInputStream {static}
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class Audio {
|
||||
- MAX_PENDING : int {static}
|
||||
- headIndex : int {static}
|
||||
- pendingAudio : PlayMessage[] {static}
|
||||
- tailIndex : int {static}
|
||||
- updateThread : Thread {static}
|
||||
+ Audio()
|
||||
+ init() {static}
|
||||
+ playSound(stream : AudioInputStream, volume : float) {static}
|
||||
+ stopService() {static}
|
||||
+ update() {static}
|
||||
}
|
||||
class PlayMessage {
|
||||
~ stream : AudioInputStream
|
||||
~ volume : float
|
||||
+ PlayMessage()
|
||||
}
|
||||
}
|
||||
@enduml
|
BIN
event-queue/etc/model.png
Normal file
BIN
event-queue/etc/model.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
BIN
event-queue/model.png
Normal file
BIN
event-queue/model.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
36
event-queue/model.ucls
Normal file
36
event-queue/model.ucls
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<class id="1" language="java" name="com.iluwatar.event.queue.Audio" project="event-queue"
|
||||
file="/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="285" y="179"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.event.queue.PlayMessage" project="event-queue"
|
||||
file="/event-queue/src/main/java/com/iluwatar/event/queue/PlayMessage.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="633" y="179"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="3">
|
||||
<end type="SOURCE" refId="1" navigable="false">
|
||||
<attribute id="4" name="pendingAudio"/>
|
||||
<multiplicity id="5" minimum="0" maximum="2147483647"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -47,10 +47,6 @@ public class Audio {
|
||||
|
||||
private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
|
||||
|
||||
public static boolean isServiceRunning() {
|
||||
return updateThread.isAlive();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method stops the Update Method's thread.
|
||||
*/
|
||||
@ -117,6 +113,8 @@ public class Audio {
|
||||
try {
|
||||
clip = AudioSystem.getClip();
|
||||
clip.open(pendingAudio[headIndex].stream);
|
||||
clip.start();
|
||||
headIndex++;
|
||||
} catch (LineUnavailableException e) {
|
||||
System.err.println("Error occoured while loading the audio: The line is unavailable");
|
||||
e.printStackTrace();
|
||||
@ -124,8 +122,5 @@ public class Audio {
|
||||
System.err.println("Input/Output error while loading the audio");
|
||||
e.printStackTrace();
|
||||
}
|
||||
clip.start();
|
||||
|
||||
headIndex++;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user