轉換流,,,,, 緩衝流,,,,,Properties類,,,,,序列化流與反序列化流
阿新 • • 發佈:2020-08-02
1.匯入相應依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2.在測試類裡進行測試
package com.ws; import org.junit.jupiter.api.Test; import org.springframework.amqp.rabbit.core.RabbitTemplate; importorg.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @SpringBootTest class Springboot02AmqpApplicationTests { @Autowired RabbitTemplate rabbitTemplate; @Testvoid contextLoads() { /*message需要自己構造一個;定義訊息體內容和訊息頭 *rabbitTemplate.send(exchange,routeKey,message) * * object預設當成訊息體,只需要傳入傳送的物件,自動序列化發給rabbitmq * rabbitTemplate.convertAnd Send(exchange,routeKey,object) * */ Map<String, Object> map = new HashMap<>(); map.put("msg","one"); map.put("data", Arrays.asList("HelloWorld",111)); rabbitTemplate.convertAndSend("exchange.direct","ws.news",map); } @Test void test2(){ Object o = rabbitTemplate.receiveAndConvert("ws.news"); System.out.println(o.getClass()); System.out.println(o); } }
3.編寫自己的序列化Bean
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.amqp.support.converter.MessageConverter; @Configuration public class MyAMQPConfig { @Bean public MessageConverter messageConverter(){ return new Jackson2JsonMessageConverter(); } }