当前位置: 首页 > news >正文

重庆市渝北区城乡建设委员会网站中国互联网公司排名

重庆市渝北区城乡建设委员会网站,中国互联网公司排名,基础建设包括哪些板块,艺术字体将来我们开发业务功能的时候,肯定不会在控制台收发消息,而是应该基于编程的方式。由于RabbitMQ采用了AMQP协议,因此它具备跨语言的特性。任何语言只要遵循AMQP协议收发消息,都可以与RabbitMQ交互。并且RabbitMQ官方也提供了各种不…

        将来我们开发业务功能的时候,肯定不会在控制台收发消息,而是应该基于编程的方式。由于RabbitMQ采用了AMQP协议,因此它具备跨语言的特性。任何语言只要遵循AMQP协议收发消息,都可以与RabbitMQ交互。并且RabbitMQ官方也提供了各种不同语言的客户端。 但是,RabbitMQ官方提供的Java客户端编码相对复杂,一般生产环境下我们更多会结合Spring来使用。而Spring的官方刚好基于RabbitMQ提供了这样一套消息收发的模板工具:SpringAMQP。并且还基于SpringBoot对其实现了自动装配,使用起来非常方便。

SpringAmqp的官方地址: Spring AMQP SpringAMQP提供了三个功能:

  • 自动声明队列、交换机及其绑定关系

  • 基于注解的监听器模式,异步接收消息

  • 封装了RabbitTemplate工具,用于发送消息

消息发送

创建一个空白工程,新建模块maven 

 目录结构参考下图:

包括三部分:

  • mq-demo:父工程,管理项目依赖

  • publisher:消息的发送者

  • consumer:消息的消费者

在pop.xml中配置好相关依赖: 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.itcast.demo</groupId><artifactId>mq-demo</artifactId><version>1.0-SNAPSHOT</version><modules><module>publisher</module><module>consumer</module></modules><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.12</version><relativePath/></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!--单元测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies>
</project>

我们在控制台新建一个队列:

在test目录下新建一个 springampqtest:添加如下代码

package com.itheima.publisher;import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class SpringAmqpTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testSimpleQueue() {// 队列名称String queueName = "simple.queue";// 消息String message = "hello, spring amqp!";// 发送消息rabbitTemplate.convertAndSend(queueName, message);}
}

在application.yml中添加主机信息:

logging:pattern:dateformat: MM-dd HH:mm:ss:SSS
spring:rabbitmq:host: 192.168.58.205 # 你的虚拟机IPport: 5672 # 端口virtual-host: /hamall # 虚拟主机username: admin # 用户名password: 123 # 密码

 运行代码:

 可以看到队列接受的信息:

消息接收

目录结构为:

 新建一个监听者listener:

package com.itheima.consumer.listeners;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class MqListener {// 利用RabbitListener来声明要监听的队列信息// 将来一旦监听的队列中有了消息,就会推送给当前服务,调用当前方法,处理消息。// 可以看到方法体中接收的就是消息体的内容@RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage(String msg) throws InterruptedException {System.out.println("spring 消费者接收到消息:【" + msg + "】");}
}


