I need to do some visibly simple task. But I do not know is it relevant to use Spring Boot JPA repository or to use some in-memory storage such like as Redis.
So I need to create some products in storage that would be viable for some interval – 15-20 min, upon which it is valid for deletion. Can I use some addProduct rest method, that after entity creation would refer for another method where I pass entity id to use Timertask, executor service or another @async secondary thread to counts this interval in minutes and remove product after it? Is it correct and possible to use in such way? Whether I should use Spring RedisTemplate with expire() method as alternative? In the context of first option – is standart db like mysql appropriate to use for some short-lived objects or in-memory H2 is better choice?
3
Answers
I want such like that but to separate new Timer() in separate method in separate thread, where it can wait but rest method would return after calling callback that should wait. I could not use such option as I read just now Timer use just one thread. So I need to delete or make valid for removal just 15 minutes after creation, but the products could be created in any time, so there is no common time to delete all ones in the same time. Maybe create some executor thread that would listen the inmemory list[id,expirationinterval] every second or two and then delete if there is such expired. I used such implementation for sse events. I also saw this example for timeout -How to set a timeout on a Spring Boot REST API? in Callable (why not in Runnable) - but I need to wait a dozen of minutes, but not several seconds.
Regarding the entity question, an entity is just a simple pojo mapped to a database row.
The jpa lifecycle and mechanism does not let you create an entity for a certain period of time. The entity in an MVC framework lives as long as the http request takes, and in some unfortunate cases, until your view(html for example) is rendere.
JPA does not offer the posibility to automatically evict entries from the persistence context. Some external party such as an scheduled task should perform evict on specific entities and this is a tedious task.
You could have an entity with a timestamp saved such as saved_date and run an scheduled job, say every 15 minutes, that cleans the database.
You could also take advantage of your TTL mechanism. So let’s say when someone request a resource(on a rest endpoint) that is 15-20 minutes old, you can discard it.
The best way to go from my opinion, I would suggest you to let the database itself handle expired data via TTL.
As far as I know Reddis and Cassandra support automatic data expiring via TTL, and the database itself will mark the candidate rows for the deletion process.
As I have read Spring has inbuild thread executor,
that allows to call async method, and return caller method,
not waiting to finish async called one.
Here –Making a Thread to Sleep for 30 minutes-I have read that use Thread.sleep() is not very relevant for such big interval, obviously, so better to use Timer.
So I have such option: @Async
Indeed I do not know is it normal to use Timer runnable inside
another secondary thread.
So there is other option – to use TaskScheduler –
But I consider such questions – should I put it inside @Async method,
are there enough threadpool for many temporal products