-2

I want to send notifications after every 30 seconds. I have scheduled my task using TimerTask but it is not working. Why is it so? My Searcher is given below:

@Override
public Result search(Query query, Execution execution) {

    // pass it down the chain to get a result
    Result result = execution.search(query);
    execution.fill(result);

    //the Date and time at which you want to execute
    DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
        date = dateFormatter.parse("2018-10-17 16:20:00");
    } catch (ParseException e) {
        e.printStackTrace();
    }

    //Now create the time and schedule it
    Timer timer = new Timer();

    assert date != null;
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            sendNotification();
        }
    }, date, 30000);


    // return the result up the chain
    return result;
}
Mohammad Sunny
  • 371
  • 1
  • 3
  • 15

1 Answers1

0

The search() method is invoked on a per request basis and you are creating a new Timer instance per request which is garbage collected away by the Java VM some time after the search method returns as there are no references to it.

Jo Kristian Bergum
  • 2,984
  • 5
  • 8