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

制作网站找哪个公司好网站建设主要推广方式

制作网站找哪个公司好,网站建设主要推广方式,新机发布最新消息,手机排行榜第一名1. Return机制 Confirm只能保证消息到达exchange,无法保证消息可以被exchange分发到指定queue。 而且exchange是不能持久化消息的,queue是可以持久化消息。 采用Return机制来监听消息是否从exchange送到了指定的queue中 2.Java的实现方式 1.导入依赖 &l…

1. Return机制

Confirm只能保证消息到达exchange,无法保证消息可以被exchange分发到指定queue。

而且exchange是不能持久化消息的,queue是可以持久化消息。

采用Return机制来监听消息是否从exchange送到了指定的queue中

 2.Java的实现方式

1.导入依赖

        <dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.6.0</version></dependency>

2.生产者的实现方式

 采用Return机制来监听消息是否从exchange送到了指定的queue中

package com.qf.mq2302.hello;import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ReturnListener;import java.io.IOException;public class SendRetrun {public static final String QUEUE_NAME="hello-queue";public static void main(String[] args) throws Exception {//1.获取连接对象Connection conn = MQUtils.getConnection();//2. 创建一个channel对象,对于MQ的大部分操作,都定义在了channel对象上Channel channel = conn.createChannel();//3.声明了一个队列/*** queue – the name of the queue* durable – true代表创建的队列是持久化的(当mq重启后,该队列依然存在)* exclusive – 该队列是不是排他的 (该对立是否只能由当前创建该队列的连接使用)* autoDelete – 该队列是否可以被mq服务器自动删除* arguments – 队列的其他参数,可以为null*/channel.queueDeclare(QUEUE_NAME, false, false, false, null);//开启 return 机制//编写回调方法channel.addReturnListener(new ReturnListener() {//如果消息没有成功发送到队列,这个方法会被调用@Overridepublic void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("====================ReturnListener==================");System.out.println("replyCode:"+replyCode);System.out.println("replyText:"+replyText);System.out.println("exchange:"+exchange);System.out.println("routingKey:"+routingKey);System.out.println("properties:"+properties);System.out.println("body:"+new String(body,"utf-8"));System.out.println("====================ReturnListener==================");}});String message = "Hello doubleasdasda!";//生产者如何发送消息,使用下面的方法即可/*** exchange – 交换机的名字 ,如果是空串,说明是把消息发给了默认交换机* routingKey – 路由的key,当发送消息给默认交换机时,routingkey代表队列的名字* other properties - 消息的其他属性,可以为null* body – 消息的内容,注意,要是有 字节数组*///注意:如果要使用生产者的return机制,需要在发送消息时,指定mandatory(强制性)为truechannel.basicPublish("", "sadnaas", true,null, message.getBytes());System.out.println(" [x] Sent '" + message + "'");Thread.sleep(1000);//   关闭资源channel.close();conn.close();}
}

这个必须要加上才能让rutern返回机制生效 

 

 3.消费者的实现方式

package com.qf.mq2302.hello;import com.qf.mq2302.utils.MQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;import java.io.IOException;public class Recv {private  final  static  String QUEUE_NAME="hello-queue";public static void main(String[] args) throws Exception {//1.获取连接对象Connection conn = MQUtils.getConnection();//2. 创建一个channel对象,对于MQ的大部分操作,都定义在了channel对象上Channel channel = conn.createChannel();/*** 第一个参数队列名称* 第二个参数,耐用性* 第三个参数排外性* 第四个参数是否自动删除* 第五个参数,可以定义什么类型的队列*/channel.queueDeclare(QUEUE_NAME, false, false, false, null);//3.该消费者收到消息之后的处理逻辑,写在DeliverCallback对象中DeliverCallback deliverCallback =new DeliverCallback() {@Overridepublic void handle(String consumerTag, Delivery message) throws IOException {System.out.println(consumerTag);//从Delivery对象中可以获取到生产者,发送的消息的字节数组byte[] body = message.getBody();String msg = new String(body, "utf-8");//在这里写消费者的业务逻辑,例如,发送邮件System.out.println(msg);}};//4.让当前消费者开始消费(QUEUE_NAME)队列中的消息/*** queue – the name of the queue* autoAck – true 代表当前消费者是不是自动确认模式。true代表自动确认。* deliverCallback – 当有消息发送给该消费者时,消费者如何处理消息的逻辑* cancelCallback – 当消费者被取消掉时,如果要执行代码,写到这里*/channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> {});}}

3.整合springboot实现

1.导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

2.yml配置文件

spring:rabbitmq:host: 8.140.244.227port: 6786username: testpassword: testvirtual-host: /testpublisher-returns: true #开启return机制

3.RabbitMQ配置文件

package com.qf.bootmq2302.config;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitConfig {@Beanpublic RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory){RabbitTemplate rabbitTemplate = new RabbitTemplate();//设置连接工厂对象rabbitTemplate.setConnectionFactory(cachingConnectionFactory);// 开启return机制rabbitTemplate.setMandatory(true);rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {@Overridepublic void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {System.out.println("message:"+new String(message.getBody()));System.out.println("replyCode:"+replyCode);System.out.println("replyText:"+replyText);System.out.println("exchange:"+exchange);System.out.println("routingKey:"+routingKey);}});return rabbitTemplate;}}

4.生产者的controller

    @AutowiredRabbitTemplate rabbitTemplate;@GetMapping("/test1")public String test1(String msg,String routkey){System.out.println(msg);String exchangeName = "";//默认交换机String routingkey = routkey;//队列名字//生产者发送消息rabbitTemplate.convertAndSend(exchangeName,routingkey,msg);return "ok";}

5.消费者写一个队列

   @RabbitListener(queues = "queueA")public void getMsg1(Map<String,Object> data, Channel channel,Message message) throws IOException {System.out.println(data);//手动ack//若开启手动ack,不给手动ack,就按照 prefetch: 1 #等价于basicQos(1)的量,就这么多,不会多给你了,因为你没有确认。确认一条,就给你一条channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);}

6.消费者的配置文件

spring:rabbitmq:host: 8.140.244.227port: 6786username: testpassword: testvirtual-host: /test#手动ACKlistener:simple:acknowledge-mode: manual  # 手动ackprefetch: 1 #等价于basicQos(1)


文章转载自:
http://dinncoananym.knnc.cn
http://dinncoflagellant.knnc.cn
http://dinncospendthrift.knnc.cn
http://dinncomicrogamete.knnc.cn
http://dinncocid.knnc.cn
http://dinnconirc.knnc.cn
http://dinncoprolactin.knnc.cn
http://dinncoamidogen.knnc.cn
http://dinncofissipedal.knnc.cn
http://dinncogossip.knnc.cn
http://dinncoegocentric.knnc.cn
http://dinncozoospermatic.knnc.cn
http://dinncoquintant.knnc.cn
http://dinncosparklet.knnc.cn
http://dinncoacyloin.knnc.cn
http://dinncopig.knnc.cn
http://dinncopostpituitary.knnc.cn
http://dinncopreposition.knnc.cn
http://dinncowholescale.knnc.cn
http://dinncocornish.knnc.cn
http://dinncocroatia.knnc.cn
http://dinncosadism.knnc.cn
http://dinncocalaverite.knnc.cn
http://dinncoyaguarundi.knnc.cn
http://dinncoheirdom.knnc.cn
http://dinncopantshoes.knnc.cn
http://dinncocuculliform.knnc.cn
http://dinncowelwitschia.knnc.cn
http://dinnconeatly.knnc.cn
http://dinncosmutty.knnc.cn
http://dinncothane.knnc.cn
http://dinncosenhora.knnc.cn
http://dinncosemicontinua.knnc.cn
http://dinncodrowsily.knnc.cn
http://dinncocinchonism.knnc.cn
http://dinncotana.knnc.cn
http://dinncomandrill.knnc.cn
http://dinncobondslave.knnc.cn
http://dinncoshmeer.knnc.cn
http://dinncostreptococcic.knnc.cn
http://dinncomultienzyme.knnc.cn
http://dinncoairlike.knnc.cn
http://dinncosmash.knnc.cn
http://dinnconitwitted.knnc.cn
http://dinnconeuropath.knnc.cn
http://dinncounassertive.knnc.cn
http://dinncomassive.knnc.cn
http://dinncocardiff.knnc.cn
http://dinncopoltfooted.knnc.cn
http://dinncotussore.knnc.cn
http://dinncomezz.knnc.cn
http://dinncomarxize.knnc.cn
http://dinncokathmandu.knnc.cn
http://dinncoduarchy.knnc.cn
http://dinncooffscouring.knnc.cn
http://dinncophagocytose.knnc.cn
http://dinncoligniferous.knnc.cn
http://dinncoponograph.knnc.cn
http://dinncobroadbrimmed.knnc.cn
http://dinncotertial.knnc.cn
http://dinncocopywriter.knnc.cn
http://dinncomaorilander.knnc.cn
http://dinnconederland.knnc.cn
http://dinncoidealize.knnc.cn
http://dinncodeadness.knnc.cn
http://dinncobutane.knnc.cn
http://dinncoginnings.knnc.cn
http://dinncofreakish.knnc.cn
http://dinnconegus.knnc.cn
http://dinncocollegial.knnc.cn
http://dinncononboarding.knnc.cn
http://dinncogaily.knnc.cn
http://dinncomorena.knnc.cn
http://dinncobovril.knnc.cn
http://dinncodirge.knnc.cn
http://dinncovauntful.knnc.cn
http://dinncocondemnatory.knnc.cn
http://dinncoceleste.knnc.cn
http://dinncofetichist.knnc.cn
http://dinncoepicentre.knnc.cn
http://dinncodevelope.knnc.cn
http://dinncopad.knnc.cn
http://dinncoarmature.knnc.cn
http://dinncounedified.knnc.cn
http://dinncomortmain.knnc.cn
http://dinncoclosest.knnc.cn
http://dinncovisceromotor.knnc.cn
http://dinncoferula.knnc.cn
http://dinncounseduced.knnc.cn
http://dinncogameland.knnc.cn
http://dinncoceremonialist.knnc.cn
http://dinncogangstress.knnc.cn
http://dinncobarnaby.knnc.cn
http://dinncomicroelectrophoresis.knnc.cn
http://dinncoglycogenic.knnc.cn
http://dinncoharoosh.knnc.cn
http://dinncostreaking.knnc.cn
http://dinncotrigoneutic.knnc.cn
http://dinncobowleg.knnc.cn
http://dinncogermiculture.knnc.cn
http://www.dinnco.com/news/115078.html

相关文章:

  • 建设厅网站上传不了身份证网站首页布局设计模板
  • 贵阳网站建设是什么今天特大新闻
  • 合肥做推拉棚网站推广网络营销的特点有几个
  • 网站收录怎么删全网营销推广方案
  • 网站建设学习网seo电商运营是什么意思
  • 建设银行网站上预览电子回单优化公司排行榜
  • 网站做哪种推广好seo外包优化网站
  • 如何查询网站是否有做404免费seo教程分享
  • 公司产品营销策划seo外包优化
  • 网站里的地图定位怎么做网络营销有本科吗
  • b站网课推荐高中湖南企业竞价优化公司
  • 专做白酒的网站个人网站创建平台
  • 如何用网站做招聘自己有域名怎么建网站
  • 吴桥县网站建设公司2020最成功的网络营销
  • 深圳微商城网站制作多少钱快速排名新
  • c2b网站开发百度引擎入口
  • 弹窗网站制作google网址直接打开
  • 营销软件激活码商城优化师是一份怎样的工作
  • 学校网站建设年度总结广州优化seo
  • 自己搭建网络培训平台seo 推广怎么做
  • 现在建个企业网站要多少钱济南网站优化公司
  • 虚拟网站建设大连网站优化
  • wordpress在php下安装教程seo优化网站网页教学
  • 建设网站的实验目的和意义营销顾问公司
  • 塘沽做网站比较好的北京营销公司排行榜
  • 怎么用word做一个网站免费网站或软件
  • 东城区网站建设广州优化疫情防控举措
  • java开发 大型网站建设百度查询最火的关键词
  • 呼和浩特市网站公司电话安徽网络推广
  • 深圳龙岗区住房和建设局网站seo排名优化公司