RabbitMQ-Spring整合RabbitMQ
一、新增spring-rabbit依賴
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
二、生產者
public class Producer {
public static void main(String[] args) throws Exception {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:spring/rabbitmq-context.xml");
// RabbitMQ模板
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
// 傳送訊息
template.convertAndSend("Hello world!");
Thread.sleep(1000);// 休眠1秒
ctx.destroy(); // 容器銷燬
}
}
三、消費者
public class Receiver {
public void receive(String msg) {
System.out.println("消費者: " + msg);
}
}
四、建立rabbitmq-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 定義RabbitMQ的連線工廠 -->
<rabbit:connection-factory
id="connectionFactory" host="127.0.0.1" port="5672"
username="rabbitmq" password="rabbitmq" virtual-host="/" />
<!-- 定義Rabbit模板,指定連線工廠以及定義exchange -->
<rabbit:template id="amqpTemplate"
connection-factory="connectionFactory" exchange="fanoutExchange" />
<!-- MQ的管理,包括佇列、交換器等 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 定義佇列,自動宣告 -->
<rabbit:queue name="myQueue" auto-declare="true" />
<!-- 定義交換器,自動宣告 -->
<rabbit:fanout-exchange name="fanoutExchange"
auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="myQueue" />
</rabbit:bindings>
</rabbit:fanout-exchange>
<!-- 佇列監聽 -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener ref="receiver" method="receive"
queue-names="myQueue" />
</rabbit:listener-container>
<bean id="receiver" class="com.study.rabbitmq.spring.Receiver" />
</beans>
五、測試
執行生產者類的main方法,可以看到控制檯輸出如下內容:
相關文章
- rabbitMQ-Spring配置檔案MQSpring
- RabbitMQ -springboot整合rabbitmqMQSpring Boot
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- Springboot整合RabbitMQSpring BootMQ
- SpringBoot 整合 rabbitmqSpring BootMQ
- Java SpringBoot 整合 RabbitMQJavaSpring BootMQ
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- 整合RabbitMQ&SpringMQSpring
- spring boot-整合RabbitMq(RabbitMq基礎)Spring BootMQ
- RabbitMQ(三):RabbitMQ與Spring Boot簡單整合MQSpring Boot
- SpringBoot專案整合RabbitMQSpring BootMQ
- springboot2.0整合rabbitmqSpring BootMQ
- Rabbit學習---SpringBoot整合RabbitMQSpring BootMQ
- laravel微服務開發,整合rabbitmqLaravel微服務MQ
- RabbitMQ(二)JavaClient SpringBoot整合 Work queuesMQJavaclientSpring Boot
- spring-boot-route(十三)整合RabbitMQSpringbootMQ
- SpringBoot整合RabbitMQ(一)快速入門Spring BootMQ
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- Spring Boot(十三)RabbitMQ安裝與整合Spring BootMQ
- go-micro整合RabbitMQ實戰和原理GoMQ
- 每日一學:如何在 Spring Boot 整合 RabbitMQ ?Spring BootMQ
- Laravel5.6 整合 RabbitMQ 訊息佇列LaravelMQ佇列
- Spring Boot 整合 RabbitMQ 訊息事務(消費者)Spring BootMQ
- Spring Boot 整合 RabbitMQ 傳送延時訊息Spring BootMQ
- SpringBoot整合RabbitMQ實戰附加死信交換機Spring BootMQ
- SpringBoot 整合 RabbitMQ 實現訊息可靠傳輸Spring BootMQ
- SpringBoot2.0原始碼分析(三):整合RabbitMQ分析Spring Boot原始碼MQ
- ASP.NET Core2利用MassTransit整合RabbitMQASP.NETMQ
- SpringBoot整合rabbitMq實現訊息延時傳送Spring BootMQ
- SpringBoot2.0應用(三):SpringBoot2.0整合RabbitMQSpring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰二Spring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰一Spring BootMQ
- Java訊息佇列:RabbitMQ與Kafka的整合與應用Java佇列MQKafka
- Spring Boot系列十七 Spring Boot 整合 websocket,使用RabbitMQ做為訊息代理Spring BootWebMQ
- spring-boot快速入門學習筆記-整合JPA mybatis rabbitmq mongodb redisSpringboot筆記MyBatisMQMongoDBRedis
- RabbitMQ(一):RabbitMQ快速入門MQ