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
RabbitMq与Spring整合实例,整个工程采用maven,具体过程看博文: http://blog.csdn.net/evankaka/article/details/50495437
介绍Spring Cloud Stream与RabbitMQ集成的代码示例。Spring Cloud Stream是一个建立在Spring Boot和Spring Integration之上的框架,有助于创建事件驱动或消息驱动的微服务。
spring rabbitmq rpc 测试代码
spring整合rabbitmq需要的jar包(spring版本4.2.0)
spring rabbitmq amqp
java rabbitmq spring springAMQP 代码包 project
rabbitmq 与spring 使用maven集成示例源码 demo 示例。
https://www.jianshu.com/p/3841059f7ca3 整合spring和rabbitmq
spring3 + rabbitmq 包含配置文件demo,用到了fastjson 。消息生产者使用了消息确认,消费者使用了事务 用到的包基本都是最近的新版本:spring-amqp-1.6.0.RELEASE.jar spring-rabbit-1.3.5.RELEASE.jar spring-retry...
spring的2个数据源的配置,并且每个数据源都配置了事物管理。还有rabbitMQ的发送端代码。本人项目亲自用到的,可以运行。
springcloud bus rabbitmq 分布式队列 http://knight-black-bob.iteye.com/blog/2356839
rabbitmq + spring boot 消息确认、持久化、备用交换机、死信交换机等demo代码
spring-rabbitMQ.xml文件
这里两个项目,一个发送mq消息列队,另一个介绍mq消息,你需要配置安装rabbitmq
RabbitMQ与java、Spring结合实例详细讲解
RabbitMQ整合spring示例代码(java maven)