\r
import java.util.ArrayList;\r
import java.util.Arrays;\r
-import java.util.Collections;\r
import java.util.Date;\r
import java.util.HashSet;\r
import java.util.List;\r
\r
import javax.mail.Authenticator;\r
import javax.mail.Message;\r
-import javax.mail.Message.RecipientType;\r
import javax.mail.PasswordAuthentication;\r
import javax.mail.Session;\r
import javax.mail.Transport;\r
\r
private final Queue<Message> queue = new ConcurrentLinkedQueue<Message>();\r
\r
- private final Set<Message> failures = Collections.synchronizedSet(new HashSet<Message>());\r
-\r
private final Session session;\r
\r
private final IStoredSettings settings;\r
if (!queue.isEmpty()) {\r
if (session != null) {\r
// send message via mail server\r
+ List<Message> failures = new ArrayList<Message>();\r
Message message = null;\r
- while ((message = queue.peek()) != null) {\r
+ while ((message = queue.poll()) != null) {\r
try {\r
if (settings.getBoolean(Keys.mail.debug, false)) {\r
- logger.info("send: "\r
- + StringUtils.trimString(\r
- message.getSubject()\r
- + " => "\r
- + message.getRecipients(RecipientType.TO)[0]\r
- .toString(), 60));\r
+ logger.info("send: " + StringUtils.trimString(message.getSubject(), 60));\r
}\r
Transport.send(message);\r
- queue.remove();\r
- failures.remove(message);\r
- } catch (Throwable e) {\r
- if (!failures.contains(message)) {\r
- logger.error("Failed to send message", e);\r
- failures.add(message);\r
- }\r
- }\r
- }\r
- }\r
- } else {\r
- // log message to console and drop\r
- if (!queue.isEmpty()) {\r
- Message message = null;\r
- while ((message = queue.peek()) != null) {\r
- try {\r
- logger.info("drop: "\r
- + StringUtils.trimString(\r
- (message.getSubject())\r
- + " => "\r
- + message.getRecipients(RecipientType.TO)[0]\r
- .toString(), 60));\r
- queue.remove();\r
- failures.remove(message);\r
} catch (Throwable e) {\r
- if (!failures.contains(message)) {\r
- logger.error("Failed to remove message from queue");\r
- failures.add(message);\r
- }\r
+ logger.error("Failed to send message", e);\r
+ failures.add(message);\r
}\r
}\r
+ \r
+ // push the failures back onto the queue for the next cycle\r
+ queue.addAll(failures);\r
}\r
}\r
}\r