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