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

网站备案有什么坏处灰色关键词排名代发

网站备案有什么坏处,灰色关键词排名代发,商城开发网站建设,网站赌博二维码收钱怎么做的RabbitMQ中的各模式及其用法 工作队列模式一、生产者代码1、封装工具类2、编写代码3、发送消息效果 二、消费者代码1、编写代码2、运行效果 发布订阅模式一、生产者代码二、消费者代码1、消费者1号2、消费者2号 三、运行效果四、小结 路由模式一、生产者代码二、消费者代码1、消…

RabbitMQ中的各模式及其用法

  • 工作队列模式
    • 一、生产者代码
      • 1、封装工具类
      • 2、编写代码
      • 3、发送消息效果
    • 二、消费者代码
      • 1、编写代码
      • 2、运行效果
  • 发布订阅模式
    • 一、生产者代码
    • 二、消费者代码
      • 1、消费者1号
      • 2、消费者2号
    • 三、运行效果
    • 四、小结
  • 路由模式
    • 一、生产者代码
    • 二、消费者代码
      • 1、消费者1号
      • 2、消费者2号
    • 三、运行结果
      • 1、绑定关系
      • 2、消费消息
  • 主题模式
    • 一、生产者代码
    • 二、消费者代码
    • 1、消费者1号
    • 2、消费者2号
    • 三、运行效果
  • 总结

工作队列模式

一、生产者代码

新建一个module,在module下创建属于自己的包,并且创建一个名为“work”的子包,以及工具类包“util”。结构如图所示:
在这里插入图片描述
在pom文件中添加图中所示依赖:

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

此时准备工作基本完成。

1、封装工具类

修改rabbitMQ地址,替换为自己的。

