下载邮件增加锁避免异步线程导致邮件内容下载串文件
关闭文件输出流
This commit is contained in:
parent
a8c32e43bd
commit
99f74539d2
|
@ -66,6 +66,8 @@ public class EmailServiceManager {
|
||||||
|
|
||||||
private RedisUtil redisUtil;
|
private RedisUtil redisUtil;
|
||||||
|
|
||||||
|
private Object downloadEmlLocal = new Object();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static EmailServiceManager getInstance(){
|
public static EmailServiceManager getInstance(){
|
||||||
return new EmailServiceManager();
|
return new EmailServiceManager();
|
||||||
|
@ -525,10 +527,12 @@ public class EmailServiceManager {
|
||||||
* 当计数大于10000后从0开始,服务重启后也从0开始
|
* 当计数大于10000后从0开始,服务重启后也从0开始
|
||||||
*/
|
*/
|
||||||
public File downloadEmailToEmlDir(@NotNull Message message,Integer emailCounter,Integer batchesCounter) throws MessagingException {
|
public File downloadEmailToEmlDir(@NotNull Message message,Integer emailCounter,Integer batchesCounter) throws MessagingException {
|
||||||
|
synchronized (downloadEmlLocal) {
|
||||||
String subject = "";
|
String subject = "";
|
||||||
File emlFile = null;
|
File emlFile = null;
|
||||||
String status = EmailLogManager.STATUS_SUCCESS;
|
String status = EmailLogManager.STATUS_SUCCESS;
|
||||||
Date receivedDate = null;
|
Date receivedDate = null;
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
try {
|
try {
|
||||||
//获取发件人
|
//获取发件人
|
||||||
final String address = ((InternetAddress) message.getFrom()[0]).getAddress();
|
final String address = ((InternetAddress) message.getFrom()[0]).getAddress();
|
||||||
|
@ -562,7 +566,8 @@ public class EmailServiceManager {
|
||||||
final String rootPath = spectrumPathProperties.getRootPath();
|
final String rootPath = spectrumPathProperties.getRootPath();
|
||||||
final String emlPath = spectrumPathProperties.getEmlPath();
|
final String emlPath = spectrumPathProperties.getEmlPath();
|
||||||
emlFile = new File(rootPath+emlPath+File.separator+fileName);
|
emlFile = new File(rootPath+emlPath+File.separator+fileName);
|
||||||
message.writeTo(new FileOutputStream(emlFile));
|
outputStream = new FileOutputStream(emlFile);
|
||||||
|
message.writeTo(outputStream);
|
||||||
|
|
||||||
// int bufferSize = 1024 * 1024; // 1M
|
// int bufferSize = 1024 * 1024; // 1M
|
||||||
// InputStream inputStream = message.getInputStream();
|
// InputStream inputStream = message.getInputStream();
|
||||||
|
@ -592,9 +597,17 @@ public class EmailServiceManager {
|
||||||
EmailLogEvent event = new EmailLogEvent(batchesCounter,Thread.currentThread().getId(),EmailLogManager.GS_TYPE_GET,status,EmailLogManager.GETIDEML,subject,DateUtils.formatDate(receivedDate,"yyyy-MM-dd HH:mm:ss:SSS"),
|
EmailLogEvent event = new EmailLogEvent(batchesCounter,Thread.currentThread().getId(),EmailLogManager.GS_TYPE_GET,status,EmailLogManager.GETIDEML,subject,DateUtils.formatDate(receivedDate,"yyyy-MM-dd HH:mm:ss:SSS"),
|
||||||
(Objects.isNull(emlFile)?" ":emlFile.getAbsolutePath()));
|
(Objects.isNull(emlFile)?" ":emlFile.getAbsolutePath()));
|
||||||
EmailLogManager.getInstance().offer(Thread.currentThread().getId(),event);
|
EmailLogManager.getInstance().offer(Thread.currentThread().getId(),event);
|
||||||
|
try {
|
||||||
|
if (Objects.nonNull(outputStream)) {
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return emlFile;
|
return emlFile;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除邮件
|
* 删除邮件
|
||||||
|
@ -656,7 +669,7 @@ public class EmailServiceManager {
|
||||||
String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId;
|
String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId;
|
||||||
int numberKey = redisUtil.get(key) != null? (int) redisUtil.get(key):0;
|
int numberKey = redisUtil.get(key) != null? (int) redisUtil.get(key):0;
|
||||||
// exist = redisUtil.hasKey(key);
|
// exist = redisUtil.hasKey(key);
|
||||||
if(numberKey > taskProperties.getForceDeletedNumber()){
|
if(numberKey >= taskProperties.getForceDeletedNumber()){
|
||||||
exist = true;
|
exist = true;
|
||||||
log.info("Check: Remove Email:{},receiveTime:{}",message.getSubject(), DateUtils.formatDate(message.getReceivedDate(),"yyyy-MM-dd HH:mm:ss"));
|
log.info("Check: Remove Email:{},receiveTime:{}",message.getSubject(), DateUtils.formatDate(message.getReceivedDate(),"yyyy-MM-dd HH:mm:ss"));
|
||||||
message.setFlag(Flags.Flag.DELETED,true);
|
message.setFlag(Flags.Flag.DELETED,true);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user