首页 > 其他分享 >RabbitMQ 消息转换器

RabbitMQ 消息转换器

时间:2023-12-02 22:35:18浏览次数:31  
标签:amqp org springframework 消息 import msg 转换器 RabbitMQ public

 代码示例:

1. 引入依赖

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

2. 在启动类中创建Bean

package com.itheima;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ErpApplication {

public static void main(String[] args) {
SpringApplication.run(ErpApplication.class, args);
}

@Bean
public MessageConverter jacksonMessageConvertor() {
return new Jackson2JsonMessageConverter();
}

}

3. 测试:发送者发送消息给队列

@GetMapping("/mq05")
public void mq05(){
String queueName = "object.queue";
Map<String, Object> msg = new HashMap<>();
msg.put("name", "jack");
msg.put("age", 21);
rabbitTemplate.convertAndSend(queueName, msg);//队列名称、要发送的消息
}

对比:下面的是加了消息转换器的,上面的是没加的时候的

 

5. 消费者从队列中接收消息(跟之前一样)

@RabbitListener(queues = "object.queue")
public void listenQueue07(Map<String, Object> msg) {
System.out.println("消费者收到了object.queue的消息:" + msg);
}

 

标签:amqp,org,springframework,消息,import,msg,转换器,RabbitMQ,public
From: https://www.cnblogs.com/gagaya2/p/17872358.html

相关文章

  • 消息转换器
    Spring会把你发送的消息序列化为字节发送给MQ,接收消息的时候,还会把字节反序列化为Java对象。只不过,默认情况下Spring采用的序列化方式是JDK序列化。众所周知,JDK序列化存在下列问题:数据体积过大有安全漏洞可读性差c配置JSON转换器显然,JDK序列化方式并不合适。我们希......
  • RabbitMQ Java代码声明队列和交换机(方法一)
      交换机和队列的声明一般写在消费者模块里 代码示例:packagecom.itheima.config_RabbitMQ;importorg.springframework.amqp.core.*;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@Configuration......
  • RabbitMQ Topic交换机
     代码示例:1.新建两个队列 2.创建交换机,名字叫hmall.topic,类型选择topic 3.hmall.topic交换机绑定第一步的两个队列,绑定过程中填写RoutingKey  4.编写消费者代码监听这两个队列@RabbitListener(queues="topic.queue1")publicvoidlistenQueue05(Str......
  • RabbitMQ Direct交换机
     代码示例:1.交换机绑定了两个队列,并给它们设置了RoutingKey2. publisher发送者给Direct交换机发消息时,第二个参数指定RoutingKey:@GetMapping("/mq03")publicvoidmq03(){StringexchangeName="hmall.direct";Stringmsg="hello,红色";//三个参数:......
  • RabbitMQ 发送消息到交换机
    发送消息到交换机的代码:@GetMapping("/mq02")//发送消息给交换机publicvoidmq02(){StringexchangeName="hmall.fanout";Stringmsg="hello,每个人";//三个参数:交换机名称、RoutingKey(暂时为空)、要发送的消息rabbitTemplate.convertAndSend(exchangeName,......
  • RabbitMQ Fanout交换机
     容易搞混的点:1.假如publisher给Fanout交换机发送了一条消息,那么Fanout交换机会给每一个绑定到它身上的队列都发送这条消息,也就是说有多少个队列跟它绑定了,这条消息就有几份,每个队列都收到一份。2.假如一个队列绑定了多个消费者,那么该队列在给消费者投递消息时就是轮询,一......
  • rabbitmq的推(push)拉(pull)模式介绍及代码实现
    在rabbitmq中有两种消息处理的模式,一种是推模式/订阅模式/投递模式(也叫push模式),消费者调用channel.basicConsume方法订阅队列后,由RabbitMQ主动将消息推送给订阅队列的消费者;另一种是拉模式/检索模式(也叫pull模式),需要消费者调用channel.basicGet方法,主动从指定队列中拉取消息。推......
  • RabbitMQ work模型
    默认情况下,MQ队列如果绑定了多个消费者,那么队列在投递消息时就是轮询,一人投递一个(并且一条消息只能投递给监听该队列的某一个消费者)在一个MQ队列上绑定多个消费者的目的是加快队列中消息的处理效率,防止队列中消息的堆积问题。 注:要在消费者的application.yml文件中加上这个......
  • RabbitMQ 接收队列的消息
     代码示例:注:要把这个类加上Component注解packagecom.itheima.amqp_listener;importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;@ComponentpublicclassMQListener{@RabbitListener(queues="simpl......
  • RabbitMQ 发送消息到队列(交换机不参与的那种)
    1.导包<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>2.在application.yml文件里编写配置信息spring:rabbitmq:host:192.168.88.130port:5672......