0

I am working on a project where we are creating an auction site that works on a weekly basis. The plan is to start all auctions on Monday and end all on Friday.

So far I have figured out that I need a database that holds the start and end date so I can check to see how much time left and so. But I need to be able to constantly check and see if the time is up or not and I do not know how to proceed. What is the proper way to do this?

We are using Java8 with Spring and react as frontend.

Adam Lundberg
  • 51
  • 1
  • 3

4 Answers4

2

two solution:

  1. Use websocket, server set a timer which due at Friday, and once timer expired, send the event to client.
  2. client side do timer also.
2

You have 3 layers in play here:

  1. Frontend (React)
  2. Backend (Java8/Spring app)
  3. Database

Now you need to figure out how to propagate data between those layers.

Propagating from backend to frontend can be done using either polling or websockets.

Propagating from database to backend can be done using either polling or database triggers.

I'd personally connect React with Spring App via websockets. Then I'd have some background task polling the database and pushing the discovered changes to connected websocket clients.

Here is Springs own tutorial on websockets https://spring.io/guides/gs/messaging-stomp-websocket/

Tarmo
  • 3,851
  • 2
  • 24
  • 41
  • Sounds like I have to take a look at websockets. Do you have a tip on how to check if the time is up in the backend, I'm a bit stuck on this one – Adam Lundberg Mar 23 '21 at 12:57
  • Can the end time change? If not then just query it once and do the checking in code. If it can then poll it from time to time and still do checking in code. – Tarmo Mar 23 '21 at 13:02
  • And if you don't want to poll anything then you can check this thread out https://stackoverflow.com/questions/18126178/notifying-postgres-changes-to-java-application – Tarmo Mar 23 '21 at 13:03
  • As of now it won't change, start every Monday at 08 and end on Fridays at 12. But the dates will probably need to be updated every week – Adam Lundberg Mar 23 '21 at 13:06
1

I think you are looking for a pull model. Basically your Java application needs to pull the end date from database at certain intervals. You can write a cron job for that.Quartz cron is one of the popular Java based frameworks out there http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html . It handles distributed system also. So if your application is having multiple instances, Quartz can cover it for you.

Another variant in pull model, you can read the entry with end dates in JVM local cache or some other cache(Redis, Memcache) & run a cron on that. But you have to maintain cache consistency with database.

Which one you choose depends on your business use case(how frequently end date changes, how frequently you need to check for end dates etc.).

Other option was to go for push model. But push model won't work with traditional databases for this case.

aatwork
  • 2,130
  • 4
  • 17
0

Possible option - is to extend org.springframework.web.servlet.handler.HandlerInterceptorAdapter
And write logic of checking current time against your time range in this class - with throwing of an exception if check fails. Potential optimization - cache values from DB (at least for some time, for example - 15 minutes - as it will help to decrease number of actual calls to the database.

Daniel
  • 630
  • 5
  • 9