package com.xxx.rabbitmq.util;import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;/*** @ClassName: ConnectionUtil* @Package: com.xxx.rabbitmq.util* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class ConnectionUtil {public static final String HOST_ADDRESS = "192.168.xxx.xxx";public static Connection getConnection() throws Exception {// 定义连接工厂ConnectionFactory factory = new ConnectionFactory();// 设置服务地址factory.setHost(HOST_ADDRESS);// 端口factory.setPort(5672);//设置账号信息,用户名、密码、vhostfactory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("123456");// 通过工程获取连接Connection connection = factory.newConnection();return connection;}public static void main(String[] args) throws Exception {Connection con = ConnectionUtil.getConnection();// amqp://guest@192.168.xxx.xxx:5672/System.out.println(con);con.close();}}

2、编写代码

新建生产者类Producer:

package com.xxx.rabbitmq.work;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;/*** @ClassName: Producer* @Package: com.xxx.rabbitmq.work* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Producer {public static final String QUEUE_NAME = "work_queue";public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME,true,false,false,null);for (int i = 1; i <= 10; i++) {String body = i+"hello rabbitmq~~~";channel.basicPublish("",QUEUE_NAME,null,body.getBytes());}channel.close();connection.close();}
}

3、发送消息效果

在这里插入图片描述

二、消费者代码

1、编写代码

创建Consumer1和Consumer2。Consumer2只是类名和打印提示不同,代码完全一样。
Consumer1:

package com.xxx.rabbitmq.work;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer1* @Package: com.xxx.rabbitmq.work* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer1 {static final String QUEUE_NAME = "work_queue";public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("Consumer1 body:"+new String(body));}};channel.basicConsume(QUEUE_NAME,true,consumer);}
}

Consumer2:

package com.xxx.rabbitmq.work;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer2* @Package: com.xxx.rabbitmq.work* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer2 {static final String QUEUE_NAME = "work_queue";public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("Consumer2 body:"+new String(body));}};channel.basicConsume(QUEUE_NAME,true,consumer);}
}

** 注意:**
运行的时候先启动两个消费端程序,然后再启动生产者端程序。
如果已经运行过生产者程序,则手动把work_queue队列删掉。

2、运行效果

最终两个消费端程序竞争结果如下:
在这里插入图片描述
在这里插入图片描述
这样就完成了工作队列模式的演示。

发布订阅模式

一、生产者代码

还是在上面的module内,新建一个名为fanout的子包,在包内创建Producer类:

package com.xxx.rabbitmq.fanout;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;/*** @ClassName: Producer* @Package: com.xxx.rabbitmq.fanout* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Producer {public static void main(String[] args) throws Exception {// 1、获取连接Connection connection = ConnectionUtil.getConnection();// 2、创建频道Channel channel = connection.createChannel();// 参数1. exchange:交换机名称// 参数2. type:交换机类型//     DIRECT("direct"):定向//     FANOUT("fanout"):扇形(广播),发送消息到每一个与之绑定队列。//     TOPIC("topic"):通配符的方式//     HEADERS("headers"):参数匹配// 参数3. durable:是否持久化// 参数4. autoDelete:自动删除// 参数5. internal:内部使用。一般false// 参数6. arguments:其它参数String exchangeName = "test_fanout";// 3、创建交换机channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT,true,false,false,null);// 4、创建队列String queue1Name = "test_fanout_queue1";String queue2Name = "test_fanout_queue2";channel.queueDeclare(queue1Name,true,false,false,null);channel.queueDeclare(queue2Name,true,false,false,null);// 5、绑定队列和交换机// 参数1. queue:队列名称// 参数2. exchange:交换机名称// 参数3. routingKey:路由键,绑定规则//     如果交换机的类型为fanout,routingKey设置为""channel.queueBind(queue1Name,exchangeName,"");channel.queueBind(queue2Name,exchangeName,"");String body = "日志信息:张三调用了findAll方法...日志级别:info...";// 6、发送消息channel.basicPublish(exchangeName,"",null,body.getBytes());// 7、释放资源channel.close();connection.close();}
}

二、消费者代码

1、消费者1号

package com.xxx.rabbitmq.fanout;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer1* @Package: com.xxx.rabbitmq.fanout* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer1 {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String queue1Name = "test_fanout_queue1";channel.queueDeclare(queue1Name,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("body:"+new String(body));System.out.println("队列 1 消费者 1 将日志信息打印到控制台.....");}};channel.basicConsume(queue1Name,true,consumer);}
}

2、消费者2号

package com.xxx.rabbitmq.fanout;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer2* @Package: com.xxx.rabbitmq.fanout* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer2 {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String queue2Name = "test_fanout_queue2";channel.queueDeclare(queue2Name,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("body:"+new String(body));System.out.println("队列 2 消费者 2 将日志信息打印到控制台.....");}};channel.basicConsume(queue2Name,true,consumer);}
}

三、运行效果

先启动消费者,然后再运行生产者程序发送消息:
在这里插入图片描述
在这里插入图片描述

四、小结

交换机和队列的绑定关系如下图所示:
在这里插入图片描述
交换机需要与队列进行绑定,绑定之后;一个消息可以被多个消费者都收到。
发布订阅模式与工作队列模式的区别:

  • 工作队列模式本质上是绑定默认交换机
  • 发布订阅模式绑定指定交换机
  • 监听同一个队列的消费端程序彼此之间是竞争关系
  • 绑定同一个交换机的多个队列在发布订阅模式下,消息是广播的,每个队列都能接收到消息

路由模式

一、生产者代码

新建子包routing,并新建Producer类:

package com.xxx.rabbitmq.routing;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;/*** @ClassName: Producer* @Package: com.xxx.rabbitmq.routing* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Producer {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String exchangeName = "test_direct";// 创建交换机channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT,true,false,false,null);// 创建队列String queue1Name = "test_direct_queue1";String queue2Name = "test_direct_queue2";// 声明(创建)队列channel.queueDeclare(queue1Name,true,false,false,null);channel.queueDeclare(queue2Name,true,false,false,null);// 队列绑定交换机// 队列1绑定errorchannel.queueBind(queue1Name,exchangeName,"error");// 队列2绑定info error warningchannel.queueBind(queue2Name,exchangeName,"info");channel.queueBind(queue2Name,exchangeName,"error");channel.queueBind(queue2Name,exchangeName,"warning");String message = "日志信息:张三调用了delete方法.错误了,日志级别error";// 发送消息channel.basicPublish(exchangeName,"error",null,message.getBytes());System.out.println(message);// 释放资源channel.close();connection.close();}
}

二、消费者代码

1、消费者1号

package com.xxx.rabbitmq.routing;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer1* @Package: com.xxx.rabbitmq.routing* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer1 {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String queue1Name = "test_direct_queue1";channel.queueDeclare(queue1Name,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("body:"+new String(body));System.out.println("Consumer1 将日志信息打印到控制台.....");}};channel.basicConsume(queue1Name,true,consumer);}
}

2、消费者2号

package com.xxx.rabbitmq.routing;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer2* @Package: com.xxx.rabbitmq.routing* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer2 {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String queue2Name = "test_direct_queue2";channel.queueDeclare(queue2Name,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("body:"+new String(body));System.out.println("Consumer2 将日志信息存储到数据库.....");}};channel.basicConsume(queue2Name,true,consumer);}}

三、运行结果

1、绑定关系

在这里插入图片描述

2、消费消息

在这里插入图片描述

主题模式

一、生产者代码

新建子包topic,新建生产者类Producer:

package com.xxx.rabbitmq.topic;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;/*** @ClassName: Producer* @Package: com.xxx.rabbitmq.topic* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Producer {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String exchangeName = "test_topic";channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true,false,false,null);String queue1Name = "test_topic_queue1";String queue2Name = "test_topic_queue2";channel.queueDeclare(queue1Name,true,false,false,null);channel.queueDeclare(queue2Name,true,false,false,null);// 绑定队列和交换机// 参数1. queue:队列名称// 参数2. exchange:交换机名称// 参数3. routingKey:路由键,绑定规则//      如果交换机的类型为fanout ,routingKey设置为""// routing key 常用格式:系统的名称.日志的级别。// 需求: 所有error级别的日志存入数据库,所有order系统的日志存入数据库channel.queueBind(queue1Name,exchangeName,"#.error");channel.queueBind(queue1Name,exchangeName,"order.*");channel.queueBind(queue2Name,exchangeName,"*.*");// 分别发送消息到队列:order.info、goods.info、goods.errorString body = "[所在系统:order][日志级别:info][日志内容:订单生成,保存成功]";channel.basicPublish(exchangeName,"order.info",null,body.getBytes());body = "[所在系统:goods][日志级别:info][日志内容:商品发布成功]";channel.basicPublish(exchangeName,"goods.info",null,body.getBytes());body = "[所在系统:goods][日志级别:error][日志内容:商品发布失败]";channel.basicPublish(exchangeName,"goods.error",null,body.getBytes());channel.close();connection.close();}
}

二、消费者代码

1、消费者1号

消费者1监听队列1:

package com.xxx.rabbitmq.topic;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer1* @Package: com.xxx.rabbitmq.topic* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer1 {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String QUEUE_NAME = "test_topic_queue1";channel.queueDeclare(QUEUE_NAME,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("body:"+new String(body));}};channel.basicConsume(QUEUE_NAME,true,consumer);}
}

2、消费者2号

消费者2监听队列2:

package com.xxx.rabbitmq.topic;import com.xxx.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;import java.io.IOException;/*** @ClassName: Consumer2* @Package: com.xxx.rabbitmq.topic* @Author: * @CreateDate: * @Version: V1.0.0* @Description:*/public class Consumer2 {public static void main(String[] args) throws Exception {Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();String QUEUE_NAME = "test_topic_queue2";channel.queueDeclare(QUEUE_NAME,true,false,false,null);Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("body:"+new String(body));}};channel.basicConsume(QUEUE_NAME,true,consumer);}
}

