1
2
3
4
5
6
7
8 package com.atlassian.mail.queue;
9
10 import com.atlassian.mail.MailException;
11 import org.apache.commons.collections.Buffer;
12 import org.apache.commons.collections.BufferUtils;
13 import org.apache.commons.collections.UnboundedFifoBuffer;
14 import org.apache.log4j.Category;
15
16 import java.sql.Timestamp;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Iterator;
20
21
22
23
24
25
26 public class MailQueueImpl implements MailQueue
27 {
28 private static final Category log = Category.getInstance(MailQueueImpl.class);
29 private Buffer items;
30 private Buffer errorItems;
31 private boolean sending;
32 private MailQueueItem itemBeingSent;
33 private Timestamp sendingStarted;
34
35 public MailQueueImpl()
36 {
37 items = BufferUtils.synchronizedBuffer(new UnboundedFifoBuffer());
38 errorItems = BufferUtils.synchronizedBuffer(new UnboundedFifoBuffer());
39 }
40
41 public void sendBuffer()
42 {
43 if (sending)
44 {
45 log.warn("Already sending "+items.size()+" mails:");
46 Iterator iter = items.iterator();
47 while (iter.hasNext()) {
48 Object o = iter.next();
49 log.warn("Sending: "+o+", "+o.getClass());
50 }
51 return;
52 }
53
54 sendingStarted();
55 List failed = new ArrayList();
56
57 try
58 {
59 while (!items.isEmpty())
60 {
61 String origThreadName = Thread.currentThread().getName();
62 MailQueueItem item = (MailQueueItem) items.remove();
63 this.itemBeingSent = item;
64 log.debug("Sending: " + item);
65 try
66 {
67 Thread.currentThread().setName("Sending mailitem "+item);
68 item.send();
69 }
70 catch (MailException e)
71 {
72 if (item.getSendCount() > 10)
73 errorItems.add(item);
74 else
75 failed.add(item);
76
77 log.error("Error occurred in sending e-mail: " + item, e);
78 }
79 finally
80 {
81 Thread.currentThread().setName(origThreadName);
82 }
83 }
84
85 items.addAll(failed);
86 }
87 finally
88 {
89
90 sendingStopped();
91 }
92 }
93
94 public int size()
95 {
96 return items.size();
97 }
98
99 public int errorSize()
100 {
101 return errorItems.size();
102 }
103
104 public void addItem(MailQueueItem item)
105 {
106 log.debug("Queued: " + item);
107 items.add(item);
108 }
109
110 public void addErrorItem(MailQueueItem item)
111 {
112 log.debug("Queued error: " + item);
113 errorItems.add(item);
114 }
115
116 public Buffer getQueue()
117 {
118 return items;
119 }
120
121 public Buffer getErrorQueue()
122 {
123 return errorItems;
124 }
125
126 public boolean isSending()
127 {
128 return sending;
129 }
130
131 public Timestamp getSendingStarted()
132 {
133 return sendingStarted;
134 }
135
136 public MailQueueItem getItemBeingSent()
137 {
138 return itemBeingSent;
139 }
140
141 public void unstickQueue() {
142 log.error("Mail on queue was considered stuck: " + itemBeingSent);
143 sendingStopped();
144 }
145
146 public void emptyErrorQueue()
147 {
148 errorItems.clear();
149 }
150
151 public void resendErrorQueue()
152 {
153 items.addAll(errorItems);
154 emptyErrorQueue();
155 }
156
157 public void sendingStarted()
158 {
159 sending = true;
160 sendingStarted = new Timestamp(System.currentTimeMillis());
161 }
162
163 public void sendingStopped()
164 {
165 sending = false;
166 sendingStarted = null;
167 }
168 }