SpringBoot b2b2c 多用户商城系统(十五)Springboot整合RabbitMQ...
这篇文章带你了解怎么整合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();}}
SpringBoot b2b2c 多用户商城系统(十五)Springboot整合RabbitMQ...相关推荐
- SpringBoot b2b2c 多用户商城系统 ssm b2b2c
来源: SpringBoot b2b2c 多用户商城系统 ssm b2b2c 用java实施的电子商务平台太少了,使用spring cloud技术构建的b2b2c电子商务平台更少,大型企业分布式互联网 ...
- SpringBoot b2b2c 多用户商城系统 (一)构建第一个SpringBoot工程
简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...
- SpringBoot b2b2c 多用户商城系统
由于业务量与日俱增,在今年产品业务线表已经有2000多张,可以想象业务之复杂.以及工程之多. 几乎每周都有新项目需要成立,原来的ssm.ssh架构搭建太繁琐,于是转换到SpringBoot上面来. 并 ...
- SpringBoot b2b2c 多用户商城系统(八):配置中心服务化和高可用
server端改造 1.添加依赖 <dependencies><dependency><groupId>org.springframework.cloud</ ...
- 了解更多的B2B2C多用户商城系统模式
B2B2C多用户平台定制不受限于单一的电商平台运营模式,打造多用户商城独立系统,真正实现商家用户的自主经营,开创出多元化的网上模式.如何更进一步的了解多用户B2B2C商城系统,请详细阅读下文. 一.B ...
- 搭建B2B2C多用户商城系统需要注意的事项
电商行业一直处于高速发展,每年的"6.18""11.11"都是电商的狂欢节,这让许多的传统企业非常眼热,很多传统企业纷纷都想加入电商渠道,开拓线上渠道,这就需要 ...
- 小象电商是采用JAVA开发的B2B2C多用户商城系统。
简介: 小象电商是采用JAVA开发的B2B2C多用户商城系统.以"平台自营+多商户入驻"为主要经营模式,可快速帮客户打造类似"京东"一样的自营+招商入驻的经营模 ...
- JAVA b2b2c多用户商城系统源码-服务发现服务端EurekaServer微服务
一.大致介绍 1.众所周知,在现在互联网开发中,访问地址的IP和端口号是动态的,一个服务停掉再重新启用后IP和端口就可能发生了改变,所以用硬编码是肯定不行了.于是我们尝试使用新的技术来解决这一难题.需 ...
- 大商创x支持mysql版本_大商创x全面升级2.0,匠心打造b2b2c多用户商城系统
2018年大商创发布了全新的产品--大商创X,因为众多用户对大商创产品的期待,所以大商创X一经面世就引起大家的极大关注,这与其系统优势有着密不可分的联系. 如今距离第一代产品发布近2年时间,大商创X也 ...
最新文章
- 深度学习核心技术精讲100篇(八十一)-NLP预训练模型ERNIE实战应用案例
- 使用 commander inquirer 构建专业的node cli
- 有的时候入门只是一瞬间
- mysql排序规则错误_MySQL中“非法混合排序规则”错误的疑难解答
- js的事件循环机制,同步和异步,以及宏任务与微任务的执行顺序
- 下滑加载更多js_jquery实现移动端下拉加载更多
- JS客户端学习笔记二
- 用命令启动java我的世界_我的世界Minecraft Mod开发学习笔记 - 实现一个简单的命令Mod...
- 编程思想的理解(POP,OOP,SOA,AOP)
- 4.9. 触发器(Trigger)
- 微信小程序实现列表及tab标签
- 基于腾讯云搭建属于自己的Fiora聊天室
- 结构健康监测平台发展现状
- Vue的props的三种写法
- android wcf 上传文件,第二篇 ( wcf 与 android 图片上传下载)
- 快排为什么一定要从右边开始?
- 20. GD32F103C8T6入门教程-adc使用外部中断IT11触发启动adc规则通道,使用外中断IT15触发注入组
- QT图片处理+文字处理
- 提取谷歌游览器Cookie的五重境界
- 批处教程 for /f 中的Delims和Tokens总结