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

一个工厂做网站有用吗校园网页设计模板

一个工厂做网站有用吗,校园网页设计模板,北京网站设计公司hlh成都柚米科技15,重庆建网站目录 ★ 发送消息★ 创建队列的两种方式代码演示需求1#xff1a;发送消息1、ContentUtil 先定义常量2、RabbitMQConfig 创建队列的两种方式之一#xff1a;配置式#xff1a;问题#xff1a; 3、MessageService 编写逻辑PublishController 控制器application.properties 配… 目录 ★ 发送消息★ 创建队列的两种方式代码演示需求1发送消息1、ContentUtil 先定义常量2、RabbitMQConfig 创建队列的两种方式之一配置式问题 3、MessageService 编写逻辑PublishController 控制器application.properties 配置属性测试消息发送 ★ 接收消息代码演示测试: 消息接收 ★ 定制监听器容器工厂完整代码application.properties RabbitMQ的连接等属性配置ContentUtil 常量工具类RabbitMQConfig 配置式创建消息队列MessageService 发送消息的业务代码PublishController.java 发送消息的控制层MyRabbitMQListener 监听器监听消息队列pom.xml ★ 发送消息 - Spring Boot可以将AmqpAdmin和AmqpTemplate注入任何其他组件接下来该组件即可通过AmqpAdmin来管理Exchange、队列和绑定还可通过AmqpTemplate来发送消息。 - Spring Boot还会自动配置一个RabbitMessagingTemplate BeanRabbitAutoConfiguration负责配置如果想使用它来发送、接收消息可使用RabbitMessagingTemplate代替上面的AmqpTemplate两个Template的注入方式完全相同。★ 创建队列的两种方式 方式一编程式在程序中通过AmqpAdmin创建队列。方式二配置式在容器中配置 org.springframework.amqp.core.Queue 类型的BeanRabbitMQ将会自动为该Bean创建对应的队列。代码演示 需求1发送消息 1、ContentUtil 先定义常量 2、RabbitMQConfig 创建队列的两种方式之一 配置式 在容器中配置 org.springframework.amqp.core.Queue 类型的BeanRabbitMQ将会自动为该Bean创建对应的队列。 就是在配置类中创建一个生成消息队列的Bean。 问题 用 Configuration 注解声明为配置类但是项目启动的时候没有自动生成这个队列。 据了解是因为RabbitMQ使用了懒加载大概是没有消费者监听这个队列就没有创建。 但是当我写后面的代码后这个消息队列就生成了但是并没有消费者去监听这个队列。 这有点想不通。 不知道后面是哪里的代码让这个配置类能成功声明这个消息队列出来。 水落石出 经过测试 在下面的MessageService 这个类中依赖注入了 AmqpAdmin 和 AmqpTemplate 这两个对象当我们通过这两个对象去声明队列、Exchange 和绑定的时候配置类中的创建消息队列的bean就能成功创建队列。 这张图结合下面的 MessageService 中的代码就可得知 这是依赖注入 AmqpAdmin 和 AmqpTemplate 这两个对象的有参构造器中声明的。 3、MessageService 编写逻辑 声明Exchange 、 消息队列 、 Exchange和消息队列的绑定、发送消息的方法等 PublishController 控制器 application.properties 配置属性 测试消息发送 成功生成队列 发送消息测试 ★ 接收消息 RabbitListener 注解修饰的方法将被注册为消息监听器方法。 【备注】该注解可通过queues属性指定它要监听的、已有的消息队列它也可使用queuesToDeclare来声明队列并监听该队列。- 如果没有显式配置监听器容器工厂RabbitListenerContainerFactorySpring Boot会在容器中自动配置一个SimpleRabbitListenerContainerFactory Bean作为监听器容器工厂如果希望使用DirectRabbitListenerContainerFactory可在application.properties文件中添加如下配置spring.rabbitmq.listener.typedirect▲ 如果在容器中配置了MessageRecoverer或MessageConverter它们会被自动关联到默认的监听器容器工厂。代码演示 创建个消息队列的监听器就可以了。 RabbitListener 注解修饰的方法将被注册为消息监听器方法。 该注解可通过queues属性指定它要监听的、已有的消息队列 它也可使用queuesToDeclare来声明队列并监听该队列 还可以用 bindings 进行 Exchange和queue的绑定操作。 测试: 消息接收 发送消息和监听消息 ★ 定制监听器容器工厂 ▲ 如果要定义更多的监听器容器工厂或覆盖默认的监听器容器工厂可通过Spring Boot提供的SimpleRabbitListenerContainerFactoryConfigurer 或DirectRabbitListenerContainerFactoryConfigurer来实现它们可对SimpleRabbitListenerContainerFactory 或DirectRabbitListenerContainerFactory进行与自动配置相同的设置。 ▲ 有了自定义的监听器容器工厂之后可通过RabbitListener注解的containerFactory属性来指定使用自定义的监听器容器工厂 例如如下注解代码RabbitListener(queues myQueue1, containerFactorymyFactory)完整代码 application.properties RabbitMQ的连接等属性配置 # 配置连接 RabbitMQ 的基本信息------------------------------------------------------ spring.rabbitmq.hostlocalhost spring.rabbitmq.port5672 # 下面属性可配置多个以逗号隔开的连接地址一旦配置了该属性host 和 port 属性就会被忽略 # spring.rabbitmq.addresses spring.rabbitmq.usernameljh spring.rabbitmq.password123456 # 连接虚拟主机 spring.rabbitmq.virtual-hostmy-vhost01# 配置RabbitMQ的缓存相关信息-------------------------------------------------------- # 指定缓存 connection 还是缓存 channel spring.rabbitmq.cache.connection.modechannel # 指定可以缓存多少个 Channel spring.rabbitmq.cache.channel.size50 # 如果选择的缓存模式是 connection 那么就可以配置如下属性 # spring.rabbitmq.cache.connection.size15# 配置 和 RabbitTemplate 相关的属性-------------------------------------------------- # 指定 RabbitTemplate 发送消息失败时会重新尝试 spring.rabbitmq.template.retry.enabledtrue # RabbitTemplate 发送消息失败后每隔1秒重新尝试发送消息 spring.rabbitmq.template.retry.initial-interval1s # RabbitTemplate 发送消息失败时最多尝试重新发送消息的次数 spring.rabbitmq.template.retry.max-attempts5 # 设置每次尝试重新发送消息的时间间隔是一个等比数列1s, 2s, 4s, 8s, 16s # 第一次等1s后尝试第二次等2s后尝试第三次等4s后尝试重新发送消息...... spring.rabbitmq.template.retry.multiplier2 # 指定发送消息时默认的Exchange名 spring.rabbitmq.template.exchange # 指定发送消息时默认的路由key spring.rabbitmq.template.routing-keytest# 配置和消息监听器的容器工厂相关的属性-------------------------------------------------- # 指定监听器容器工厂的类型 spring.rabbitmq.listener.typesimple # 指定消息的确认模式 spring.rabbitmq.listener.simple.acknowledge-modeauto # 指定获取消息失败时是否重试 spring.rabbitmq.listener.simple.retry.enabledtrue # 发送消息失败时最多尝试重新发送消息的次数 spring.rabbitmq.listener.simple.retry.max-attempts2 # 发送消息失败后每隔1秒重新尝试发送消息 spring.rabbitmq.listener.simple.retry.initial-interval1s ContentUtil 常量工具类 package cn.ljh.app.rabbitmq.util;//常量 public class ContentUtil {//定义Exchange的常量-----fanout扇形就是广播类型public static final String EXCHANGE_NAME boot.fanout;//消息队列数组public static final String[] QUEUE_NAMES new String[] {queue_boot_01,queue_boot_02,queue_boot_03};} RabbitMQConfig 配置式创建消息队列 package cn.ljh.app.rabbitmq.config;import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;//配置式在容器中配置 org.springframework.amqp.core.Queue 类型的BeanRabbitMQ将会自动为该Bean创建对应的队列 //声明这个类为配置类 Configuration public class RabbitMQConfig {//用配置式的方式在RabbitMQ中定义队列Beanpublic Queue myQueue(){//在容器中配置一个 Queue BeanSpring 就会为它在 RabbitMQ 中自动创建对应的 Queuereturn new Queue(queue_boot, /* Queue 消息队列名 */true, /* 是否是持久的消息队列 */false, /* 是否是独占的消息队列独占就是是否只允许该消息消费者消费该队列的消息 */false, /* 是否在没有消息的时候自动删除消息队列 */null /* 额外的一些消息队列的参数 */);} } MessageService 发送消息的业务代码 声明Exchange 、Queue Exchange 绑定Queue发送消息代码 package cn.ljh.app.rabbitmq.service;import cn.ljh.app.rabbitmq.util.ContentUtil; import org.springframework.amqp.core.*; import org.springframework.stereotype.Service;//业务逻辑声明Exchange 和 Queue 消息队列发送消息的方法 Service public class MessageService {//AmqpAdmin来管理Exchange、队列和绑定private final AmqpAdmin amqpAdmin;//AmqpTemplate来发送消息private final AmqpTemplate amqpTemplate;//通过有参构造器进行依赖注入public MessageService(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate){this.amqpAdmin amqpAdmin;this.amqpTemplate amqpTemplate;//由于声明 Exchange 、 队列 、 绑定Exchange绑定队列都只需要做一次即可因此放在此处构造器中完成即可//创建 fanout 类型的 Exchange 使用FanoutExchange实现类FanoutExchange exchange new FanoutExchange(ContentUtil.EXCHANGE_NAME,true, /* Exchange是否持久化 */false, /* 是否自动删除 */null /* 额外的参数属性 */);//声明 Exchangethis.amqpAdmin.declareExchange(exchange);//此处循环声明 Queue 也相当于代码式创建 Queuefor (String queueName : ContentUtil.QUEUE_NAMES){Queue queue new Queue(queueName, /* Queue 消息队列名 */true, /* 是否是持久的消息队列 */false, /* 是否是独占的消息队列独占就是是否只允许该消息消费者消费该队列的消息 */false, /* 是否在没有消息的时候自动删除消息队列 */null /* 额外的一些消息队列的参数 */);//此处声明 Queue 也相当于【代码式】创建 Queuethis.amqpAdmin.declareQueue(queue);//声明 Queue 的绑定Binding binding new Binding(queueName, /* 指定要分发消息目的地的名称--这里是要发送到这个消息队列里面去 */Binding.DestinationType.QUEUE, /* 分发消息目的的类型指定要绑定 queue 还是 Exchange */ContentUtil.EXCHANGE_NAME, /* 要绑定的Exchange */x, /* 因为绑定的Exchange类型是 fanout 扇形广播模式所以路由key随便写没啥作用 */null);//声明 Queue 的绑定amqpAdmin.declareBinding(binding);}}//发送消息的方法public void publish(String content){//发送消息amqpTemplate.convertAndSend(ContentUtil.EXCHANGE_NAME, /* 指定将消息发送到这个Exchange */, /* 因为Exchange是fanout 类型的广播类型所以写什么路由key都行都没意义 */content /* 发送的消息体 */);} }PublishController.java 发送消息的控制层 package cn.ljh.app.rabbitmq.controller;import cn.ljh.app.rabbitmq.service.MessageService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;//发送消息 RestController public class PublishController {private final MessageService messageService;//有参构造器进行依赖注入public PublishController(MessageService messageService){this.messageService messageService;}GetMapping(/publish/{message})//因为{message}是一个路径参数所以方法接收的时候需要加上注解 PathVariablepublic String publish(PathVariable String message){//发布消息messageService.publish(message);return 消息发布成功;}} MyRabbitMQListener 监听器监听消息队列 package cn.ljh.app.rabbitmq.listener;import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component;//监听器监听消息队列并进行消费 Component public class MyRabbitMQListener {//queues 指定监听已有的哪个消费队列RabbitListener(queues queue_boot_01)public void onQ1Message(String message){System.err.println(从 queue_boot_01 消息队列接收到的消息 message);}//queues 指定监听已有的哪个消费队列RabbitListener(queues queue_boot_02)public void onQ2Message(String message){System.err.println(从 queue_boot_02 消息队列接收到的消息 message);}//queues 指定监听已有的哪个消费队列//还可以用 queuesToDeclare 直接声明并监听该队列还可以用 bindings 进行Exchange和queue的绑定RabbitListener(queuesToDeclare Queue(name queue_boot_03,durable true,exclusive false,autoDelete false),admin amqpAdmin /*指定声明Queue绑定Queue所用的 amqpAdmin不指定的话就用容器中默认的那一个 */)public void onQ3Message(String message){System.err.println(从 queue_boot_03 消息队列接收到的消息 message);} }pom.xml ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.4.5/version/parentgroupIdcn.ljh/groupIdartifactIdrabbitmq_boot/artifactIdversion1.0.0/versionnamerabbitmq_boot/namepropertiesjava.version11/java.versionproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- RabbitMQ 的依赖 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependency!-- web 依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- 开发者工具的依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependency!-- lombok 依赖--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.amqp/groupIdartifactIdspring-rabbit-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project
文章转载自:
http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn
http://www.morning.elmtw.cn.gov.cn.elmtw.cn
http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn
http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn
http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn
http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn
http://www.morning.trrd.cn.gov.cn.trrd.cn
http://www.morning.kphyl.cn.gov.cn.kphyl.cn
http://www.morning.trsdm.cn.gov.cn.trsdm.cn
http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn
http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn
http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn
http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn
http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn
http://www.morning.jyznn.cn.gov.cn.jyznn.cn
http://www.morning.mlyq.cn.gov.cn.mlyq.cn
http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn
http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn
http://www.morning.tbstj.cn.gov.cn.tbstj.cn
http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn
http://www.morning.dblfl.cn.gov.cn.dblfl.cn
http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn
http://www.morning.xysdy.cn.gov.cn.xysdy.cn
http://www.morning.wnywk.cn.gov.cn.wnywk.cn
http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn
http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn
http://www.morning.grxyx.cn.gov.cn.grxyx.cn
http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn
http://www.morning.bcngs.cn.gov.cn.bcngs.cn
http://www.morning.przc.cn.gov.cn.przc.cn
http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn
http://www.morning.xsklp.cn.gov.cn.xsklp.cn
http://www.morning.blxlf.cn.gov.cn.blxlf.cn
http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn
http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn
http://www.morning.dphmj.cn.gov.cn.dphmj.cn
http://www.morning.rjhts.cn.gov.cn.rjhts.cn
http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn
http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn
http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn
http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com
http://www.morning.qykxj.cn.gov.cn.qykxj.cn
http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn
http://www.morning.mqghs.cn.gov.cn.mqghs.cn
http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn
http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn
http://www.morning.ccphj.cn.gov.cn.ccphj.cn
http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn
http://www.morning.rwbh.cn.gov.cn.rwbh.cn
http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn
http://www.morning.rsnn.cn.gov.cn.rsnn.cn
http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn
http://www.morning.rgyts.cn.gov.cn.rgyts.cn
http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn
http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn
http://www.morning.wxckm.cn.gov.cn.wxckm.cn
http://www.morning.qznkn.cn.gov.cn.qznkn.cn
http://www.morning.khzml.cn.gov.cn.khzml.cn
http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn
http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn
http://www.morning.qwgct.cn.gov.cn.qwgct.cn
http://www.morning.yltyz.cn.gov.cn.yltyz.cn
http://www.morning.bsghk.cn.gov.cn.bsghk.cn
http://www.morning.nknt.cn.gov.cn.nknt.cn
http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn
http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn
http://www.morning.kdbbm.cn.gov.cn.kdbbm.cn
http://www.morning.nqcts.cn.gov.cn.nqcts.cn
http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn
http://www.morning.lkthj.cn.gov.cn.lkthj.cn
http://www.morning.nlglm.cn.gov.cn.nlglm.cn
http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn
http://www.morning.xsctd.cn.gov.cn.xsctd.cn
http://www.morning.hxpff.cn.gov.cn.hxpff.cn
http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn
http://www.morning.rtpw.cn.gov.cn.rtpw.cn
http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn
http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn
http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn
http://www.morning.czxrg.cn.gov.cn.czxrg.cn
http://www.tj-hxxt.cn/news/256070.html

