由于包含以下内容,以下测试的执行时间约为 5 秒m.saveChanges()
.
import org.junit.Before;
import org.junit.Test;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Test
public void test1() throws MessagingException, IOException {
Session s = Session.getDefaultInstance(new Properties());
MimeMessage m = new MimeMessage(s);
m.setContent("<b>Hello</b>", "text/html; charset=utf-8");
m.saveChanges();
assertEquals(m.getContent(), "<b>Hello</b>");
assertEquals(m.getContentType(), "text/html; charset=utf-8");
}
我还用mockito嘲笑了Session,但这没有帮助:
Session s = mock(Session.class);
when(s.getProperties()).thenReturn(new Properties());
这里有什么问题?我可以模拟什么来加快速度?
Fix the 人们在使用 JavaMail 时最常犯的错误 https://javaee.github.io/javamail/FAQ#commonmistakes首先在你的代码中。
DNS查询 https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html#getCanonicalHostName--可能会损害某些机器的性能。对于 JDK,您可以更改缓存 DNS 查找的安全属性networkaddress.cache.ttl and networkaddress.cache.negative.ttl https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html或设置系统属性sun.net.inetaddr.ttl and sun.net.inetaddr.negative.ttl https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html。 JDK 7 及更高版本中的默认行为可以很好地进行缓存,因此您不必更改这些设置。
最好,您可以使用会话属性来避免某些此类查找。
- 添加会话属性mail.from or mail.host https://javaee.github.io/javamail/docs/api/(这些确切的键名称),因为任一属性都会阻止在其中进行名称查找InternetAddress.getLocalAddress(Session) https://javaee.github.io/javamail/docs/api/javax/mail/internet/InternetAddress.html#getLocalAddress-javax.mail.Session-。这些属性可以在其对应属性的基础上进行设置,也可以不进行设置,这些属性在键名中包含协议(稍后会详细介绍)。这些属性将提高性能MimeMessage.saveChanges() https://javaee.github.io/javamail/docs/api/javax/mail/internet/MimeMessage.html#saveChanges--, MimeMessage.updateHeaders() https://javaee.github.io/javamail/docs/api/javax/mail/internet/MimeMessage.html#updateHeaders--, MimeMessage.updateMessageID() https://javaee.github.io/javamail/docs/api/javax/mail/internet/MimeMessage.html#updateMessageID--, and MimeMessage.setFrom() https://javaee.github.io/javamail/docs/api/javax/mail/internet/MimeMessage.html#setFrom--都叫
InternetAddress::getLocalAddress
。如果未设置上述属性,则此方法将尝试查询主机名。通过设置属性,此方法将从会话中提取主机名字符串,而不是尝试昂贵的 DNS 查找。
- 设置会话属性mail.smtp.localhost or mail.smtps.localhost https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html以防止在传输时使用 HELO 命令进行名称查找。
- 设置会话属性mail.smtp.from or mail.smtps.from https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/SMTPTransport.html#ehlo-java.lang.String-以防止在传输时查找 EHLO 命令。
- 或者,您可以设置系统属性mail.mime.address.usecanonicalhostname https://javaee.github.io/javamail/docs/api/javax/mail/internet/package-summary.html to
false
如果你的代码依赖于setFrom() https://javaee.github.io/javamail/docs/api/javax/mail/internet/MimeMessage.html#setFrom--但如果您应用了第 1 点,就会处理这个问题。
- 对于 IMAP,您可以尝试设置mail.imap.sasl.usecanonicalhostname or mail.imaps.sasl.usecanonicalhostname https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html to
false
这是默认值。
由于您没有传输消息,因此请通过将代码更改为以下内容来应用规则#1:
@Test
public void test1() throws MessagingException, IOException {
Properties props = new Properties();
props.put("mail.host", "localhost"); //Or use IP.
Session s = Session.getInstance(props);
MimeMessage m = new MimeMessage(s);
m.setContent("<b>Hello</b>", "text/html; charset=utf-8");
m.saveChanges();
assertEquals(m.getContent(), "<b>Hello</b>");
assertEquals(m.getContentType(), "text/html; charset=utf-8");
}
如果您正在传输消息,则结合规则#1、#2 和#3,这将阻止访问主机系统进行名称查找。如果您想阻止传输期间的所有 DNS 查找,则必须使用 IP 地址。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)