rabbitmq学习8:spring-amqp的重要类的认识

本文介绍了Spring AMQP中核心类的功能与作用,包括Message、Exchange、Queue、Binding及AmqpTemplate等,深入探讨了它们如何协同工作实现消息传递。

   对于大多数应用来说都做了与spring整合,对于rabbitmq来说。也有与spring的整合。可能通过spring的官网找到spring-amqp项目下载。spring-amqp项目包括三个子项目:spring-amqp、spring-erlang、spring-rabbit.

   下面来认识一下spring-amqp中的几个重要类;以spring-amqp-1.0.0.M3版本为例

    1、Message : Spring AMQP定义的Message类是AMQP域模型中代表之一。Message类封装了body(消息BODY)和properties(消息属性) 。使得这个API看起来很简单。Message类定义如下:

public class Message {

	private final MessageProperties messageProperties;

	private final byte[] body;


	public Message(byte[] body, MessageProperties messageProperties) {
		this.body = body;
		this.messageProperties = messageProperties;
	}


	public byte[] getBody() {
		return this.body;
	}

	public MessageProperties getMessageProperties() {
		return this.messageProperties;
	}


	}

 

   其中MessageProperties类中定义了例如messageId、timestamp、contentType等属性。这此属性可以扩展到用户通过setHeader(String key, Object value)方法来自定义“headers”。

 2、Exchange

      Exchange接口代表一个AMQP的Exchange,决定消息生产者发送消息。每个Exchange都包括一个特定的唯一名字的虚拟主机的代理和一些其他属性。

 

public interface Exchange {

	String getName();

	String getType();

	boolean isDurable();

	boolean isAutoDelete();

	Map<String, Object> getArguments();

}

 

 其中 AbstractExchange类实现了Exchange类。而DirectExchange、TopicExchange、FanoutExchang、HeadersExchange四个类继承AbstractExchange。并重写了getType()类。根据各自相对应的Exchange类型。DirectExchange、TopicExchange、FanoutExchang、HeadersExchange分别对应的类型为direct,topic,fanout,headers.对应Exchange与routingkey的判定关系在前面的章节中已学习了!

 3、Queue

 Queue类是消息消费者接收消息中重要的一个组成部分。通过与Exchange判定来肯定消费者所接收的消息。伪代码如下:

public class Queue {
private final String name;
private volatile boolean durable;
private volatile boolean exclusive;
private volatile boolean autoDelete;
private volatile Map<String, Object> arguments;
public Queue(String name) {
this.name = name;
}
// Getters and Setters omitted for brevity

 

其中name表示队列的名称、durable表示持久性。true表示是。exclusive表示独占性。由于在AmqpTemplate中提供一个方法来得到唯一的队列。这个队列可能是一个”reply-to“地址或者其他信息,因此一般exclusive和autoDelete一般设定为true.

4、Binding

     Bingding类通过多种构造参数来判定Exchange,Queue,routingkey;例如

 

Binding(Queue queue, FanoutExchange exchange)

 

Binding(Queue queue, HeadersExchange exchange, Map<String, Object> arguments)
Binding(Queue queue, DirectExchange exchange)
Binding(Queue queue, DirectExchange exchange, String routingKey) 
Binding(Queue queue, TopicExchange exchange, String routingKey)

 

5、AmqpTemplate

AmqpTemplate是用来发送消息的模板类

public interface AmqpTemplate {

	// send methods for messages

	void send(Message message) throws AmqpException;

	void send(String routingKey, Message message) throws AmqpException;

	void send(String exchange, String routingKey, Message message) throws AmqpException;

	// send methods with conversion

	void convertAndSend(Object message) throws AmqpException;

	void convertAndSend(String routingKey, Object message) throws AmqpException;

	void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;

