I have a scenario in which a javax.ejb.Schedule @Schedule timer job is deployed on WebSphere Application Server in a 2 cluster environment with 2 seperate nodes, meaning that there are always 2 instances of application up and running. I have a mail sending scheduler written as below (runs after every 10 seconds, get the entries from database, and then updates the database column delivery status after sending email successfully):
@Startup
@Singleton
public class MailScheduler {
@EJB
EmailQueueService emailQueueService;
@Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 seconds timer", persistent = false)
public void automaticallyScheduled(Timer timer) {
scheduleMail(timer);
}
private void scheduleMail(Timer timer) {
emailQueueService.sendMails(); // This function send the emails and updates the database column
}
Now the problem I am facing is that When the EAR file containing above Scheduler is started, it starts on both nodes(servers) simultaneously. Now, users are receiving duplicate emails, as the EAR file started on both node at the same time, and the entries from database are picked by both Node's Scheduler.
Can someone let me know the solution to this problem? I want to run the @Schedule only once, and if it is already running, than second node should not execute this scheduler. If I set persistent = true, will it solve the problem? Any clue in resolving this issue would be highly appreciated.
Thanks.