交换机属性-持久化和自动删除
- 1、交换机属性
- 2、交换机(Exchange)的持久化属性
- 2.1、RabbitConfig配置类(关键代码)
- 2.2、发送消息
- 2.3、启动类
- 2.4、application.yml配置文件
- 2.5、pom.xml配置文件
- 2.6、测试
- 3、交换机(Exchange)的自动删除属性
- 3.1、RabbitConfig配置类(关键代码)
- 3.2、发送消息
- 3.3、启动类
- 3.4、application.yml配置文件
- 3.5、pom.xml配置文件
- 3.6、测试
1、交换机属性
1、Name:交换机名称;就是一个字符串
2、Type:交换机类型,direct, topic, fanout, headers四种
3、Durability:持久化,声明交换机是否持久化,代表交换机在服务器重启后是否还存在;
4、Auto delete:是否自动删除,曾经有队列绑定到该交换机,后来解绑了,那就会自动删除该交换机;
5、Internal:内部使用的,如果是yes,客户端无法直接发消息到此交换机,它只能用于交换机与交换机的绑定。
6、Arguments:只有一个取值alternate-exchange,表示备用交换机;
2、交换机(Exchange)的持久化属性
交换机默认是持久化的,即创建后重启rabbitmq服务器交换机也不会丢失。
2.1、RabbitConfig配置类(关键代码)
package com.power.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitConfig {@Value("${my.exchangeName}")private String exchangeName;@Value("${my.queueName}")private String queueName;//创建交换机@Beanpublic DirectExchange directExchange(){return ExchangeBuilder //交换机默认是持久化的.directExchange(exchangeName).durable(false) //durable值默认是true,如果此处设置为false,那么Rabbitmq服务器重启后交换机就不会持久化,会自动删除.build();}//创建队列@Beanpublic Queue queue(){return QueueBuilder.durable(queueName).build();}@Beanpublic Binding binding(DirectExchange exchangeName,Queue queueName){return BindingBuilder.bind(queueName).to(exchangeName).with("info");}
}
2.2、发送消息
package com.power.service;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;@Service
@Slf4j
public class MessageService {@Resourceprivate RabbitTemplate rabbitTemplate;@Value("${my.exchangeName}")private String exchangeName;@Beanpublic void sendMsg(){Message message = MessageBuilder.withBody("hello world".getBytes()).build();rabbitTemplate.convertAndSend(exchangeName,"info",message);log.info("消息发送完毕,发送时间是:"+new Date());}
}
2.3、启动类
package com.power;import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.annotation.Resource;@SpringBootApplication
public class Application implements ApplicationRunner {@Resourceprivate MessageService messageService;public static void main(String[] args) {SpringApplication.run(Application.class);}@Overridepublic void run(ApplicationArguments args) throws Exception {messageService.sendMsg();}
}
2.4、application.yml配置文件
server:port: 8080
spring:application:name: rabbit_10_exchange_properties01rabbitmq:host: 你的服务器IPport: 5672username: 你的账号password: 你的密码virtual-host: powermy:exchangeName: exchange.properties.01queueName: queue.properties.01
2.5、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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.power</groupId><artifactId>rabbit_10_exchange_properties01</artifactId><version>1.0-SNAPSHOT</version><name>rabbit_10_exchange_properties01</name><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.13</version><relativePath/></parent><dependencies><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></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.6、测试
先启动服务,然后登录rabbitmq服务器查看,发现新创建的交换机并没有被持久化。
关闭rabbitmq服务器,再重新rabbitmq
登录rabbitmq后台,查看刚刚创建的交换机不见了
3、交换机(Exchange)的自动删除属性
3.1、RabbitConfig配置类(关键代码)
package com.power.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitConfig {@Value("${my.exchangeName}")private String exchangeName;@Value("${my.queueName}")private String queueName;//创建交换机@Beanpublic DirectExchange directExchange(){return ExchangeBuilder //交换机默认不是自动删除的。即如果队列和交换机解绑,默认情况下不会自动删除交换机.directExchange(exchangeName).autoDelete()//不设该值是默认不自动删除,设完后自动删除。.build();}//创建队列@Beanpublic Queue queue(){return QueueBuilder.durable(queueName).build();}@Beanpublic Binding binding(DirectExchange exchangeName,Queue queueName){return BindingBuilder.bind(queueName).to(exchangeName).with("info");}
}
3.2、发送消息
代码同上
3.3、启动类
代码同上
3.4、application.yml配置文件
server:port: 8080
spring:application:name: rabbit_10_exchange_properties01rabbitmq:host: 你的服务器IPport: 5672username: 你的账号password: 你的密码virtual-host: powermy:exchangeName: exchange.properties.02queueName: queue.properties.02
3.5、pom.xml配置文件
同上
3.6、测试
先启动服务,然后登录rabbitmq服务器查看,发现新创建的交换机自动删除属性是true。
进入该交换机,尝试解绑交换机和队列
解绑完成后,回到交换机列表页面,发现交换机exchange.properties.02自动删除了