Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of automatic cancellation of unpaid order time-out in Java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to realize the automatic cancellation of unpaid overtime in Java. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

After purchasing goods on e-commerce, if the order is issued without payment, it is generally prompted to complete the payment in 30 minutes, otherwise the order will be automatic. For example, when JD.com issues an order to complete the payment:

The order will be cancelled automatically after 24 hours. Java timer is used below to achieve the function of canceling the order after the timeout.

Timer timer

Timer is a tool for scheduling the execution of tasks, which can be executed at one time or repeatedly, and the system will start a thread to execute all scheduled tasks.

TimerTask scheduled task

TimerTask is an abstract class that implements Runnable, and implementing Runnable creates multithreaded tasks.

Create TimerTask

TimerTask is an abstract class. Abstraction is for code reuse. To create a class that inherits TimerTask:

Public class CancelOrderTimeTask extends TimerTask {private Long id; public CancelOrderTimeTask (long id) {this.id = id;} @ Override public void run () {/ / execute order cancellation cancelOrder (id); System.out.println (getCurrentTime () + "time cancellation, order id:" + id);} private String getCurrentTime () {SimpleDateFormat sdf = new SimpleDateFormat () Sdf.applyPattern ("yyyy-MM-dd HH:mm:ss"); Date date = new Date (); return sdf.format (date);}}

Perform the order cancellation task in the run method.

Because this method does not create a bean, you use ApplicationContext to get the bean when calling dao or other bean.

Timer timer calls TimerTask

Create a new Timer, using the schedule method call, which takes two parameters, the first is the instance of the task, and the other is how long the task is called, in milliseconds. The code is as follows:

@ RestControllerpublic class TimerController {@ GetMapping ("/ timer") public String timer (long id) {Timer timer = new Timer (); CancelOrderTimeTask timeTask = new CancelOrderTimeTask (id); System.out.println ("current time is" + getCurrentTime ()); / / execute task timer.schedule (timeTask,10 * 1000) in 10 seconds; return "ok" } private String getCurrentTime () {SimpleDateFormat sdf = new SimpleDateFormat (); sdf.applyPattern ("yyyy-MM-dd HH:mm:ss"); Date date = new Date (); return sdf.format (date);}}

After calling the method, the console outputs the following content, indicating that the timing call is successful.

The current time is 2022-01-24 00:05:09

2022-01-24 00:05:19 time cancel order, order id:3

Summary

First, create a scheduled task, inherit TimerTask, and write business logic in the run method.

The schedule method is called using Timer, and the schedule method is written to the TimerTask instance and the latency.

About Java how to achieve order unpaid timeout automatic cancellation function is shared here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report