zoukankan      html  css  js  c++  java
  • rabbitmq学习之路(六)

    上一次说到了消息的消费,今天具体说一下消息消费有哪几种形式

    消息有两种模式来消费

    第一种:Subscribe订阅的方式

    这种也是我们比较熟悉的方式,例如前面我们写的代码,用@RabbitListener这样的方式去监听某一个队列,一旦队列中有消息,就会自动下发给消费者

    第二种:Poll拉取消息

    这一种是消费者主动去队列中拉取消息进行消费,是可控的,主动的

    参考APi

    Message receive() throws AmqpException;

    Message receive(String queueName) throws AmqpException;

    Message receive(long timeoutMillis) throws AmqpException;

    Message receive(String queueName, long timeoutMillis) throws AmqpException;

    同样的,这种方式也可以获取对象

    Object receiveAndConvert() throws AmqpException;

    Object receiveAndConvert(String queueName) throws AmqpException;

    Object receiveAndConvert(long timeoutMillis) throws AmqpException;

    Object receiveAndConvert(String queueName, long timeoutMillis) throws AmqpException;

    <T> T receiveAndConvert(ParameterizedTypeReference<T> type) throws AmqpException;

    <T> T receiveAndConvert(String queueName, ParameterizedTypeReference<T> type) throws AmqpException;

    <T> T receiveAndConvert(long timeoutMillis, ParameterizedTypeReference<T> type) throws AmqpException;

    <T> T receiveAndConvert(String queueName, long timeoutMillis, ParameterizedTypeReference<T> type)

    throws AmqpException;

    用这种泛型的方式

    使用这四个方法需要配置org.springframework.amqp.support.converter.SmartMessageConverter,这是一个接口,Jackson2JsonMessageConverter已经实现了这个接口,所以只要将Jackson2JsonMessageConverter设置到RabbitTemplate中即可。

    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());

    原文链接:https://blog.csdn.net/weixin_38380858/article/details/84258507

  • 相关阅读:
    JavaScript中{}+{}
    网站性能优化
    C++是如何从代码到游戏的?
    C++是如何从代码到游戏的?
    【力扣】至少是其他数字两倍的最大数 中速题解
    代码编辑器选择Atom还是VScode?
    TIOBE 4 月榜单:少儿编程语言 Scratch 进入 TOP 20
    熟悉一下oncontextmenu事件的知识
    input属性type为file打开文件资源管理器时,如何限制多次选取或只能一次选取的行为
    HTML5的拖放事件
  • 原文地址:https://www.cnblogs.com/changeCode/p/11313264.html
Copyright © 2011-2022 走看看