delayQueue延时队列-模拟简易MQ

来源:互联网 发布:游族网络股票 编辑:程序博客网 时间:2024/06/08 20:02

package delayQueue;/* * Copyright (c) 2017 UCSMY. * All rights reserved. * Created on 2017/9/25 * Contributors: *      - initial implementation */import java.util.concurrent.DelayQueue;/** * 利用DelayQueue实现延时消息队列(简易版MQ) * * @author moss * @since 2017/9/25 */public class Producer implements  Runnable{    private DelayQueue<Message> queue;    public Producer(DelayQueue<Message> queue){        this.queue = queue;    }    @Override    public void run() {        //5秒后发送消息        Message m2 = new Message("2","Tom",5000);        queue.offer(m2);        System.out.println("消息生产者往消息队列放置消息:"+m2.getId()+":"+m2.getName());        //3秒后发送消息        Message m1 = new Message("1","Tom",3000);        queue.offer(m1);        System.out.println("消息生产者往消息队列放置消息:"+m1.getId()+":"+m1.getName());    }}
package delayQueue;/* * Copyright (c) 2017 UCSMY. * All rights reserved. * Created on 2017/9/25 * Contributors: *      - initial implementation */import java.util.concurrent.DelayQueue;/** * 暂无描述 * * @author moss * @since 2017/9/25 */public class Consumer implements Runnable{        private DelayQueue<Message> queue;        public Consumer(DelayQueue<Message> queue){            this.queue = queue;        }        @Override        public void run() {            while(true){                try {                    Message take = queue.take();                    System.out.println("消息需求者获取消息:"+take.getId()+":"+take.getName());                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }
package delayQueue;/* * Copyright (c) 2017 UCSMY. * All rights reserved. * Created on 2017/9/25 * Contributors: *      - initial implementation */import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;/** * * 1)2个小时后给用户发送短信。 2)15分钟后关闭网络连接。 3)2分钟后再次尝试回调。 * * @author ucs_gaokuixin * @since 2017/9/25 */public class Message implements Delayed {    private String id;    private String name;    private long activeTime;//执行时间    public Message(){    }    public Message(String id, String name,long activeTime) {        super();        this.id = id;        this.name = name;        this.activeTime = TimeUnit.NANOSECONDS.convert(activeTime, TimeUnit.MILLISECONDS) + System.nanoTime();    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public long getDelay(TimeUnit unit) {           return unit.convert(this.activeTime - System.nanoTime(), TimeUnit.NANOSECONDS);    }    @Override    public int compareTo(Delayed o) {        Message msg = (Message)o;               return Integer.valueOf(this.id)>Integer.valueOf(msg.id)?1:( Integer.valueOf(this.id)<Integer.valueOf(msg.id)?-1:0);    }}
package delayQueue;/* * Copyright (c) 2017 UCSMY. * All rights reserved. * Created on 2017/9/25 * Contributors: *      - initial implementation */import java.util.concurrent.DelayQueue;/** * 暂无描述 * * @author moss * @since 2017/9/25 */public class DelayQueueTest {    public static void main(String[] args) {        DelayQueue<Message> queue = new DelayQueue<Message>();        new Thread(new Producer(queue)).start();        new Thread(new Consumer(queue)).start();    }}
消息生产者往消息队列放置消息:2:Tom
消息生产者往消息队列放置消息:1:Tom
消息需求者获取消息:1:Tom
消息需求者获取消息:2:Tom

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信被限制登录收不到验证码怎么办 微信登录申诉收不到短信怎么办 登录微信手机收不到验证码怎么办 美图t8冲一会电就发烧怎么办 美图t8系统不小心升级了 怎么办 美图手机看相册视频就会变黑怎么办 苹果4s屏幕唤醒速度慢怎么办 美图m4换电池后不显示卡怎么办 美图手机久没充电再充没反应怎么办 美图6s开不开机怎么办 苹果4s照片不能拍照黑屏怎么办 美图手机开机键坏了怎么办 金立金刚二手机烧卡怎么办 金立金刚手机开不开机怎么办 小米4手机拆机信号不好怎么办 荣耀自带游览器无法正常打开怎么办 苹果4s电池越来越不耐用怎么办 苹果4s电池不耐用了怎么办 苹果手机4s电池不耐用怎么办 红米4s电池不耐用怎么办 32位app私密相册打不开怎么办 红米手机取卡针断手机里了怎么办 我差评了客服打电话骂我怎么办 顺丰快递在预计时间没有回来怎么办 同款商品比京东便宜怎么办 京东自营不支持7天退货怎么办 天猫买了一个月的电动车坏了怎么办 发票号码和机打号码不一致怎么办 交电费的本子弄丢了怎么办 快递正在派件中发现地址错了怎么办 快递当天送达当天签收还算延怎么办 深圳国税公众号预约取号公司怎么办 社保买了停了2年怎么办 qq号被冻结申请不回来了怎么办 qq号被冻结 手机密保忘了怎么办 微信账号封了2天怎么办 买qq号被申诉找回了怎么办 收到了京东白条的催款通知单怎么办 成都买房社保不够两年怎么办18年 电话号码给人设置成骚扰电话怎么办 找不到领导电话不接短信不回怎么办