网站建设首选定制开发,国内做外单的网站有哪些资料,呼市做网站建设的公司哪家好,商务网站建设课程设计今天给大家上一盘硬菜#xff0c;并且是支付中非常重要的一个技术解决方案#xff0c;有这块业务的同学注意自己尝试一把哈#xff01;
一、需求如下#xff1a; 生成订单30分钟未支付#xff0c;自动取消 生成订单60秒后,给用户发短信
对上述的需求#xff0c;我们给…今天给大家上一盘硬菜并且是支付中非常重要的一个技术解决方案有这块业务的同学注意自己尝试一把哈
一、需求如下 生成订单30分钟未支付自动取消 生成订单60秒后,给用户发短信
对上述的需求我们给一个专业的名字来形容那就是延时任务。你可能会问延时任务和定时任务有啥区别呢
一共有以下几点区别 定时任务有明确的触发时间延时任务没有 定时任务有执行周期而延时任务在某事件触发后一段时间内执行没有执行周期 定时任务一般执行的是批处理操作是多个任务而延时任务一般是单个任务
二、解决方案
(1)数据库轮询
该方案通常是在小型项目中使用即通过一个线程定时的去扫描数据库通过订单时间来判断是否有超时的订单然后进行update或delete等操作
1引入依赖
dependencygroupIdorg.quartz-scheduler/groupIdartifactIdquartz/artifactIdversion2.2.2/version
/dependency
2创建Demo类实现
public class MyJobDemo implements Job {public void execute(JobExecutionContext context)throws JobExecutionException {System.out.println(我去访问数据库啦。。。);}public static void main(String[] args) throws Exception {// 创建任务JobDetail jobDetail JobBuilder.newJob(MyJobDemo.class).withIdentity(job1, group1).build();// 创建触发器 每3秒钟执行一次Trigger trigger TriggerBuilder.newTrigger().withIdentity(trigger1, group3).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever()).build();Scheduler scheduler new StdSchedulerFactory().getScheduler();// 将任务及其触发器放入调度器scheduler.scheduleJob(jobDetail, trigger);// 调度器开始调度任务scheduler.start();}
}
3运行结果每3秒输出
我去访问数据库啦。。。
优点:简单易行支持集群操作
缺点:
(1)对服务器内存消耗大
(2)存在延迟比如你每隔3分钟扫描一次那最坏的延迟时间就是3分钟
(3)假设你的订单有几千万条每隔几分钟这样扫描一次数据库损耗极大
(2)JDK的延迟队列
该方案是利用JDK自带的DelayQueue来实现这是一个无界阻塞队列该队列只有在延迟期满的时候才能从中获取元素放入DelayQueue中的对象是必须实现Delayed接口的。 Poll():获取并移除队列的超时元素没有则返回空 take():获取并移除队列的超时元素如果没有则wait当前线程直到有元素满足超时条件返回结果。
1定义一个类OrderDelay实现Delayed
public class OrderDelay implements Delayed {private String orderId;private long timeout;OrderDelay(String orderId, long timeout) {this.orderId orderId;this.timeout timeout System.nanoTime();}public int compareTo(Delayed other) {if (other this)return 0;OrderDelay t (OrderDelay) other;long d (getDelay(TimeUnit.NANOSECONDS) - t.getDelay(TimeUnit.NANOSECONDS));return (d 0) ? 0 : ((d 0) ? -1 : 1);}// 返回距离你自定义的超时时间差值public long getDelay(TimeUnit unit) {return unit.convert(timeout - System.nanoTime(),TimeUnit.NANOSECONDS);}void print() {System.out.println(orderId编号的订单即将删除啦。。。。);}
}
2运行的测试Demo为我们设定延迟时间为3秒
public class DelayQueueDemo {public static void main(String[] args) { ListString list new ArrayListString(); list.add(00000001); list.add(00000002); list.add(00000003); list.add(00000004); list.add(00000005); DelayQueueOrderDelay queue newDelayQueueOrderDelay(); long start System.currentTimeMillis(); for(int i 0;i5;i){ //延迟三秒取出queue.put(new OrderDelay(list.get(i), TimeUnit.NANOSECONDS.convert(3,TimeUnit.SECONDS))); try { queue.take().print(); System.out.println(After (System.currentTimeMillis()-start) MilliSeconds); } catch (InterruptedException e) {} } }
}
3输出如下
00000001编号的订单即将删除啦。。。。
After 3003 MilliSeconds
00000002编号的订单即将删除啦。。。。
After 6006 MilliSeconds
00000003编号的订单即将删除啦。。。。
After 9006 MilliSeconds
00000004编号的订单即将删除啦。。。。
After 12008 MilliSeconds
00000005编号的订单即将删除啦。。。。
After 15009 MilliSeconds
优点:效率高,任务触发时间延迟低。
缺点:
(1)服务器重启后数据全部消失怕宕机
(2)集群扩展相当麻烦
(3)因为内存条件限制的原因比如下单未付款的订单数太多那么很容易就出现OOM异常
(4)代码复杂度较高
(3)时间轮算法 时间轮算法可以类比于时钟如上图箭头指针按某一个方向按固定频率轮动每一次跳动称为一个 tick。
这样可以看出定时轮由个3个重要的属性参数 ticksPerWheel一轮的tick数 tickDuration一个tick的持续时间 timeUnit时间单位
例如当ticksPerWheel60tickDuration1timeUnit秒这就和现实中的始终的秒针走动完全类似了。
如果当前指针指在1上面我有一个任务需要4秒以后执行那么这个执行的线程回调或者消息将会被放在5上。那如果需要在20秒之后执行怎么办由于这个环形结构槽数只到8如果要20秒指针需要多转2圈。位置是在2圈之后的5上面20 % 8 1
具体实现(使用Netty的HashedWheelTimer来实现):
1引依赖:
dependencygroupIdio.netty/groupIdartifactIdnetty-all/artifactIdversion4.1.24.Final/version
/dependency
2创建HashedWheelTimerTest测试
public class HashedWheelTimerTest {static class MyTimerTask implements TimerTask{boolean flag;public MyTimerTask(boolean flag){this.flag flag;}public void run(Timeout timeout) throws Exception {System.out.println(我去数据库删除订单了。。。。);this.flag false;}}public static void main(String[] argv) {MyTimerTask timerTask new MyTimerTask(true);Timer timer new HashedWheelTimer();timer.newTimeout(timerTask, 5, TimeUnit.SECONDS);int i 1;while(timerTask.flag){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(过去了i秒);i;}}
}
3输出如下:
过去了1秒
过去了2秒
过去了3秒
过去了4秒
过去了5秒
我去数据库删除订单了。。。。
过去了6秒
优点:效率高,任务触发时间延迟时间比delayQueue低代码复杂度比delayQueue低。
缺点:
(1)服务器重启后数据全部消失怕宕机
(2)集群扩展相当麻烦
(3)因为内存条件限制的原因比如下单未付款的订单数太多那么很容易就出现OOM异常
(4)redis缓存
思路一
利用redis的zset,zset是一个有序集合每一个元素(member)都关联了一个score,通过score排序来取集合中的值
相关的命令操作 添加元素:ZADD key score member [[score member] [score member] …] 按顺序查询元素:ZRANGE key start stop [WITHSCORES] 查询元素score:ZSCORE key member 移除元素:ZREM key member [member …]
具体实现我们将订单超时时间戳与订单号分别设置为score和member,系统扫描第一个元素判断是否超时具体如下图所示 1代码实现
public class AppTest {private static final String ADDR 127.0.0.1;private static final int PORT 6379;private static JedisPool jedisPool new JedisPool(ADDR, PORT);public static Jedis getJedis() {return jedisPool.getResource();}//生产者,生成5个订单放进去public void productionDelayMessage(){for(int i0;i5;i){//延迟3秒Calendar cal1 Calendar.getInstance();cal1.add(Calendar.SECOND, 3);int second3later (int) (cal1.getTimeInMillis() / 1000);AppTest.getJedis().zadd(OrderId,second3later,OID0000001i);System.out.println(System.currentTimeMillis()ms:redis生成了一个订单任务订单ID为OID0000001i);}}//消费者取订单public void consumerDelayMessage(){Jedis jedis AppTest.getJedis();while(true){SetTuple items jedis.zrangeWithScores(OrderId, 0, 1);if(items null || items.isEmpty()){System.out.println(当前没有等待的任务);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}continue;}int score (int) ((Tuple)items.toArray()[0]).getScore();Calendar cal Calendar.getInstance();int nowSecond (int) (cal.getTimeInMillis() / 1000);if(nowSecond score){String orderId ((Tuple)items.toArray()[0]).getElement();jedis.zrem(OrderId, orderId);System.out.println(System.currentTimeMillis() ms:redis消费了一个任务消费的订单OrderId为orderId);}}}public static void main(String[] args) {AppTest appTest new AppTest();appTest.productionDelayMessage();appTest.consumerDelayMessage();}
}
2输出的时候会看到几乎都是3秒后进行订单的消费然而它有一个致命的伤高并发条件下多消费者会取到同一个订单号也就是我们常说的超卖问题显然出现了多个线程消费同一个资源的情况。
针对这个问题的解决方案是
(1)用分布式锁但是用分布式锁性能下降了该方案不细说。
(2)对ZREM的返回值进行判断只有大于0的时候才消费数据于是consumerDelayMessage()方法里的
if(nowSecond score){String orderId ((Tuple)items.toArray()[0]).getElement();jedis.zrem(OrderId, orderId);System.out.println(System.currentTimeMillis()ms:redis消费了一个任务消费的订单OrderId为orderId);
}
修改为
if(nowSecond score){String orderId ((Tuple)items.toArray()[0]).getElement();Long num jedis.zrem(OrderId, orderId);if( num ! null num0){System.out.println(System.currentTimeMillis()ms:redis消费了一个任务消费的订单OrderId为orderId);}
}
修改后代码输出即为正常。
思路二
该方案使用redis的Keyspace Notifications利用该机制可以在key失效之后提供一个回调实际上是redis会给客户端发送一个消息。值得注意的是redis版本要在2.8以上。
具体实现
1向redis.conf中加入一条配置
notify-keyspace-events Ex
2代码实现
public class RedisTest {private static final String ADDR 127.0.0.1;private static final int PORT 6379;private static JedisPool jedis new JedisPool(ADDR, PORT);private static RedisSub sub new RedisSub();public static void init() {new Thread(new Runnable() {public void run() {jedis.getResource().subscribe(sub, __keyevent0__:expired);}}).start();}public static void main(String[] args) throws InterruptedException {init();for(int i 0;i10;i){String orderId OID000000i;jedis.getResource().setex(orderId, 3, orderId);System.out.println(System.currentTimeMillis()ms:orderId订单生成);}}static class RedisSub extends JedisPubSub {public void onMessage(String channel, String message) {System.out.println(System.currentTimeMillis()ms:message订单取消);}}
}
3输出体现3秒过后订单取消了
redis的pub/sub机制存在一个硬伤官网内容如下
Because Redis Pub/Sub is fire and forget currently there is no way to use this feature if your application demands reliable notification of events, that is, if your Pub/Sub client disconnects, and reconnects later, all the events delivered during the time the client was disconnected are lost.
直译过来的意思
Redis的发布/订阅目前是即发即弃(fire and forget)模式的因此无法实现事件的可靠通知。也就是说如果发布/订阅的客户端断链之后又重连则在客户端断链期间的所有事件都丢失了。
故这个方案不太推荐使用。如你对可靠性要求不是很高时可以使用。
优点:
(1)由于使用Redis作为消息通道消息都存储在Redis中。如果发送程序或者任务处理程序挂了重启之后还有重新处理数据的可能性。
(2)做集群扩展相当方便
(3)时间准确度高
缺点:
需要额外进行redis维护
(5)使用消息队列
可以采用RabbitMQ的延时队列。RabbitMQ具有以下两个特性可以实现延迟队列
RabbitMQ可以针对Queue和Message设置 x-message-tt来控制消息的生存时间如果超时则消息变为dead letter
lRabbitMQ的Queue可以配置x-dead-letter-exchange 和x-dead-letter-routing-key可选两个参数用来控制队列内出现了deadletter则按照这两个参数重新路由。
优点:
高效,可以利用rabbitmq的分布式特性轻易的进行横向扩展,消息支持持久化增加了可靠性。
缺点
本身的易用度要依赖于rabbitMq的运维.因为要引用rabbitMq,所以复杂度和成本变高