(十五)java B2B2C 多级Springboot多租户电子商城系统 Springboot整合RabbitMQ
Spring cloud b2b2c電子商務社交平臺源碼請加企鵝求求:一零三八七七四六二六。這篇文章帶你了解怎么整合RabbitMQ服務器,并且通過它怎么去發送和接收消息。我將構建一個springboot工程,通過RabbitTemplate去通過MessageListenerAdapter去訂閱一個POJO類型的消息。 準備工作 15min IDEA maven 3.0 在開始構建項目之前,機器需要安裝rabbitmq,你可以去官網下載,http://www.rabbitmq.com/download.html ,如果你是用的Mac,你可以這樣下載:
brew install rabbitmq復制代碼安裝完成后開啟服務器:
rabbitmq-server復制代碼開啟服務器成功,你可以看到以下信息:
RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.Licensed under the MPL. See http://www.rabbitmq.com/Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log/usr/local/var/log/rabbitmq/rabbit@localhost-sasl.logStarting broker... completed with 6 plugins.復制代碼構建工程
構架一個SpringBoot工程,其pom文件依賴加上spring-boot-starter-amqp的起步依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>復制代碼創建消息接收者
在任何的消息隊列程序中,你需要創建一個消息接收者,用于響應發送的消息。
@Component public class Receiver {private CountDownLatch latch = new CountDownLatch(1);public void receiveMessage(String message) {System.out.println("Received <" + message + ">");latch.countDown();}public CountDownLatch getLatch() {return latch;}}復制代碼消息接收者是一個簡單的POJO類,它定義了一個方法去接收消息,當你注冊它去接收消息,你可以給它取任何的名字。其中,它有CountDownLatch這樣的一個類,它是用于告訴發送者消息已經收到了,你不需要在應用程序中具體實現它,只需要latch.countDown()就行了。
創建消息監聽,并發送一條消息
在spring程序中,RabbitTemplate提供了發送消息和接收消息的所有方法。你只需簡單的配置下就行了:
- 需要一個消息監聽容器
- 聲明一個quene,一個exchange,并且綁定它們
- 一個組件去發送消息
代碼清單如下:
package com.forezp;import com.forezp.message.Receiver; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean;@SpringBootApplication public class SpringbootRabbitmqApplication {final static String queueName = "spring-boot";@BeanQueue queue() {return new Queue(queueName, false);}@BeanTopicExchange exchange() {return new TopicExchange("spring-boot-exchange");}@BeanBinding binding(Queue queue, TopicExchange exchange) {return BindingBuilder.bind(queue).to(exchange).with(queueName);}@BeanSimpleMessageListenerContainer container(ConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.setQueueNames(queueName);container.setMessageListener(listenerAdapter);return container;}@BeanMessageListenerAdapter listenerAdapter(Receiver receiver) {return new MessageListenerAdapter(receiver, "receiveMessage");}public static void main(String[] args) {SpringApplication.run(SpringbootRabbitmqApplication.class, args);} }復制代碼創建一個測試方法:
@Component public class Runner implements CommandLineRunner {private final RabbitTemplate rabbitTemplate;private final Receiver receiver;private final ConfigurableApplicationContext context;public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,ConfigurableApplicationContext context) {this.receiver = receiver;this.rabbitTemplate = rabbitTemplate;this.context = context;}@Overridepublic void run(String... args) throws Exception {System.out.println("Sending message...");rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);context.close();}}復制代碼啟動程序,你會發現控制臺打印
Spring Cloud大型企業分布式微服務云構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六
轉載于:https://juejin.im/post/5cde707f51882525ae1f4312
總結
以上是生活随笔為你收集整理的(十五)java B2B2C 多级Springboot多租户电子商城系统 Springboot整合RabbitMQ的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python实现二分查找
- 下一篇: java信息管理系统总结_java实现科