0

What I would like to do: If a user on my web application (tomcat, java) performs an action and 1 week passes before he performs it again, I would like to send him an email. For example, someone performs a "like" on jan 1, 2015 then performs another "like" on Jan 3, 2015, he will get sent an email on Jan 10, 2015 (provided he doesn't perform any more "like's" after Jan 3, 2015).

How to execute: Originally, I wanted to use a simple java.util.Timer Object. However, I couldn't find a way to reference different tasks for this Timer object. Every time a user performed said action, I would need to check if a task already exists for that user, if it did, I would reschedule it for 1 week from current time. But like I said, I couldn't reference tasks with the Timer Object.

So I looked into cron4j and Quartz. Cron4j seems simpler so I would like to use that. Cron4j.Scheduler allows for referencing tasks. As I understand, I would create one instance of the cron4j.Scheduler class for the entire web application and then schedule additional tasks as needed to this one instance of the Scheduler class. Is this correct? If so, How would I do this so that I can access an instance of the Scheduler class from a servlet later? (I thought of maybe setting an instance of the Scheduler as an attribute of the ServletContext. But, for this I would need to execute some servlet once per my web application. Is this how it would be done? If so, How do I specify that a servlet is to be executed once?)

I welcome any answers to my questions above or suggestions for different approaches for doing what I would like.

Thank you

theyuv
  • 1,556
  • 4
  • 26
  • 55

1 Answers1

1

I'd go from the other way around. If you store the likes, and the date/timestamp of the like, have a daily job, that queries for all users having their last like in more than 7 days.

  • Thanks for responding. Yes, I store the "likes". I considered this approach. I am using MySQL so it would involve creating an Event and a UserDefinedFunction. So, I just figured that this would also require some research (for example, I read that the user defined function has to be written in C). But if it is the more logical solution then I will look into it. – theyuv May 31 '15 at 16:57
  • In theory I'd define a daily task (probably cron4j should be able to create a Java task, if not, the Quartz is for you). Also, MySQL has it's means to find [intervals](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html), however you can only use them via native queries, not when using JPA/QueryDSL/etc. – Sandor Nemeth May 31 '15 at 18:58
  • Could you give me some direction as to where to initialize the cronj4 scheduler and how to get that scheduler object in a servlet? Thanks – theyuv May 31 '15 at 22:30
  • I implemented your suggestion, using a ScheduledExecutorService as is explained here: http://stackoverflow.com/a/9186070/3684479 – theyuv Jun 01 '15 at 13:34
  • Any suggestions as to what to do if I have two identical instances running (ie: the task is called twice, since it's in my program)? One option is a field in the DB that states whether the task ran. Anything better? I figure others have had this problem. – theyuv Feb 23 '16 at 08:19