英文:
how to send mail in background right after insert data in db through method in java
问题
在Java中发送邮件会导致我在页面上长时间等待,直到所有邮件发送完成。
我从daoImpl类文件方法中将数据插入到数据库中。在插入数据后,我会在同一个方法中发送电子邮件。
我如何将仅邮件处理部分分离到后台,并在插入数据后返回到操作层。
public int insertViewInfoDetails(AddViewInfoForm form) throws SQLException, Exception {
// 数据库插入代码...
pst.setDouble(29, RE_ORDER_LEVEL);
pst.execute();
Logger.getLogger(CLASS_NAME).info("inserted ViewTankInfoDetails - " + form);
StringBuilder emailDesc = new StringBuilder();
emailDesc.append("Customer :: ");
emailDesc.append(CUSTOMER_NAME);
emailDesc.append("\n");
final String getEmailId = "SELECT email_id FROM mst_user";
pst = conn.prepareStatement(getEmailId);
pst.setString(1, CONTAINER_ID);
rs5 = pst.executeQuery();
while (rs5.next()) {
// 调用发送邮件的方法
eMailAfterGettingAlerts(properties, rs5.getString("EMAIL_ID"), emailDesc.toString());
}
rs5.close();
rs5 = null;
return n:
}
我可以创建一个调度程序吗?不是基于时间...而是仅当我调用它时,它应该在此之后运行,运行完成后应该停止。
英文:
Sending Mail in Java making me to wait on page for long time until all mails finish sending.
I'm inserting data's into DB from daoImpl class file method. after inserting im sending emails in same method.
how i can apart only the mail process to background and return to action layer after insert data.
public int insertViewInfoDetails(AddViewInfoForm form) throws SQLException, Exception {
//DATABASE INSERT CODE...
pst.setDouble(29, RE_ORDER_LEVEL);
pst.execute();
Logger.getLogger(CLASS_NAME).info("inserted ViewTankInfoDetails - " + form);
StringBuilder emailDesc = new StringBuilder();
emailDesc.append("Customer :: ");
emailDesc.append(CUSTOMER_NAME);
emailDesc.append("\n");
final String getEmailId = "SELECT email_id FROM mst_user";
pst = conn.prepareStatement(getEmailId);
pst.setString(1, CONTAINER_ID);
rs5 = pst.executeQuery();
while (rs5.next()) {
//CALL MAILING METHOD
eMailAfterGettingAlerts(properties, rs5.getString("EMAIL_ID"), emailDesc.toString());
}
rs5.close();
rs5 = null;
return n:
}
can i create Scheduler? not by timing... but only when i call it should run after that process should stop.
答案1
得分: 1
以下是翻译好的内容:
你可以在另一个线程中执行它,在实现可运行接口的邮件逻辑中设置,并且在遍历结果时使用线程执行器来执行发送邮件的逻辑。
可运行接口的示例:
class MailSender implements Runnable {
public MailSender(String email, String message) {
this.email = email;
this.message = message;
}
@Override
public void run() {
sendMail(email, message);
}
}
然后你可以使用执行器服务来运行它:
ExecutorService executorService = Executors.newCachedThreadPool();
while (rs5.next()) {
executorService.submit(new MailSender(rs5.getString("EMAIL_ID"), rs5.getString("EMAIL_MESSAGE")));
}
英文:
You can execute it in another thread, set the mailing logic in implementation for a runnable interface and while you loop through the results use thread executor to execute the sending mail logic
Sample for the runnable :
class MailSender implements Runnable {
public MailSender(String email,String message) {
this.email=email;
this.message=message;
}
@Override
public void run() {
sendMail(email,message)
}
}
and you can run it using the executor service
ExecutorService executorService = Executors.newCachedThreadPool();
while (rs5.next()) {
executorService.submit(new MailSender(rs5.getString("EMAIL_ID"),rs5.getString("EMAIL_MESSAGE")));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论