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

猜你喜欢

转载自www.cnblogs.com/changeCode/p/11313264.html