三、运行效果

队列1:
在这里插入图片描述
队列2:
在这里插入图片描述
至此,就完成了RabbitMQ各模式的使用演示。

总结

在选择使用什么模式时,需要对应业务需求,结合需求选择合适的模式。


文章转载自:
http://dinncohoatching.bkqw.cn
http://dinncosiffleur.bkqw.cn
http://dinnconephelitic.bkqw.cn
http://dinncobarbotine.bkqw.cn
http://dinncoturboshaft.bkqw.cn
http://dinncothoria.bkqw.cn
http://dinncohostess.bkqw.cn
http://dinncokilowatt.bkqw.cn
http://dinncofix.bkqw.cn
http://dinncophocomelia.bkqw.cn
http://dinncolector.bkqw.cn
http://dinncounjealous.bkqw.cn
http://dinncoblendword.bkqw.cn
http://dinncohereabout.bkqw.cn
http://dinncomoxibustion.bkqw.cn
http://dinncosarcoidosis.bkqw.cn
http://dinncopillar.bkqw.cn
http://dinncoopiology.bkqw.cn
http://dinncodispossession.bkqw.cn
http://dinncogovern.bkqw.cn
http://dinncofogging.bkqw.cn
http://dinncopyrgeometer.bkqw.cn
http://dinncocastries.bkqw.cn
http://dinncoinkholder.bkqw.cn
http://dinncounshackle.bkqw.cn
http://dinncoincretionary.bkqw.cn
http://dinncoperistome.bkqw.cn
http://dinncoforedate.bkqw.cn
http://dinncocookies.bkqw.cn
http://dinnconones.bkqw.cn
http://dinncobdsa.bkqw.cn
http://dinncochainlet.bkqw.cn
http://dinncogasometer.bkqw.cn
http://dinncojigger.bkqw.cn
http://dinncothermopile.bkqw.cn
http://dinncotextolite.bkqw.cn
http://dinncoantimicrobial.bkqw.cn
http://dinncoslotware.bkqw.cn
http://dinncorepublic.bkqw.cn
http://dinncoconflux.bkqw.cn
http://dinncoluxe.bkqw.cn
http://dinncoeconomize.bkqw.cn
http://dinncocryptesthesia.bkqw.cn
http://dinncobutterwort.bkqw.cn
http://dinncoirishwoman.bkqw.cn
http://dinncoami.bkqw.cn
http://dinncoentemple.bkqw.cn
http://dinncobloodcurdling.bkqw.cn
http://dinncomillionnaire.bkqw.cn
http://dinncoroentgenometry.bkqw.cn
http://dinncospermatogenic.bkqw.cn
http://dinncosubmaxilla.bkqw.cn
http://dinncomalacostracan.bkqw.cn
http://dinncoexpressible.bkqw.cn
http://dinncomorganite.bkqw.cn
http://dinncocryptoclimate.bkqw.cn
http://dinncoeuropeanly.bkqw.cn
http://dinncobelowdecks.bkqw.cn
http://dinnconautic.bkqw.cn
http://dinncoastatic.bkqw.cn
http://dinncoyouthwort.bkqw.cn
http://dinncochapiter.bkqw.cn
http://dinncoderinger.bkqw.cn
http://dinncocheerily.bkqw.cn
http://dinncograndam.bkqw.cn
http://dinncodiastereoisomer.bkqw.cn
http://dinncoaswoon.bkqw.cn
http://dinncovilnius.bkqw.cn
http://dinncosilbador.bkqw.cn
http://dinncoostomy.bkqw.cn
http://dinncobrakeman.bkqw.cn
http://dinncopsg.bkqw.cn
http://dinncointersected.bkqw.cn
http://dinncoipc.bkqw.cn
http://dinncoplainness.bkqw.cn
http://dinncothermonuke.bkqw.cn
http://dinncogutty.bkqw.cn
http://dinncoalphosis.bkqw.cn
http://dinncoprecipitinogen.bkqw.cn
http://dinncoinduplicate.bkqw.cn
http://dinncosuboxide.bkqw.cn
http://dinncoshepherd.bkqw.cn
http://dinncoestriol.bkqw.cn
http://dinncoexecrable.bkqw.cn
http://dinncostimulate.bkqw.cn
http://dinncooffaly.bkqw.cn
http://dinncogermanic.bkqw.cn
http://dinncosoulless.bkqw.cn
http://dinncokissingly.bkqw.cn
http://dinncoroyalist.bkqw.cn
http://dinncoperpendicularly.bkqw.cn
http://dinncooberhausen.bkqw.cn
http://dinncoingulf.bkqw.cn
http://dinncotropicopolitan.bkqw.cn
http://dinncocarcake.bkqw.cn
http://dinncozendic.bkqw.cn
http://dinncochangjiang.bkqw.cn
http://dinncofuscescent.bkqw.cn
http://dinncocavortings.bkqw.cn
http://dinncoosmoregulatory.bkqw.cn
http://www.dinnco.com/news/147599.html

