本文介绍了MimeMessage.saveChanges真的很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 由于包含 m.saveChanges(),以下测试需要大约5秒才能执行。The following test is taking around 5 seconds to execute due to the inclusion of 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;@Testpublic 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嘲笑会话但是没有帮助:I have also mocked the Session with mockito but it doesn't help:Session s = mock(Session.class);when(s.getProperties()).thenReturn(new Properties());这是什么问题?我可以模拟什么来加快速度?What is the problem here? What can I mock to speed things up?推荐答案修复人们在代码中使用JavaMail时最常犯的错误。 DNS查找会影响某些机器的性能。对于JDK,您可以更改用于缓存DNS查找的安全属性 networkaddress.cache.ttl 和 networkaddress.cache.negative.ttl 或设置系统属性 sun.net.inetaddr.ttl 和 sun.net.inetaddr.negative.ttl 。 JDK 7及更高版本中的默认行为可以很好地缓存。DNS lookup can hurt performance on some machines. For the JDK you can change the security properties for caching DNS lookup networkaddress.cache.ttl and networkaddress.cache.negative.ttl or set the system properties sun.net.inetaddr.ttl and sun.net.inetaddr.negative.ttl. The default behavior in JDK 7 and later does a good job of caching.最好,您可以使用会话属性来避免某些查找。Preferably, you can use the session properties to avoid some these lookups. 设置 mail.smtp.localhost 以防止在HELO命令上进行名称查找。 为 设置会话属性mail.from 或 mail.host (不是协议版本),因为这会阻止 InternetAddress。 getLocalAddress(会话) 。致电 MimeMessage .saveChanges() , MimeMessage.updateHeaders() , MimeMessage.updateMessageID() 或 MimeMessage.setFrom() 将触发此方法。 设置 mail.smtp.from 防止查找EHLO命令。 Altern您可以设置系统属性 mail.mime.address.usecanonicalhostname 到 false 如果您的代码依赖于 setFrom() 但这应该由第2点处理。 对于IMAP,您可以尝试设置 mail.imap.sasl.usecanonicalhostname 到 false 这是默认值。Set the session property for mail.smtp.localhost to prevent name lookup on the HELO command.Set session property for mail.from or mail.host (not the protocol versions) as that will prevent the name lookup on InternetAddress.getLocalAddress(Session). Calling MimeMessage.saveChanges(), MimeMessage.updateHeaders(), MimeMessage.updateMessageID(), or MimeMessage.setFrom()will trigger this method.Set session property for mail.smtp.from to prevent lookup on EHLO command.Alternatively, you can set the system property mail.mime.address.usecanonicalhostname to falseif your code is relying on the setFrom() but this should be handled by point #2.For IMAP, you can try to set mail.imap.sasl.usecanonicalhostname to false which is the default value.因为你没有运输一条消息,通过将代码更改为以下来应用规则#2:Since your are not transporting a message, apply rule #2 by changing your code to:@Testpublic 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地址。If you are transporting a message then combine the rules #1, #2, and #3 which will prevent accessing the host system for a name lookup. If you want to prevent all DNS lookups during a transport then you have to use IP addresses. 这篇关于MimeMessage.saveChanges真的很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 21:30