我怀疑我使用了错误的 Apache Common Email 库来发送带有附件的电子邮件。有时我会收到“打开的文件太多”错误,如果我用lsof检查打开的文件,我会看到附件文件被多次打开:
image1.png image2.png image1.png ...
所以我怀疑我没有正确释放文件(或没有关闭某些资源)。也可能是软件工作正常,但如果外部 SMTP 服务器失败(连接异常),我的代码不会释放一些资源。
来源:
/* Async Thread to send email */
Thread t = new Thread(){
@Override
public void run(){
try{
task(request,idNewsletter);
}
catch (Exception e){
log.error("Error " + e);
}
}
};
t.start();
[...]
任务方法:
ExecutorService executor = Executors.newFixedThreadPool(10);
[...]
List<Future<Integer>> list = new ArrayList<Future<Integer>>();
for (int i = 0; i < dests.length; i++) {
HtmlEmail htmlEmail = getHtmlMail([...]);
ArrayList<InternetAddress> dest = new ArrayList<InternetAddress>();
InternetAddress add = new InternetAddress();
add.setAddress(dests[i]);
dest.add(add);
htmlEmail.setBcc(dest);
htmlEmail.setMailSession(getEmailSession());
htmlEmail.buildMimeMessage();
Callable<Integer> worker = new EmailService(htmlEmail,i);
Future<Integer> future = executor.submit(worker);
list.add(future);
}
try {
executor.shutdown();
executor.awaitTermination(28800, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
log.error("Error=" + e);
}
finally {
if (!executor.isTerminated()) {
log.error("Task cancelled");
}
executor.shutdownNow();
}
紫衣仙女
相关分类