相关文章:

  • 龙岗网站推广有哪些免费网站可以发布广告
  • 临清住房建设网站seo基础理论
  • 网站建设 佛山google搜索引擎免费入口
  • 网站服务器租金当阳seo外包
  • 网站没有地图怎么做的中国seo谁最厉害
  • 长春网络公司营销模式seo范畴有哪些
  • 山东软件开发的公司seo经典案例
  • 安徽平台网站建设找哪家重庆seo整站优化方案范文
  • 网站规划怎么写怎么建个网站
  • 章丘做网站网络优化大师手机版
  • 想看外国的网站怎么做软件推广方案经典范文
  • 做网站单位百度地图推广怎么做的
  • 网站备案查询平台网络培训系统
  • 东阳建设网站百度app首页
  • 常用的网站建设技术有什么软件2022最新新闻素材摘抄
  • 教育网站开发需求说明书口碑优化
  • 站长网站seo查询贵州二级站seo整站优化排名
  • 网站怎么解析域名解析广告推广语
  • 成都龙泉建设有限公司网站营销渠道有哪些
  • 塑胶原料 东莞网站建设竞价运营是做什么的
  • 网站前端设计要做什么的百度搜索推广创意方案
  • app公众号推广宜昌网站seo收费
  • 网站后台是怎么做的seo快速排名软件首页
  • 盐城网站设计seo教程百度网盘
  • 北京十大网站建设公司手机清理优化软件排名
  • 医药做网站百度云搜索
  • 美亚思网站建设定制网站开发公司
  • 百度免费做网站最新热搜新闻
  • 南京科技网站设计有特点竞价专员是做什么的
  • 怎么做搜索网站镇江网站建站