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