发送端,执行Message对象的setDelayTimeLevel();
级别从1到18,时常分别为 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
public class OrderStep {
private Long orderId;
private String desc;
public OrderStep(){}
public OrderStep(Long orderId, String desc) {
this.orderId = orderId;
this.desc = desc;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "OrderStep{" +
"orderId=" + orderId +
", desc='" + desc + '\'' +
'}';
}
public static List buildOrders(){
List orderSteps = new ArrayList<>();
orderSteps.add(new OrderStep(00001L,"创建"));
orderSteps.add(new OrderStep(00001L,"付款"));
orderSteps.add(new OrderStep(00001L,"消费"));
orderSteps.add(new OrderStep(00001L,"完成"));
orderSteps.add(new OrderStep(00002L,"创建"));
orderSteps.add(new OrderStep(00002L,"付款"));
orderSteps.add(new OrderStep(00002L,"消费"));
orderSteps.add(new OrderStep(00002L,"完成"));
orderSteps.add(new OrderStep(00003L,"创建"));
orderSteps.add(new OrderStep(00003L,"付款"));
orderSteps.add(new OrderStep(00003L,"消费"));
orderSteps.add(new OrderStep(00003L,"完成"));
orderSteps.add(new OrderStep(00004L,"创建"));
orderSteps.add(new OrderStep(00004L,"付款"));
orderSteps.add(new OrderStep(00004L,"消费"));
orderSteps.add(new OrderStep(00004L,"完成"));
return orderSteps;
}
}
public class SyncDelayProducer {
public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
// 1.创建消息生产者,并且制定生产者组名
DefaultMQProducer producer = new DefaultMQProducer("group1");
// 设置超时时间
producer.setSendMsgTimeout(10000);
// 2.指定namesrv地址
producer.setNamesrvAddr("192.168.48.128:9876;192.168.48.137:9876");
// producer.setNamesrvAddr("47.96.165.93:9876;106.52.131.197:9876");
// 3.启动producer
try {
producer.start();
} catch (
MQClientException e) {
e.printStackTrace();
}
List orderStepList = OrderStep.buildOrders();
for (OrderStep order: orderStepList){
// 4.创建消息对象,指定主题topic,tag和消息体
Message msg = new Message("delayTopic","delay","delayOrder"+order.getOrderId(),order.getDesc().getBytes());
msg.setDelayTimeLevel(3);
// 5.发送消息,参数1:消息对象;参数2:消息队列的选择器;参数3:选择队列的业务标识(orderId)
SendResult result = producer.send(msg, new MessageQueueSelector() {
/**
*
* @param list 消息队列集合
* @param message 消息对象
* @param o 业务标识
* @return
*/
@Override
public MessageQueue select(List list, Message message, Object o) {
long orderId = (long) o;
int index = (int) (orderId%list.size());
return list.get(index);
}
},order.getOrderId());
SendStatus status = result.getSendStatus();
String msgId = result.getMsgId();
int queueId = result.getMessageQueue().getQueueId();
System.out.println("send status:"+status+",message ID:"+msgId+",queue:"+queueId+",desc:"+order.getDesc());
}
// * 关闭producer
producer.shutdown();
}
}
public class DelayConsumer {
public static void main(String[] args) {
consumer();
}
public static void consumer(){
// 1 创建消费者Consumer ,并确定消费者组名
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
// 2 指定NameServer地址
consumer.setNamesrvAddr("192.168.48.128:9876;192.168.48.137:9876");
// consumer.setNamesrvAddr("106.52.131.197:9876;47.96.165.93:9876");
try {
// 3 订阅主题Topic和Tag
consumer.subscribe("delayTopic","*");
// 4 设置回调函数,处理消息
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
for(MessageExt msg:list){
// 延迟时间 = 消费时间-存储到队列时间
System.out.println(Thread.currentThread().getName()+",延迟时间:"+(System.currentTimeMillis()-msg.getStoreTimestamp()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
// 5 启动消息内容
consumer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
}
}
ConsumeMessageThread_4,延迟时间:18541
ConsumeMessageThread_10,延迟时间:-28781067
ConsumeMessageThread_12,延迟时间:-28781111
ConsumeMessageThread_6,延迟时间:-28781075
ConsumeMessageThread_8,延迟时间:-28781078
ConsumeMessageThread_11,延迟时间:-28781069
ConsumeMessageThread_2,延迟时间:-28781072
ConsumeMessageThread_1,延迟时间:-28781061
ConsumeMessageThread_14,延迟时间:18479
ConsumeMessageThread_13,延迟时间:-28781017
ConsumeMessageThread_15,延迟时间:-28781022
ConsumeMessageThread_7,延迟时间:18515
ConsumeMessageThread_9,延迟时间:-28780995
ConsumeMessageThread_5,延迟时间:-28781063
ConsumeMessageThread_3,延迟时间:-28780952
ConsumeMessageThread_16,延迟时间:18454
不支持广播消费模式