MimeMessage.saveChanges真的很慢 [英] MimeMessage.saveChanges is really slow

查看:1063
本文介绍了MimeMessage.saveChanges真的很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于包含 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;

@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嘲笑会话但是没有帮助:

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 或设置系统属性< a href =https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html\"rel =nofollow noreferrer> 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.


  1. 设置 mail.smtp.localhost 以防止在HELO命令上进行名称查找。

  2. 设置会话属性mail.from mail.host (不是协议版本),因为这会阻止 InternetAddress。 getLocalAddress(会话) 。致电 MimeMessage .saveChanges() MimeMessage.updateHeaders() MimeMessage.updateMessageID() MimeMessage.setFrom() 将触发此方法。

  3. 设置 mail.smtp.from 防止查找EHLO命令。

  4. Altern您可以设置系统属性 mail.mime.address.usecanonicalhostname false 如果您的代码依赖于 setFrom() 但这应该由第2点处理。

  5. 对于IMAP,您可以尝试设置 mail.imap.sasl.usecanonicalhostname false 这是默认值。

  1. Set the session property for mail.smtp.localhost to prevent name lookup on the HELO command.
  2. 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.
  3. Set session property for mail.smtp.from to prevent lookup on EHLO command.
  4. 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.
  5. 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:

@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地址。

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真的很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