4

Here is the use case:

I am using Java (with Spring)

Once the user (through a web-app) confirms to the subscription, I want to send him an email exactly after 30 mins.

Now how to do this? Do I need a message broker? or something like ScheduledExecutorService? Do I need some sort of queue?

Please advise.

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
adi
  • 1,711
  • 3
  • 29
  • 50

4 Answers4

4

Can look into quartz scheduler for this.

By the way a common strategy is to send a bulk of all pending mails in bulk in every 30 minutes or so. Quartz can help in do that as well.

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • thanks, but how will this happen actually? Am I right in assuming that at occurance of every event, a new thread will be created which will wait for 30 mins, then sends the mail and then die? – adi Jul 28 '11 at 13:19
  • can we do this using ScheduledExecutorService? – adi Jul 28 '11 at 13:20
  • @adi I am not sure about exact inner workings of Quartz, but rest assured, it is super efficient... I have used it to drive couple of thousands jobs without any significant resource depletion. As far as the specific mailing jobs are concerned... I guess Quartz would do great. I never used ScheduledExecutorService, so can't honestly comment on it. – Nishant Jul 28 '11 at 14:12
2

Create an object for Timer

 private Timer myTimer;

in main method

myTimer = new Timer();
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
           //your method
        }

    }, 0, 200);
Muhammed Fasil
  • 7,909
  • 2
  • 19
  • 28
2

You can use the Quartz Scheduler. Its fairly easy to use. You can schedule something every week or ever 30 minutes or whatever you want basically.

RMT
  • 7,040
  • 4
  • 25
  • 37
0

It is not that the thread will die after sending the mail. When you configure Quartz, a new thread will automatically be created and will execute the assigned task on specified interval. Or else you use Timer class also. It is very easy to use.

    Timer timer = new Timer(); // Get timer

    long delay = 30 * 60 * 1000; // 3o min delay

    // Schedule the two timers to run with different delays.
    timer.schedule(new MyTask(), 0, delay);

...................



class MyTask extends TimerTask {

    public void run() {
        // business logic
        // send mail here
    }
}
RMT
  • 7,040
  • 4
  • 25
  • 37
Partha
  • 572
  • 1
  • 9
  • 17
  • what will happen to the thread if it is not dead after sending the mail? will I have hundreds on threads after a while? – adi Jul 29 '11 at 10:49
  • A separate thread will be running and when time ticks Task will gets executed. When you create a timer and it is scheduled, it runs in the background and executes the task every after specified interval. Try the following example public class Main { public static void main(String[] args) { Timer timer = new Timer(); long delay = 1000; // 1 sec delay timer.schedule(new MyTask(), 0, delay); while(true){ } } } public class MyTask extends TimerTask { public void run() { // business logic System.out.println("Mail sent"); } } – Partha Jul 29 '11 at 16:16
  • that works well but what about if I need to change times? For instance, I want delay time to be in the range of two time. – grep Oct 23 '15 at 16:13