-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It will one day be able to consolidate recent several events to notify users about into a single email. Right now it's an untested system.out.println(), but we'll get there.
- Loading branch information
Showing
6 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package utilities; | ||
import java.util.Set; | ||
|
||
import entities.User; | ||
|
||
|
||
public class NotificationQueue extends Thread{ | ||
// The purpose of this class is to help reduce notification spam. | ||
// Instead of firing off emails immediately, spawn a thread in this class which | ||
// waits patiently for a few minutes and then collects all relevent activity that happened | ||
// after the event that created it. Then send a single email summerizing it all. | ||
// Right now I'm using it when admins approve tickets. It may be usefull for notifications to admins as well. | ||
private String instructions=""; | ||
// this determines what information the notification queue will collect when it is finished waiting. | ||
// >'ticketConfirmations' will cause it to query approved tickets since initialization associated with 'employee' | ||
private User employee; | ||
public NotificationQueue(User employee, String initialize){ | ||
instructions=initialize; | ||
this.employee=employee; | ||
} | ||
public void run(){ | ||
try { | ||
String threadName = instructions+employee.getID(); | ||
// We don't want this to do anything if there is already a thread waiting to send a summary email | ||
// That would defeat the purpose of this class | ||
// So first we check active threads for one that matches threadName; which would be another thread that is | ||
// both assigned to the user in question, and monitoring the same activity. | ||
Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); | ||
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]); | ||
boolean match = false; | ||
for (int i=0; i<threadArray.length; i++) { | ||
if(threadArray[i].getName().equals(threadName)) match=true; | ||
} | ||
if(match){ | ||
return; //should kill thread | ||
} | ||
else{ | ||
// If there's not yet a queue of the type in question then go ahead and declare this thread as that type, | ||
// so similar notification activity in the future doesn't make a redundant thread. | ||
// Note: I know this is far from a proper mutex, but the only 'race condition' occurs when multiple people click a button | ||
// at the same time so I'm not too worried. Worst case someone gets 2 emails. | ||
Thread.currentThread().setName(threadName); | ||
// Finally do the thing we came for | ||
switch(instructions){ | ||
case "ticketConfirmations": | ||
startTicketConfirmQueue(employee); | ||
break; | ||
default: | ||
System.out.println("NotificationQueue: Did not recognize instruction string"); | ||
break; | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
private void startTicketConfirmQueue(User employee) throws InterruptedException{ | ||
Thread.sleep(10000); | ||
System.out.println(Thread.currentThread().getName()+" will send summary email now"); | ||
} | ||
} |