Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

java - MimeMessage.saveChanges is really slow

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");
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Fix the most common mistakes people make when using JavaMail in your code first.

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 so you shouldn't have to change these settings.

Preferably, you can use the session properties to avoid some these lookups.

  1. 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 a call to get the local address. If the above properties are not set then this method will attempt to query for the host name. By setting the properties, this method will pull the host name string from the session instead of attempting the expensive DNS lookup.
  2. Set the session property for mail.smtp.localhost or mail.smtps.localhost to prevent name lookup on the HELO command.
  3. Set session property for mail.smtp.from or mail.smtps.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 will be handled if you applied point #1.
  5. For IMAP, you can try to set mail.imap.sasl.usecanonicalhostname or mail.imaps.sasl.usecanonicalhostname to false which is the default value.

Since your are not transporting a message, apply rule #1 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");
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...