Skip to content

Commit

Permalink
read.me and the diagram is added
Browse files Browse the repository at this point in the history
  • Loading branch information
qpi committed Apr 22, 2017
1 parent dce767c commit 152b276
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
29 changes: 29 additions & 0 deletions event-queue/README.md
Original file line number Diff line number Diff line change
@@ -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]
26 changes: 26 additions & 0 deletions event-queue/etc/event-queue.urm.puml
Original file line number Diff line number Diff line change
@@ -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
Binary file added event-queue/etc/model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added event-queue/model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions event-queue/model.ucls
Original file line number Diff line number Diff line change
@@ -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>
9 changes: 2 additions & 7 deletions event-queue/src/main/java/com/iluwatar/event/queue/Audio.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -117,15 +113,14 @@ public static void update() {
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();
} catch (IOException e) {
System.err.println("Input/Output error while loading the audio");
e.printStackTrace();
}
clip.start();

headIndex++;
}
}

0 comments on commit 152b276

Please sign in to comment.