01: /*
02: * Created on 04.08.2005
03: *
04: * To change the template for this generated file go to
05: * Window - Preferences - Java - Code Generation - Code and Comments
06: */
07: package de.schlund.pfixcore.webservice.fault;
08:
09: import java.util.ArrayList;
10:
11: import org.apache.log4j.Logger;
12:
13: import de.schlund.pfixcore.util.email.EmailSender;
14: import de.schlund.pfixcore.util.email.EmailSenderException;
15:
16: public class EmailNotifier extends Thread {
17:
18: Logger LOG = Logger.getLogger(getClass().getName());
19:
20: private static EmailNotifier instance = new EmailNotifier();
21:
22: private NotifierThread thread;
23: ArrayList<Email> emailQueue;
24:
25: private EmailNotifier() {
26: emailQueue = new ArrayList<Email>();
27: thread = new NotifierThread();
28: thread.start();
29: }
30:
31: public static EmailNotifier getInstance() {
32: return instance;
33: }
34:
35: public void sendMail(String subject, String text,
36: String[] recipients, String sender, String smtpHost) {
37: Email email = new Email(subject, text, recipients, sender,
38: smtpHost);
39: synchronized (emailQueue) {
40: emailQueue.add(email);
41: emailQueue.notify();
42: }
43: }
44:
45: class Email {
46:
47: String subject;
48: String text;
49: String[] recipients;
50: String sender;
51: String smtpHost;
52:
53: Email(String subject, String text, String[] recipients,
54: String sender, String smtpHost) {
55: this .subject = subject;
56: this .text = text;
57: this .recipients = recipients;
58: this .sender = sender;
59: this .smtpHost = smtpHost;
60: }
61:
62: }
63:
64: class NotifierThread extends Thread {
65:
66: public void run() {
67: while (!isInterrupted()) {
68: Email email = null;
69: synchronized (emailQueue) {
70: if (emailQueue.isEmpty()) {
71: try {
72: emailQueue.wait();
73: } catch (InterruptedException x) {
74: }
75: }
76: email = (Email) emailQueue.remove(0);
77: }
78: try {
79: EmailSender.sendMail(email.subject, email.text,
80: email.recipients, email.sender,
81: email.smtpHost);
82: } catch (EmailSenderException x) {
83: LOG.error("Error while sending exception mail.", x);
84: }
85: }
86: }
87:
88: }
89:
90: }
|