-1

I want to run a method which sends notification e mail to users in java. But how can I tell the program "run the method if variable's value haven't changed for last 24 hours" ?

  • You could run a cronjob every 15 min which checks if the variable has changed. There are probably multiple solutions and on google you will definitely find a solution. – Malte Kölle Feb 22 '19 at 09:08

2 Answers2

2

Short answer: you can't. What you can do is save a copy of the variable's value, and schedule a task to be executed periodically, compare the variable's value with the one you saved, call the method if it's different, and save the new value.

UPDATE

After a exchange of comments, I will point out that I would not recommend that solution in the case where you can control the assignments to variable (for instance if there is a setter for that variable, and you can alter the code of that setter), in this case, I would rather recommend a solution like the one posted by GhostCat.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
  • 1
    Simply wrong. Even when you have a a plain POJO, you could simply go in and take a timestamp every time a setter is called. – GhostCat Feb 22 '19 at 09:13
  • @GhostCat I can agree with that, but the simple fact that he has ask the question makes me think that he cannot or does not want to change the code that is assigning to the variable. – Maurice Perry Feb 22 '19 at 09:27
2

Simple: by writing code for that. What you will need:

  • some sort of "time aware" data store. In other words: a plain field within your class won't do. Like, when you have class Person { String lastName ... ... tracking changes directly on a field of a Person object isn't possible. In other words: you need to write your own code that gets told "update the value for key X" ... and whenever that happens, you store the time stamp of the change. The most simple approach: have setter() methods that store that timestamp. But that idea doesn't scale, and would lead to a terrible design.
  • a service that regularly comes in and checks these timestamps, to then make decisions about that.

And hint: given the fact that your question is very broad and unspecific, don't expect more specific answers (or even code) coming back.

GhostCat
  • 137,827
  • 25
  • 176
  • 248