有没有办法提高从 Javamail IMAP 服务器获取消息的性能? 我找到了一些技巧,但无法提高性能。 此方法将从文件夹中获取大约 20 条消息,并将格式化的消息返回到 DTO,我尝试注释行以获取附件,并获取收件人,但它不会改变性能中的任何内容。
/**
* Get all the emails that are inside the param folder
*
* @param folderId folder to return the emails from the informed folder.
* @param pageToken pageToken to be used as pagination
* @param maxResults maxResults to create a limit to the number of registers
* @return {@link EmailsDTO} with all the messages
*/
public EmailsDTO getMessages(final String folderId, final String pageToken, final Integer maxResults) {
final Properties properties = getServerInputProperties();
final Session session = Session.getDefaultInstance(properties);
try {
final EmailsDTO emailsDTO = new EmailsDTO();
final List<EmailDTO> emailDTOList = new ArrayList<>();
// connects to the message store
final Store store = session.getStore(IMAP);
store.connect(email, password);
// opens the folder to search the messages
final Folder folder = store.getFolder(folderId);
folder.open(Folder.READ_ONLY);
//set a pagination used to get the results in JavaMail
final int pageStart = nonNull(pageToken) ? Integer.valueOf(pageToken) : 1;
final int pageEnd = (pageStart + maxResults) > folder.getMessageCount() ? folder.getMessageCount() : pageStart + maxResults;
// fetches new messages from server, starts with 1
final Message[] messages = folder.getMessages(pageStart, pageEnd);
final FetchProfile fetchProfile = new FetchProfile();
fetchProfile.add(FetchProfile.Item.ENVELOPE);
folder.fetch(messages, fetchProfile); // Load the profile of the messages in 1 fetch.
for (final Message msg : messages) {
final EmailDTO emailDTO = new EmailDTO();
emailDTO.setId(msg.getHeader(MESSAGE_ID)[0]);
emailDTO.setCc(this.parseAddresses(msg.getRecipients(Message.RecipientType.CC)));
emailDTO.setTo(this.parseAddresses(msg.getRecipients(Message.RecipientType.TO)));
emailDTO.setBcc(this.parseAddresses(msg.getRecipients(Message.RecipientType.BCC)));
emailDTO.setFrom(this.getFrom(msg.getFrom()));
emailDTO.setRead(msg.getFlags().contains(Flags.Flag.SEEN));
emailDTO.setHasAttachments(this.hasAttachments(msg));
emailDTO.setSubject(msg.getSubject());
emailDTO.setMessage(this.getMessageContent(msg));
emailDTO.setAttachments(this.getAttachements(msg));
emailDTO.setDate(msg.getReceivedDate().toString());
emailDTO.setParentFolder(msg.getFolder().getName());
//TODO: see what need to put here emailDTO.setLabels();
emailDTOList.add(emailDTO);
}
// disconnect
folder.close(false);
store.close();
emailsDTO.setEstimatedNumberOfMessages(folder.getMessageCount());
emailsDTO.setNextPageToken(String.valueOf(emailDTOList.size()));
emailsDTO.setMessages(emailDTOList);
return emailsDTO;
} catch (final MessagingException | IOException e) {
throw new RuntimeException(String.format("couldn't get messages from the folder with id: %s", folderId), e);
}
}
在
for (final Message msg : messages)
中,将 messages
更改为 folder.getMessages()
。
messages
-> 多次加载
folder.getMessages()
-> 在一次 fetch 中加载,因为您在其中使用了 fetchProfile。
我不同意@Rose的回答。获取消息[]就可以了。