【发布时间】:2018-03-21 09:10:20
【问题描述】:
我正在运行一个 spring(cron) 的 web 应用程序调度程序来发送通知邮件,当在本地服务器上运行时它运行良好,但在生产模式下它发送多个邮件。
<bean id="EmailNotificationScheduledTask" class="com.prism.utils.EmailNotificationTask" />
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="EmailNotificationScheduledTask" method="run" cron="0 0 10 * * MON"/>
</task:scheduled-tasks>
<task:scheduler id="scheduler" pool-size="1"/>
public class EmailNotificationTask extends TimerTask{
@Override
public void run() {
synchronized (this) {
sendPropertyNotificationEmail();
}
//}
}
@SuppressWarnings("unchecked")
public void sendPropertyNotificationEmail(){
try{
//Mail sending logic
}
} catch (DAOException ee) {
_logger.error("Error while sending notification messages", ee);
}
}
}
【问题讨论】:
-
你的代码中也有注释吗?
-
@hisener 不只是调用了“sendPropertyNotificationEmail”方法,其中包含邮件发送功能
-
sendPropertyNotificationEmail() 是否希望每次调用发送一封邮件?当您说“发送多封邮件”时,您的意思是在多个线程中以某种方式调用了 sendPropertyNotificationEmail() 吗?请详细说明问题。
-
@Rahul 是的,我认为它在多个线程中被调用。是的,该功能实现为每周一上午 10 点发送一封邮件,当我在本地对其进行测试时,它运行良好,只发送一封邮件,但在生产模式下部署时,正在发送多封邮件
-
@Rahul,请在问题中找到原始代码,我再次对其进行了编辑。实际上在原始代码中调用了run方法。
标签: java spring taskscheduler crontrigger