	void convertAndSend(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;

	void convertAndSend(String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;

	void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;

	// receive methods for messages

	Message receive() throws AmqpException;

	Message receive(String queueName) throws AmqpException;

	// receive methods with conversion

	Object receiveAndConvert() throws AmqpException;

	Object receiveAndConvert(String queueName) throws AmqpException;

	// send and receive methods for messages

	Message sendAndReceive(Message message) throws AmqpException;

	Message sendAndReceive(String routingKey, Message message) throws AmqpException;

	Message sendAndReceive(String exchange, String routingKey, Message message) throws AmqpException;

	// send and receive methods with conversion

	Object convertSendAndReceive(Object message) throws AmqpException;

	Object convertSendAndReceive(String routingKey, Object message) throws AmqpException;

	Object convertSendAndReceive(String exchange, String routingKey, Object message) throws AmqpException;

}

 6、AmqpAdmin和RabbitAdmin

   用户配置Queue、Exchange、Binding的代理类。代理类会自动声明或创建这些配置信息。

下面这个类用于异步接收消息的处理类

   7、MessageConverter 消息转换器类

 8、SimpleMessageListenerContainer 监听消息容器类

在引入 RabbitMQ 相关的 Maven 依赖时,如 `spring-boot-starter-amqp`、`spring-rabbit`、`spring-amqp` `spring-retry` 等模块后出现错误,通常可能由以下几个原因引起: ### 1. 依赖版本冲突 不同 Spring 模块之间可能存在版本不兼容的问题。例如,`spring-boot-starter-amqp` 已经包含了 `spring-amqp` `spring-rabbit` 的兼容版本,如果手动引入这些模块并指定版本号,可能会导致版本冲突。建议优先使用 Spring Boot 的默认版本管理机制,避免显式指定子依赖的版本[^1]。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` ### 2. 缺少必要的依赖 某些功能可能依赖额外的库,例如 `spring-retry` 需要引入对应的依赖才能生效。如果未正确引入,会导致运行时报错。应确保所有需要的功能模块都被正确引入: ```xml <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> ``` 此外,如果使用了 Spring Boot 的自动配置功能,应确保 `spring-boot-starter` 基础依赖已正确引入[^1]。 ### 3. 依赖作用域或排除配置不当 有时为了精简依赖或避免冲突,可能会使用 `<exclusion>` 排除某些依赖,但如果不小心排除了关键模块,也会导致运行时错误。检查 `pom.xml` 中是否错误地排除了 `spring-core`、`spring-context` 等核心模块[^1]。 ### 4. 依赖未正确下载或仓库配置问题 Maven 在下载依赖时可能出现网络问题或本地仓库损坏,导致部分依赖未正确解析。可以通过以下方式排查: - 删除本地 Maven 仓库中相关的依赖目录,强制重新下载。 - 检查 `settings.xml` 中的镜像配置是否影响了某些仓库的访问。 - 使用 `mvn dependency:tree` 查看依赖树,确认是否缺少必要的依赖节点。 ### 5. 自定义 Starter 的配置问题 若项目中使用了自定义封装的 Starter(如引用中提到的封装 `spring-boot-starter-amqp`),需确保其内部配置逻辑正确,并且与当前项目的 Spring Boot 版本兼容。例如,自定义 Starter 中的 `spring.factories` 文件应正确配置自动配置,避免因路径缺失或配置错误导致启动失败[^2]。 ### 6. 应用启动未正确配置 Spring Boot 应用需要有 `@SpringBootApplication` 注解的主来触发自动配置。如果缺少该注解,或自动配置未被正确加载,也会导致 RabbitMQ 相关 Bean 初始化失败。 ### 示例:正确配置 RabbitMQ 的 Starter 依赖 ```xml <dependencies> <!-- Spring Boot RabbitMQ Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- Spring Retry --> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> </dependencies> ``` ### 7. 配置文件中未正确设置 RabbitMQ 连接信息 即使依赖正确引入,若 `application.yml` 或 `application.properties` 中未配置 RabbitMQ 的连接参数(如 host、port、username、password),也会导致连接失败。应确保配置如下: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` ### 8. 依赖管理策略不统一 在大型项目或微服务架构中,若多个模块共享相同的 RabbitMQ 配置,建议统一管理配置文件,并通过自定义 Starter 实现配置的集中化复用[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值