Deleting a record after a period of time or when the specified time reaches automatically in Java-Collection of common programming errors
I’m developing a Java web application. The scenario is described below:
-
Many users submit their status and they can see others’ statuses as well. It looks like a kind of social network.
-
Their statuses have a period of time to display, after this period, their statuses in database will be deleted.
-
At the time their statuses are deleted, send a notification to all users to request an update (the deleted statuses have to be vanished in realtime!)
I’ve tried to do that automatically by a thread in the Java web application. However, I’m stuck at some problems:
+Is it possible to manage database like that? I’ve thought of database event, but when a record is deleted, I will not be noticed.
+I’m using web socket to make the application work in realtime. If a person update a new status or change it, it’s possible to send an update notification to all of the users by using web socket. But when the changes come from database, I don’t know how to send a notification like that.
Can you give me some ideas? Thank you in advanced!
-
You can use
DelayQueue
to store each item to be removed at a specified delay,and when you are removing the item,remove the record in database and send a notify to browser. -
If you need the statuses to be deleted even if your program crashes, then I recommend that you use quartz – this way if your program crashes then quartz will update the database when you re-load your program. If your program doesn’t need to be this robust then use a DelayQueue instead.