Я сравнил с отправкой 10000 простой почты в java и nodejs.
i пользователь Fakesmtp как почтовый сервер при запуске в локальном. минуты. < /strong> < /p>
Почему есть такая большая разница? Код < /p>
const nodemailer = require('nodemailer');
// SMTP local
const transporter = nodemailer.createTransport({
host: 'localhost',
port: 25,
secure: false,
tls: {
rejectUnauthorized: false
}
});
// Fonction pour envoyer un seul email
function sendEmail(index) {
return transporter.sendMail({
from: 'test@localhost',
to: `user${index}@example.com`,
subject: `Test email ${index}`,
text: `Ceci est le message numéro ${index}`
});
}
async function main() {
console.time('envoi_10000_emails');
for (let i = 1; i
java code: < /p>
import jakarta.mail.*;
import jakarta.mail.internet.*;
import java.util.Properties;
public class SequentialEmailSender2 {
private static final String SMTP_HOST = "localhost";
private static final int SMTP_PORT = 25;
private static final int TOTAL_EMAILS = 10000;
private static Session session;
private static Transport transport;
public static void main(String[] args) throws MessagingException {
long startTime = System.currentTimeMillis();
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "false");
session = Session.getInstance(props);
transport = session.getTransport("smtp");
transport.connect();
for (int i = 1; i
java с 50 потоками: 13 минут 45 секунд еще далеко от 3 минут < /p>
public class BatchEmailSender3 {
private static final String SMTP_HOST = "localhost";
private static final int SMTP_PORT = 25;
private static final int TOTAL_EMAILS = 10000;
private static final int BATCH_SIZE = 50;
private static Session session;
private static Transport transport;
public static void main(String[] args) throws MessagingException {
long startTime = System.currentTimeMillis();
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "false");
session = Session.getInstance(props);
transport = session.getTransport("smtp");
transport.connect();
ExecutorService executor = Executors.newFixedThreadPool(BATCH_SIZE);
for (int i = 1; i {
sendEmail(session, index);
});
}
executor.shutdown();
try {
if (!executor.awaitTermination(20, TimeUnit.MINUTES)) {
System.err.println("Timeout atteint avant fin des envois.");
}
} catch (InterruptedException e) {
System.err.println("Interrompu pendant l’attente.");
Thread.currentThread().interrupt();
}
long endTime = System.currentTimeMillis();
System.out.println("Temps total : " + (endTime - startTime) + " ms");
}
private static void sendEmail(Session session, int index) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("test@localhost"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("user" + index + "@example.com"));
message.setSubject("Test email " + index);
message.setText("Ceci est le contenu de l'email numéro " + index);
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
System.err.println("Error email " + index + " : " + e.getMessage());
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... s-and-java