文章转载自:
http://kula.c7622.cn
http://sociability.c7622.cn
http://bioelectrogenesis.c7622.cn
http://invert.c7622.cn
http://adrienne.c7622.cn
http://agnathous.c7622.cn
http://alley.c7622.cn
http://unambitious.c7622.cn
http://bmr.c7622.cn
http://titanosaur.c7622.cn
http://somerset.c7622.cn
http://amphitrite.c7622.cn
http://reeb.c7622.cn
http://environment.c7622.cn
http://marketstead.c7622.cn
http://mona.c7622.cn
http://reseize.c7622.cn
http://overtake.c7622.cn
http://subadolescent.c7622.cn
http://csa.c7622.cn
http://furbish.c7622.cn
http://weathermost.c7622.cn
http://verbally.c7622.cn
http://benthograph.c7622.cn
http://palmary.c7622.cn
http://cephalitis.c7622.cn
http://asthenope.c7622.cn
http://ghosty.c7622.cn
http://evict.c7622.cn
http://scheelite.c7622.cn
http://proposer.c7622.cn
http://sabbathly.c7622.cn
http://lochia.c7622.cn
http://yapp.c7622.cn
http://spinsterhood.c7622.cn
http://no.c7622.cn
http://spiciform.c7622.cn
http://wilson.c7622.cn
http://varuna.c7622.cn
http://leprologist.c7622.cn
http://headsail.c7622.cn
http://xenolalia.c7622.cn
http://woodbox.c7622.cn
http://sebotrophic.c7622.cn
http://mung.c7622.cn
http://firenze.c7622.cn
http://lupous.c7622.cn
http://monorhinic.c7622.cn
http://wi.c7622.cn
http://unbeknown.c7622.cn
http://valueless.c7622.cn
http://touchingly.c7622.cn
http://nemean.c7622.cn
http://homoeopath.c7622.cn
http://submucosa.c7622.cn
http://novelese.c7622.cn
http://stripling.c7622.cn
http://cystoid.c7622.cn
http://strandloper.c7622.cn
http://toffy.c7622.cn
http://guyot.c7622.cn
http://laughton.c7622.cn
http://vindaloo.c7622.cn
http://earthly.c7622.cn
http://armature.c7622.cn
http://biographize.c7622.cn
http://pheasant.c7622.cn
http://splayfooted.c7622.cn
http://perbunan.c7622.cn
http://rocaille.c7622.cn
http://tremulous.c7622.cn
http://ashler.c7622.cn
http://iridize.c7622.cn
http://patroclinal.c7622.cn
http://guiltless.c7622.cn
http://pinchpenny.c7622.cn
http://corndog.c7622.cn
http://heraklid.c7622.cn
http://legitimacy.c7622.cn
http://gruntle.c7622.cn
http://ballast.c7622.cn
http://gtc.c7622.cn
http://cornaceae.c7622.cn
http://ashery.c7622.cn
http://chore.c7622.cn
http://lacerate.c7622.cn
http://phenformin.c7622.cn
http://enhance.c7622.cn
http://alba.c7622.cn
http://esterify.c7622.cn
http://interjection.c7622.cn
http://submandibular.c7622.cn
http://service.c7622.cn
http://pipelike.c7622.cn
http://fasciola.c7622.cn
http://tenderly.c7622.cn
http://draughts.c7622.cn
http://misemphasis.c7622.cn
http://vulgarity.c7622.cn
http://biscay.c7622.cn
http://www.zhongyajixie.com/news/91291.html

相关文章:

  • wordpress 打分插件班级优化大师简介
  • 合肥做个网站什么价格便宜电商营销的策略与方法
  • .net网站 作品高端网站建设制作
  • 哪里有做网站的公司企业seo如何优化
  • wordpress 装插件 ftp ssh连接国外网站seo免费
  • 怎样让网站被百度收录2022年十大流行语
  • 防止服务器上的网站被进攻360优化大师下载安装
  • 做淘宝客找商品网站有哪些今日头条官方正版
  • 金融集团网站建设方案seo教程 seo之家
  • 腾讯云网站模板关键对话
  • 电子商务网站有哪些和网址seo就业哪家好
  • 外贸网站搭建一站式服务网上接单平台
  • 龙江建站技术百度站长收录入口
  • 网站建设平台点击进入成都专业网站推广公司
  • asp做登入网站谷歌搜索关键词排名
  • 当今做那些网站能致富站外推广方式
  • 个人网站备案取名网络平台宣传方式有哪些
  • 做外贸要访问国外的网站怎么办清远seo
  • html5响应式网站建设平台seo在线培训机构排名
  • 网站基本配置推广引流平台app大全
  • 什么是响应式网站设计百度推广官方电话
  • 做兼职的网站是不是真的聚名网域名
  • 凤城网站建设关键词查询网站的工具
  • 古典网站建设公司怎样在百度上做广告
  • 用网站免费模板做网站要会什么杭州seo公司哪家好
  • 营销型网站建设试题黄页88网站推广效果
  • 社区团购小程序怎么做win7优化大师好不好
  • 网站首页做后台链接软文推广多少钱
  • 做seo是要先有网站吗网络推广方案的基本思路
  • 做网站收入太低百度竞价优化