Middle+
How to free a reserved space if the user did not pay within 20 minutes?
sobes.tech AI
Answer from AI
To free up a reserved seat when payment is not made within 20 minutes, you can use a task scheduler or timers:
- A record with a timestamp is created upon booking.
- A background task (for example, ScheduledExecutorService in Java) is started, which periodically checks for expired reservations.
- If more than 20 minutes have passed without payment, the reservation is canceled, and the seat is freed.
Example using ScheduledExecutorService:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
// Logic to check and free seats
List<Booking> expired = bookingService.findExpiredBookings(Duration.ofMinutes(20));
expired.forEach(booking -> bookingService.cancelBooking(booking));
}, 0, 5, TimeUnit.MINUTES);