大多数的b2c商城项目都会有限时活动,当用户下单后都会有支付超时时间,当订单超时后订单的状态就会自动变成已取消 ,这个功能的实现有很多种方法,本文的实现方法适合大多数比较小的商城使用。
实现原理:
利用 jdk 的 delayqueue的阻塞队列的特性实现。在项目启动时开启一个线程处理 delayqueue 队列里弹出的超时订单对象,订单未超时该线程处于等待中。
delayqueue的简单介绍:
delayqueue类的主要作用:是一个无界的blockingqueue,用于放置实现了delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即队头对象的延迟到期时间最长。注意:不能将null元素放置到这种队列中。
实现方式 :
1.创建一个实现delayed接口的 order 类并重写compareto和 getdelay方法
2.创建一个帮助类ordercollection(订单的增删查)
3. 创建cancellorder类
在生成订单时将订单号创建时间和过期时间封装成一个实现delayed接口的对象存入delayqueue队列中,当该订单支付完成后将该对象从队列中移除,(为了保证不丢失订单建议在项目启动时将数据库中的符合条件的订单初始化到delayqueue队列中 )
实现代码如下:
/**
* 类说明
*
* @author grl
* @date 2019年12月16日 新建
*/
public class order implements delayed {
private string ordershopnum;
/**
* 1-普通活动 2-限时活动 3-拼购活动
*/
private int ordertype;
private long ordercreatetime;
private long exptime;
public order(string ordershopnum, int ordertype, date createtime) {
if (stringutils.isnotblank(ordershopnum)) {
this.ordershopnum = ordershopnum.trim();
}
if (createtime == null) {
this.ordercreatetime = system.currenttimemillis();
} else {
this.ordercreatetime = createtime.gettime();
}
this.ordertype = ordertype;
if (ordertype == 2) {
this.exptime = timeunit.milliseconds.convert(const.limit_activity_expiration_time, timeunit.minutes)
createtime.gettime();
}if(ordertype == 3){
this.exptime = timeunit.milliseconds.convert(const.limit_group_buy_expiration_time, timeunit.minutes)
createtime.gettime();
} else {
this.exptime = timeunit.milliseconds.convert(const.order_payment_deadline, timeunit.days)
createtime.gettime();
}
}
public string getordershopnum() {
return ordershopnum;
}
public long getordercreatetime() {
return ordercreatetime;
}
public long getexptime() {
return exptime;
}
public int getordertype() {
return ordertype;
}
@override
public int compareto(delayed o) {
return long.valueof(this.exptime).compareto(long.valueof(((order) o).exptime));
}
@override
public long getdelay(timeunit unit) {
return unit.convert(this.exptime - system.currenttimemillis(), timeunit.milliseconds);
}
}
/**
* 类说明
*
* @author grl
* @date 2019年12月16日 新建
*/
public class ordercollection {
/**
* 订单管理集合
*/
private static delayqueue orderlist = new delayqueue();
private ordercollection() {
}
/**
* 获取订单集合
* @author grl
* @return
*/
protected static delayqueue getordercollection() {
return orderlist;
}
/**
* 向集合中添加订单
*
* @author grl
* @param order
* @return
*/
public static boolean add(order order) {
boolean flag = false;
if (order != null && stringutils.isnotblank(order.getordershopnum())) {
flag = orderlist.offer(order);
}
return flag;
}
/**
* 从集合中删除订单
*
* @author grl
* @param ordershopnum
* @return
*/
public static boolean remove(string ordershopnum) {
boolean flag = false;
order thisorder = null;
if (stringutils.isnotblank(ordershopnum)) {
ordershopnum = ordershopnum.trim();
for (order order : orderlist) {
string ordernum = order.getordershopnum();
if (ordernum.equals(ordershopnum)) {
thisorder = order;
}
}
if (thisorder != null) {
flag = orderlist.remove(thisorder);
}
}
return flag;
}
/**
* 获取订单的过期剩余时间
*
* @author grl
* @param ordershopnum
* @param unit
* @return -1 已经过期
*/
public static long getdelay(string ordershopnum) {
long time = -1;
if (stringutils.isnotblank(ordershopnum)) {
ordershopnum = ordershopnum.trim();
for (order order : orderlist) {
string ordernum = order.getordershopnum();
if (ordernum.equals(ordershopnum)) {
time = order.getdelay(timeunit.milliseconds);
if(time<5000) {
time = -1;
}
}
}
}
return time;
}
}
/**
* 类说明
*
* @author grl
* @date 2019年12月16日 新建
*/
@component
public class cancellorder implements runnable {
private static final logger log = loggerfactory.getlogger(cancellorder.class);
@override
public void run() {
while (true) {
try {
order take = ordercollection.getordercollection().take();
string ordershopnum = take.getordershopnum();
int ordertype = take.getordertype();
// 业务逻辑操作
} catch (interruptedexception e) {
e.printstacktrace();
log.error("cancellorder delayqueue 错误 {}", e.getmessage());
}
}
}
}
总结
以上所述是小编给大家介绍的java实现商城订单超时取消功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,九游会下载的版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌九游会下载的版权的内容,欢迎发送邮件至:notice#www.elefans.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。