相关文章:

  • 营销式网站深圳房管局官网查询系统
  • 网站建设详细合同范本餐饮网站建设思路
  • 做网站大概多少酒泉哪家公司可以做网站
  • 搭建网站步骤温州建设局网站林南飞
  • 建设银行网站首页口重庆丰标建设网站
  • 贵阳做网站优化网站建设所需的硬软件
  • 足球竞猜网站开发怎么把自己的网站放到网上
  • 上虞中国建设银行官网站3d网络游戏前十名
  • 协助别人做网站犯法么易思企业网站管理
  • 做网站需要了解的内容网站二维码链接怎么做
  • 搭建网站的方案wordpress安装上传
  • 网站正在备案中杭州网站推广公司
  • 数据库在网站建设中的作用小程序游戏怎么赚钱
  • 北京商城网站建设地址短视频营销ppt
  • 阳江公司做网站sql server网站建设
  • 资讯网站的优势苏州网站开发网站建立费用
  • 威海哪里做网站做it的要给赌场网站做维护吗
  • 寮步网站制作网站设计与制作的流程
  • 如何自己建网站网站可以做网站广告
  • 电子科技公司网站网页设计昆明网站快速优化排名
  • 太原做网络推广的公司网站优化排名的方法
  • 网站开发美工绩效考核通过服务推广网站的案例
  • 手机网站建设必要性emlog wordpress
  • 做网站平台多少钱做个手机网站多少钱 广州
  • 网站建设与维护报告总结qq刷赞网站咋做
  • 微网站哪家好verycloud wordpress
  • 高性能网站建设进阶指南cms开发是什么意思
  • 自己做的网站打不开怎么回事中文网站建设中模板
  • 下载类网站开发条件php企业网站建设论文
  • 网站底部导航制作网站案例网站建设