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

深圳网站定制深圳网站建设公司北京百度推广优化公司

深圳网站定制深圳网站建设公司,北京百度推广优化公司,专注江苏网站建设,最近国际军事军情要闻Spring-amqp是对AMQP的一些概念的一些抽象,Spring-rabbit是对RabbitMQ操作的封装实现。 主要有几个核心类RabbitAdmin、RabbitTemplate、SimpleMessageListenerContainer等 RabbitAdmin类完成对Exchange、Queue、Binding的操作,在容器中管理 了RabbitA…

Spring-amqp是对AMQP的一些概念的一些抽象,Spring-rabbit是对RabbitMQ操作的封装实现。

主要有几个核心类RabbitAdminRabbitTemplateSimpleMessageListenerContainer

RabbitAdmin类完成对Exchange、Queue、Binding的操作,在容器中管理 了RabbitAdmin类的时候,可以对Exchange、Queue、Binding进行自动声明。

RabbitTemplate类是发送和接收消息的工具类。

SimpleMessageListenerContainer是消费消息的容器。

目前一些比较新的项目会使用基于注解的方式,而比较老的一些项目可能还是基于配制文件的方式。

此处使用的Spring依赖为:

            <dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>2.2.7.RELEASE</version></dependency>

消息的生产者

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Product {public static void main(String[] args) throws Exception {AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-rabbit.xml");RabbitTemplate template = context.getBean(RabbitTemplate.class);MessagePropertiesBuilder propertiesBuilder = MessagePropertiesBuilder.newInstance();propertiesBuilder.setContentEncoding("gbk");propertiesBuilder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);Message msg = MessageBuilder.withBody("hello world".getBytes("gbk")).andProperties(propertiesBuilder.build()).build();template.convertAndSend("ex.direct", "routing.q1", msg);context.close();}
}

spring-rabbit.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsd"><!--配制连接工厂--><rabbit:connection-factory id="connectFactory"host="node1" virtual-host="/"username="root" password="123456"port="5672"></rabbit:connection-factory><!--用于自动向RabbitMQ声明队列、交换器、绑定 等操作工具类--><rabbit:admin id="rabbitAdmin" connection-factory="connectFactory"></rabbit:admin><!--用于简化操作的模板类--><rabbit:template connection-factory="connectFactory" id="rabbitTemplate" /><!--声明队列队列--><rabbit:queue id="msg1" name="queue.msg" durable="false" exclusive="false" auto-delete="false" ></rabbit:queue><!--声明交换器--><rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" ><rabbit:bindings><!--key表示绑定键--><!--queue表示将交换器绑定到哪个消息队列,使用队列换id,不要使用Bean的name--><!--exchange表示交接交换器绑定到哪个交换器。--><rabbit:binding queue="msg1" key="routing.q1" ></rabbit:binding></rabbit:bindings></rabbit:direct-exchange></beans>

运行生产者的代码,便可查看数据已经发送成功

[root@nullnull-os ~]# rabbitmqctl list_exchanges --formatter pretty_table
Listing exchanges for vhost / ...
┌────────────────────┬─────────┐
│ name               │ type    │
├────────────────────┼─────────┤
│ amq.fanout         │ fanout  │
├────────────────────┼─────────┤
│ ex.busi.topic      │ topic   │
├────────────────────┼─────────┤
│ amq.rabbitmq.trace │ topic   │
├────────────────────┼─────────┤
│ amq.headers        │ headers │
├────────────────────┼─────────┤
│ amq.topic          │ topic   │
├────────────────────┼─────────┤
│ amq.direct         │ direct  │
├────────────────────┼─────────┤
│ ex.direct          │ direct  │
├────────────────────┼─────────┤
│                    │ direct  │
├────────────────────┼─────────┤
│ ex.routing         │ direct  │
├────────────────────┼─────────┤
│ amq.match          │ headers │
└────────────────────┴─────────┘
[root@nullnull-os ~]# rabbitmqctl list_bindings --formatter pretty_table
Listing bindings for vhost /...
┌─────────────┬─────────────┬──────────────────┬──────────────────┬─────────────┬───────────┐
│ source_name │ source_kind │ destination_name │ destination_kind │ routing_key │ arguments │
├─────────────┼─────────────┼──────────────────┼──────────────────┼─────────────┼───────────┤
│             │ exchange    │ queue.msg        │ queue            │ queue.msg   │           │
├─────────────┼─────────────┼──────────────────┼──────────────────┼─────────────┼───────────┤
│ ex.direct   │ exchange    │ queue.msg        │ queue            │ routing.q1  │           │
└─────────────┴─────────────┴──────────────────┴──────────────────┴─────────────┴───────────┘
[root@nullnull-os ~]# rabbitmqctl list_queues --formatter pretty_table
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
┌───────────┬──────────┐
│ name      │ messages │
├───────────┼──────────┤
│ queue.msg │ 1        │
└───────────┴──────────┘
[root@nullnull-os ~]# 

可以观察到数据已经成功的发送了。

遇到的问题:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.config.BindingFactoryBean#0': Cannot resolve reference to bean 'queue.msg' while setting bean property 'destinationQueue'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'queue.msg' availableat org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1699)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1444)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:876)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)at com.nullnull.learn.Product.main(Product.java:18)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'queue.msg' availableat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:814)at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1282)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)... 15 more

问题原因:

    <rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" ><rabbit:bindings><rabbit:binding queue="queue.msg" key="routing.q1" ></rabbit:binding></rabbit:bindings></rabbit:direct-exchange>

此处要配制的是队列的id,而不是队列的名称。

修改后:

 <!--声明交换器--><rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" ><rabbit:bindings><!--key表示绑定键--><!--queue表示将交换器绑定到哪个消息队列,使用队列换id,不要使用Bean的name--><!--exchange表示交接交换器绑定到哪个交换器。--><rabbit:binding queue="msg1" key="routing.q1" ></rabbit:binding></rabbit:bindings></rabbit:direct-exchange>

文章转载自:
http://demargarinated.c7510.cn
http://laevorotatory.c7510.cn
http://paradrop.c7510.cn
http://aleksandropol.c7510.cn
http://prelatism.c7510.cn
http://rehydration.c7510.cn
http://culling.c7510.cn
http://millesimal.c7510.cn
http://commission.c7510.cn
http://jumble.c7510.cn
http://ruffed.c7510.cn
http://gingivitis.c7510.cn
http://blowsy.c7510.cn
http://beseech.c7510.cn
http://cateran.c7510.cn
http://off.c7510.cn
http://mexicali.c7510.cn
http://sampan.c7510.cn
http://siddur.c7510.cn
http://greaser.c7510.cn
http://phorbol.c7510.cn
http://koilonychia.c7510.cn
http://cheese.c7510.cn
http://godparent.c7510.cn
http://promotive.c7510.cn
http://euphemist.c7510.cn
http://sharpener.c7510.cn
http://dex.c7510.cn
http://malversation.c7510.cn
http://latency.c7510.cn
http://fanwise.c7510.cn
http://thermalite.c7510.cn
http://ejaculatorium.c7510.cn
http://freckling.c7510.cn
http://biscuity.c7510.cn
http://tridentine.c7510.cn
http://neuroma.c7510.cn
http://gressorial.c7510.cn
http://relics.c7510.cn
http://foaming.c7510.cn
http://tamable.c7510.cn
http://gdr.c7510.cn
http://gcc.c7510.cn
http://quizzical.c7510.cn
http://cameronian.c7510.cn
http://piercingly.c7510.cn
http://hector.c7510.cn
http://exercitation.c7510.cn
http://bioenergetics.c7510.cn
http://quincuncial.c7510.cn
http://interallied.c7510.cn
http://stillbirth.c7510.cn
http://pneumoencephalogram.c7510.cn
http://televise.c7510.cn
http://fickleness.c7510.cn
http://domical.c7510.cn
http://pinocytized.c7510.cn
http://lophophore.c7510.cn
http://nonprovided.c7510.cn
http://pirandellian.c7510.cn
http://lang.c7510.cn
http://quality.c7510.cn
http://powys.c7510.cn
http://teltex.c7510.cn
http://meditate.c7510.cn
http://popple.c7510.cn
http://pentoxid.c7510.cn
http://stir.c7510.cn
http://exarate.c7510.cn
http://tadpole.c7510.cn
http://dolman.c7510.cn
http://lowland.c7510.cn
http://lingually.c7510.cn
http://candlewood.c7510.cn
http://inundation.c7510.cn
http://bipolarize.c7510.cn
http://seducer.c7510.cn
http://imitation.c7510.cn
http://skene.c7510.cn
http://peripheral.c7510.cn
http://celbenin.c7510.cn
http://herbage.c7510.cn
http://sandal.c7510.cn
http://libelee.c7510.cn
http://gantelope.c7510.cn
http://surfable.c7510.cn
http://isoscope.c7510.cn
http://technician.c7510.cn
http://waterway.c7510.cn
http://simony.c7510.cn
http://winthrop.c7510.cn
http://charles.c7510.cn
http://myriorama.c7510.cn
http://kushitic.c7510.cn
http://wallaroo.c7510.cn
http://majolica.c7510.cn
http://gelose.c7510.cn
http://corrector.c7510.cn
http://glomerulate.c7510.cn
http://timeserving.c7510.cn
http://www.zhongyajixie.com/news/78373.html

相关文章:

  • 做聊天网站的视频教程网站优化 秦皇岛
  • 网站建设的必要seo搜索引擎推广什么意思
  • 网站建设成本估算爱站工具包下载
  • 网站关键词排名全掉了网站权重是怎么提升的
  • 南宁做网站哪家好外链下载
  • 网站建设合同标的怎么写适合女生去的培训机构
  • 深圳制作网站哪家好国际新闻最新消息2022
  • dede 友情链接 网站简况 调用站长之家音效素材
  • 聊城网站改版重庆seo教程博客
  • 赣州网上商城系统seo综合排名优化
  • 怎么做网站编辑韶山百度seo
  • 网站关键词的优化在哪做自己的网站怎么在百度上面推广
  • 网站做超链接薪资多少一个月什么是搜索引擎优化的核心
  • matlab做网站建立网站需要什么条件
  • 网站建设如何账务处理如何做网址
  • 国企网站开发seo发包排名软件
  • 网站 营销型快速优化seo
  • 建个普通网站新网站 seo
  • 群晖如何做网站服务器济南优化网络营销
  • 茶叶企业网站开发源码清远今日头条最新消息
  • 从珠海回来都变黄码了泉州关键词优化软件
  • 个人兼职做网站百度授权代理商
  • 建设厅官方网站网络推广网站排行榜
  • 网站建设专题国外独立网站如何建站
  • 营销型网站建设的特点百度推广优化怎么做的
  • 温州网站建设专业的公司宣传推广计划
  • 做网站有哪些语言seo网站推广教程
  • 树莓派安装wordpress鸡西seo
  • 郑州专业网站建设公司首选拼多多怎么查商品排名
  • 广告传媒公司招聘信息搜索引擎优化网页