我正在包装 java 邮件传输类并调用我的方法来处理发送电子邮件的有效地址。
运输类
public class Transport extends javax.mail.Transport {
private static final String CLASSNAME = Transport.class.getName();
private static final Logger LOGGER = Logger.getLogger(CLASSNAME);
public Transport(Session session, URLName urlname) {
super(session, urlname);
}
@Override
public void sendMessage(Message msg, Address[] addresses) throws MessagingException {
Dispatcher.processMailSending(msg,addresses);
send(msg,addresses);
}
public static void send(Message msg , Address[] addresses) throws MessagingException{
Dispatcher.processMailSending(msg,addresses);
javax.mail.Transport.send(msg,addresses);
}
public static void send(Message msg) throws MessagingException{
Dispatcher.processMailSending(msg);
javax.mail.Transport.send(msg);
}
}
调度员类
public class Dispatcher{
public static void processMailSending(Message msg , Address[] address){
Validator v = new Validator();
v.validate();
}
public static void processMailSending(Message msg) throws MessagingException{
Validator v = new Validator();
v.validate();
//TOD: do validation
}
}
class Validator{
private Message msg;
private Address[] address;
public Validator(Message msg , Address address){
this.msg = msg;
this.address = address;
}
public void validate(){
checkSpam(msg.getFrom());
}
}
我阅读了oracle论坛帖子。他们说Transport.send
是线程安全的。
https://forums.oracle.com/forums/thread.jspa?threadID=1589778&tstart=2865。
我的问题
线程Dispatcher.processMailSending
安全吗?
我避免syncrhonized
在processMailSending
. 这个方法会被频繁调用。
请提出您的意见。