ThreadPoolTaskScheduler thread pool realizes automatic cancellation of orders without operation for 15 minutes
summary
Use ThreadPoolTaskScheduler's schedule method to implement the execution of a timed task after 15 minutes of the current operation
1. Triggered after the order is created
1.1 Impl layer
private final OrderSchedulerService schedulerService; @Override @Transactional(rollbackFor = ) public R saveHotelOrder(HotelOrderPO po) { HotelOrder order = new HotelOrder(); boolean saveOrUpdate = saveOrUpdate(order); // Supplement your own development logic if(Conditions met){ (()); } return (()); }
1.2 OrderSchedulerService (core class)
@Slf4j @Service @AllArgsConstructor public class OrderSchedulerService { private final HotelOrderMapper hotelOrderMapper; private final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); private final ConcurrentHashMap<Long, ScheduledFuture<?>> orderTasks = new ConcurrentHashMap<>(); // Simulate new orders for users public void createScheduler(Long orderId) { (); // Execute after 15 minutes Instant plus = ().plus((15)); ScheduledFuture<?> future = (() -> { if (isOrderActive(orderId)) { // Cancel the order HotelOrder order = (orderId); (CommonConstant.INT_5); (order); ("15No operation in minutes,Automatically cancel order:{}", ()); } }, plus); (orderId, future); } // When the order is cancelled, the 15-minute scheduled task of canceling the order public void cancelScheduler(Long orderId) { ScheduledFuture<?> future = (orderId); if (future != null && !()) { (false); (orderId); ("15Payment operation in minutes,Cancel order timing task:{}", orderId); } } // Check if the order is still in the task list (simulated order is not deleted) private boolean isOrderActive(Long orderId) { return (orderId); } }
2. If the order is manually cancelled, the corresponding timing task will be canceled.
1.1 Impl layer
@Override public R cancelByIdForMini(Long id, String reason) { // Write your own cancellation business // When the order is cancelled, the 15-minute scheduled task of canceling the order (" When the order is cancelled,Cancel order15Minute timed tasks:{}", ()); (id); return (true); }
Summarize
Use the schedule method of ThreadPoolTaskScheduler to implement the current operation for 15 minutes to execute the timing task~Creation is not easy, respect knowledge, please attach the link to this article to reprint
This is the article about Springboot's ThreadPoolTaskScheduler thread pool that can be easily handled for 15 minutes and automatically cancel orders without operating for 15 minutes. For more related Springboot ThreadPoolTaskScheduler thread pool content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!