delayQueue延时队列-模拟简易MQ


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值