rocketmq 示例(spring boot)延时消息


rocketmqTemplate 延时消息

 

***********************

引入 jar 包

 


    org.apache.rocketmq
    rocketmq-spring-boot-starter
    2.0.3

 

************************

相关类

 

public class RocketMQTemplate extends AbstractMessageSendingTemplate implements InitializingBean, DisposableBean {

    public SendResult syncSend(String destination, Message message, long timeout, int delayLevel) {
                      //同步延时发送

   public void asyncSend(String destination, Message message, SendCallback sendCallback, long timeout, int delayLevel) {
                      //异步延时发送
   。。。。

}

 

**********************************************

使用示例

 

******************************

producer 端

 

@Service
public class ProducerService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public void sendDelay(){
        SendResult result=rocketMQTemplate.syncSend("topic-1:delay",MessageBuilder.withPayload("瓜田李下 delay").build(),2000,2);
        System.out.println("发送时间:"+System.currentTimeMillis());
        System.out.println(result);
    }

}

 

*************************************

consumer 端

 

@Service
@RocketMQMessageListener(consumerGroup = "consumerGroup",topic = "topic-1",selectorExpression = "*")
public class ConsumerService implements RocketMQListener {

    @Override
    public void onMessage(String s) {
        System.out.println("消费时间:"+System.currentTimeMillis());
        System.out.println(s);
    }
}

 

**************************************

控制台输出

 

      

 

 

 

 

你可能感兴趣的:(rocketmq)