p88-高级-消息- RabbitTemplate发送接受消息&序列化机制

SmarTTY中首先搭建环境,过程如图所述,每次只需要走这几步,就可以在rabbitmq的客户端访问到:
在这里插入图片描述

RabbitTemplate发送接受消息&序列化机制
1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.atguigu</groupId>
	<artifactId>springboot-03-amqp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-03-amqp</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
***2、application.properties***

```java
spring.rabbitmq.host=192.168.1.113
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#spring.rabbitmq.port=5672
#spring.rabbitmq.virtual-host=

3、public class Springboot03AmqpApplication

package amqp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* 自动配置
*1、这是rabbitmq的自动配置类RabbitAutoConfiguration
*2、有自动配置了连接工厂ConnectionFactory
*3、RabbitProperties封装了 RabbitMQ的配置
*4、RabbitTemplate:给RabbitMQ发送和接收消息,就像之前学过的jdbcTemplate/RedisTemplate
*5、amqpAdmin:RabbitMQ系统管理功能组件
* */
@SpringBootApplication
public class Springboot03AmqpApplication {

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

}

4、class Springboot03AmqpApplicationTests

package amqp;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.awt.print.Book;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class Springboot03AmqpApplicationTests {

	@Autowired
	RabbitTemplate rabbitTemplate;


	/*
	 * 1、单播(点对点)
	 * */

	@Test
	public void contextLoads() {
		//message需要自己构造一个;定义消息体内容和消息头
		//rabbitTemplate.send(exchange,routeKey,message);

		//默认是object是消息体,只需要传入要发送的对象,自动序列化,然后保存发送给rabbitMQ
		//rabbitTemplate.convertAndSend(exchange,routeKey,object);
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "这是第一个消息");
		map.put("data", Arrays.asList("helloworldworld", 123, true));
		//对象被默认序列化以后发送出去,这是发送点对点消息
		//rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
		rabbitTemplate.convertAndSend("exchange.direct", "atguigu.news", new amqp.bean.Book("西游记", "吴承恩"));

	}

	//接收数据,如何将数据自动的转为json发送出去
	@Test
	public void receive() {
		Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
		System.out.println(o.getClass());
		System.out.println(o);
	}

	/*广播*/

/*	@Test
	public void sendMsg() {
		rabbitTemplate.convertAndSend("exchange.fanout", "", new amqp.bean.Book("三国演义", "罗贯中"));
	}*/
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1、
在这里插入图片描述
2在这里插入图片描述

3
在这里插入图片描述

4
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值