SpringAMQP--消息转换器
测试发送Object类型消息
1.声明队列
java
@Configuration
public class FanoutConfig {
@Bean
public Queue objectQueue(){
return new Queue("object.queue");
}
}
运行消费者后:

2.发送消息
java
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSendObjectQueue(){
Map<String,Object> msg = new HashMap<>();
msg.put("name","柳岩");
msg.put("age",26);
rabbitTemplate.convertAndSend("object.queue",msg);
}
}
运行发布者后:

3.使用基于JDK的ObjectOutputstream完成序列化
3.1在父工程中引入依赖(发布者与消费者都需要使用)
xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
3.2在发布者服务与消费者服务声明MessageConverter
java
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
3.3发布者服务结果
先清理object.queue队列中的信息

在运行发布者后:

3.4消费者服务结果
java
//消费者代码
@Component
public class SpringRabbitListener {
@RabbitListener(queues = "object.queue")
public void listenObjectQueue(Map<String,Object> msg){
System.out.println("接收到object.queue的消息:" + msg);
}
}
运行后的结果:
