diff --git a/event-queue/README.md b/event-queue/README.md index e69de29bb..2129f8c69 100644 --- a/event-queue/README.md +++ b/event-queue/README.md @@ -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. + +![alt text](./etc/model.png "Event 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] diff --git a/event-queue/etc/event-queue.urm.puml b/event-queue/etc/event-queue.urm.puml new file mode 100644 index 000000000..e2aabee31 --- /dev/null +++ b/event-queue/etc/event-queue.urm.puml @@ -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 \ No newline at end of file diff --git a/event-queue/etc/model.png b/event-queue/etc/model.png new file mode 100644 index 000000000..8222dccbf Binary files /dev/null and b/event-queue/etc/model.png differ diff --git a/event-queue/model.png b/event-queue/model.png new file mode 100644 index 000000000..8222dccbf Binary files /dev/null and b/event-queue/model.png differ diff --git a/event-queue/model.ucls b/event-queue/model.ucls new file mode 100644 index 000000000..ed923014b --- /dev/null +++ b/event-queue/model.ucls @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java b/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java index a9f0c5a67..e00cd375a 100644 --- a/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java +++ b/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java @@ -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++; } }