Spring Hot(3)Spring with RabbitMQ
1. Some Concepts
Work Queue
Exchange: direct, topic, headers, fanout
Core Classes —> ConnectionFactory, Connection, Channel, QueueingConsumer
http://wubin850219.iteye.com/blog/1007984
Core Features
spring-amqp, spring-erlang, spring-rabbit
Message
Exchange —> DirectExchange, TopicExchange, FanoutExchange, HeadersExchange
Queue
Binding —> Exchange, Queue, RoutingKey
AmqpTemplate ——> Send the Message
AmqpAdmin/RabbitAdmin ——> Queue, Exchange, Binding
MessageConverter ——> Convert the message
SimpleMessageListenerContainer ——> Listener
2. Working with Spring Send Message
Reading the samples from official website.
>git clone https://github.com/spring-projects/spring-amqp-samples.git
Import that into our Eclipse
>mvn eclipse:eclipse
3. Configuration on Spring
Here is the dependency configuration
<!-- spring RabbitMQ --><dependencyorg="org/springframework/amqp" name="spring-rabbit"rev="1.3.4.RELEASE"/><dependencyorg="org/springframework/amqp" name="spring-amqp"rev="1.3.4.RELEASE"/><dependencyorg="com/rabbitmq"name="amqp-client"rev="3.3.3"/>
Here is the spring configuration
<?xml version="1.0” encoding=“UTF-8”?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<rabbit:connection-factory
id="connectionFactory"
addresses="localhost:5672,localhost:5673"
username="guest" password="guest"/>
<rabbit:template
id="amqpTemplate"
connection-factory="connectionFactory" />
<rabbit:admin
id="containerGuest"
connection-factory="connectionFactory" />
<rabbit:queue
name="myqueue1"
declared-by="containerGuest" />
<rabbit:queue
name="myqueue2"
declared-by="containerGuest" />
<bean id="messageConverter" class="org.springframework.amqp.support.converter.SimpleMessageConverter"> </bean>
<bean id="receiveMessageHandler" class="com.sillycat.easyspringrabbitmqpublisher.main.ReceiveMessageHandler" />
<bean id="messageListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter"> <constructor-arg ref="receiveMessageHandler" />
<property name="defaultListenerMethod" value="handleMessage"></property> <property name="messageConverter" ref="messageConverter"></property> </bean>
<bean id="listenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"> <constructor-arg ref="connectionFactory" />
<property name="queues" ref="myqueue2" />
<property name="messageListener" ref="messageListenerAdapter" /> </bean>
</beans>
Sync Send and Receive Message
package com.sillycat.easyspringrabbitmqpublisher.main;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class SendOneMessageApp {
public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext(
"classpath:main-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
// Sync send Message
template.convertAndSend("myqueue1", "message1");
System.out.println("Done");
}
}
package com.sillycat.easyspringrabbitmqpublisher.main;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ReceiveOneMessageApp {
public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext(
"classpath:main-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
// Sync receive Message
String foo = (String) template.receiveAndConvert("myqueue1");
System.out.println("What I get from Sync = " + foo);
}
}
Async Handle Message
package com.sillycat.easyspringrabbitmqpublisher.main; import java.util.Date; publicclass ReceiveMessageHandler { publicvoid handleMessage(String message){ System.out.println("Received " + message + " at " + new Date()); } }
All the details are in project easyspringrabbitmqpublisher.
Tips
1. Permission Problem
Error Message:
Caused by: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:338)at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:590)
Solutions:
Visit this page and change what you want.
http://localhost:15672/#/users
References:
http://wubin850219.iteye.com/blog/1002932 Simple Working Queue, Buffering
http://wubin850219.iteye.com/blog/1003840 Working Queue, Split tasks to workers
http://wubin850219.iteye.com/blog/1004921 Publish/Subscribe
http://wubin850219.iteye.com/blog/1004948 Routing
http://wubin850219.iteye.com/blog/1004973 Topics
http://wubin850219.iteye.com/blog/1007984 Core Class
http://sillycat.iteye.com/blog/2066116 Configure the Cluster
http://projects.spring.io/spring-amqp/#quick-start
http://docs.spring.io/spring-amqp/docs/1.3.4.RELEASE/reference/html/quick-tour.html#d4e59
http://wubin850219.iteye.com/blog/1050279 sync
http://wubin850219.iteye.com/blog/1050328 async
Samples
https://github.com/spring-projects/spring-amqp-samples
User Management
https://www.rabbitmq.com/man/rabbitmqctl.1.man.html
相关推荐
rabbitmq spring rabbitmq spring rabbitmq spring rabbitmq spring http://knight-black-bob.iteye.com/blog/2304089
3. **编写生产者代码**:在Spring Bean中创建一个发送消息的方法,使用`RabbitTemplate`来发送消息到指定的交换机和路由键。 ```java @Service public class RabbitProducer { @Autowired private RabbitTemplate ...
介绍Spring Cloud Stream与RabbitMQ集成的代码示例。Spring Cloud Stream是一个建立在Spring Boot和Spring Integration之上的框架,有助于创建事件驱动或消息驱动的微服务。
首先,我们要理解Spring对RabbitMQ的支持主要体现在Spring AMQP项目中,它为RabbitMQ提供了一套高级抽象层,使得开发者能够更加便捷地使用RabbitMQ。在整合Spring和RabbitMQ时,我们需要引入相应的依赖。在Maven工程...
当我们将Spring与RabbitMQ整合时,可以利用Spring的IOC(Inversion of Control)和AOP(Aspect-Oriented Programming)特性来优雅地管理消息的生产与消费。 在"spring整合rabbitmq需要的jar包(spring版本4.2.0)...
Spring RabbitMQ RPC(远程过程调用)是一种使用RabbitMQ实现客户端与服务器间通信的方式,它允许客户端发送请求到服务器,然后服务器处理请求并返回结果。在这个场景中,RabbitMQ作为一个消息中间件,帮助解耦应用...
在SpringCloud框架中集成RabbitMQ,可以实现高效的消息通信,增强系统的可扩展性和容错性。RabbitMQ是一款开源的消息代理和队列服务器,它使用AMQP(Advanced Message Queuing Protocol)协议,广泛应用于分布式系统...
3. **配置RabbitMQ连接**:在Spring的配置文件中,我们需要定义一个`CachingConnectionFactory`来建立与RabbitMQ服务器的连接。配置包括主机名、端口、用户名和密码等信息。 4. **声明Exchange和Queue**:通过`...
本示例聚焦于Spring的事务管理和RabbitMQ的使用,这都是分布式系统中不可或缺的组件。 首先,让我们深入了解Spring的事务管理。在Java环境中,事务管理是确保数据一致性的重要手段。Spring提供了一种声明式事务管理...
3. **Spring框架**:Spring是Java开发中最流行的框架之一,它提供了一个全面的编程和配置模型,简化了企业级Java应用的开发。Spring的核心特性包括依赖注入、面向切面编程(AOP)、数据访问/集成、Web应用、测试以及更...
Spring RabbitMQ AMQP 是一个基于Java的开源框架,它整合了RabbitMQ消息中间件,实现了高级消息队列协议(AMQP)。这个框架是Spring生态的一部分,为Spring Boot应用程序提供了强大的消息处理能力。AMQP是一种标准的...
3. 如何在Spring Cloud项目中引入RabbitMQ依赖。 4. 创建RabbitMQ的配置,如连接参数、交换机、队列和绑定。 5. 使用Spring Cloud的RabbitMQ客户端API发送和接收消息。 6. Ribbon或Feign在服务间调用中的应用,以及...
1. 引入依赖:在项目中添加RabbitMQ的Spring整合依赖,如`spring-amqp`库。 2. 配置RabbitMQ:在Spring的配置文件中,定义连接工厂、信道配置以及RabbitMQ服务器的相关属性。 3. 创建消息模板:使用`RabbitTemplate`...
spring3 + rabbitmq 包含配置文件demo,用到了fastjson 。消息生产者使用了消息确认,消费者使用了事务 用到的包基本都是最近的新版本:spring-amqp-1.6.0.RELEASE.jar spring-rabbit-1.3.5.RELEASE.jar spring-retry...
3. 定义消息监听容器:创建一个`SimpleMessageListenerContainer`,这是Spring AMQP提供的一个bean,用于接收消息。 4. 创建消息处理器:定义一个`MessageListenerAdapter`,作为实际处理消息的组件。 5. 创建...
首先,我们需要在项目中引入Spring AMQP库,这是Spring对RabbitMQ的官方支持。可以通过在pom.xml文件中添加以下Maven依赖: ```xml <groupId>org.springframework.amqp <artifactId>spring-rabbit 您的版本号 ...
3. **声明消息监听器**:通过实现`RabbitListener`注解或配置`SimpleMessageListenerContainer`,定义消息监听器以接收和处理来自RabbitMQ的消息。监听器可以是匿名的,也可以关联到特定的队列。 4. **发送消息**:...
RabbitMQ作为一款流行的开源消息中间件,广泛应用于Spring Boot项目中。本教程将详细介绍如何在Spring Boot应用中结合RabbitMQ实现消息确认、消息持久化、备用交换机以及死信交换机等功能。 首先,让我们理解这些...