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

网站推送怎么做的天河区做网站的公司

网站推送怎么做的,天河区做网站的公司,网站建设案例价位,邢台网络运营中心处理中心大纲 创建Core交换器用户登录发起聊天邀请接受邀请聊天实验过程总结代码工程 经过之前的若干节的学习#xff0c;我们基本掌握了Rabbitmq各个组件和功能。本文我们将使用之前的知识搭建一个简单的单人聊天服务。 基本结构如下。为了避免Server有太多连线导致杂乱#xff0c;下… 大纲 创建Core交换器用户登录发起聊天邀请接受邀请聊天实验过程总结代码工程 经过之前的若干节的学习我们基本掌握了Rabbitmq各个组件和功能。本文我们将使用之前的知识搭建一个简单的单人聊天服务。 基本结构如下。为了避免Server有太多连线导致杂乱下图将Server画成两个模块实则是一个服务。 该服务由两个核心交换器构成。 Core交换器是服务启动时创建的它主要是为了向不同用户传递“系统通知型”消息。比如Jerry向Tom发起聊天邀请则是通过上面黑色字体6-10的流程发给了Core交换器。然后Core交换器将该条消息告知Tom。 Fanout交换器是用来消息传递的。Jerry和Tom都向其发送消息然后路由到两个队列。它们两各自订阅一个队列就可以看到彼此的聊天内容了。 创建Core交换器 package com.rabbitmq.chat.service;import java.util.Map; import java.util.concurrent.locks.ReentrantLock;import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import jakarta.annotation.PostConstruct; import reactor.core.publisher.Flux;Service public class Core {Autowiredprivate RabbitTemplate rabbitTemplate;private ConnectionFactory connectionFactory;final String exchangeName Core;PostConstructpublic void init() {connectionFactory rabbitTemplate.getConnectionFactory();createExchange(exchangeName);}private void createExchange(String exchangeName) {rabbitTemplate.execute(channel - {channel.exchangeDeclare(exchangeName, direct, false, true, null);return null;});}用户登录 用户登录后我们会创建一个“系统通知”队列。然后用户就会通过长连接形式持续等待系统发出通知。 private final ReentrantLock lock new ReentrantLock();final private MapString, SimpleMessageListenerContainer listeners new java.util.HashMap();public FluxString Login(String username) {createExclusiveQueue(username);createBanding(exchangeName, username, username);return Flux.create(emitter - {SimpleMessageListenerContainer container getListener(username, (Message message) - {String msg new String(message.getBody());System.out.println(Received message: msg);emitter.next(msg);});container.start();});}private void createExchange(String exchangeName) {rabbitTemplate.execute(channel - {channel.exchangeDeclare(exchangeName, direct, false, true, null);return null;});}private void createBanding(String exchangeName, String queueName, String routingKey) {rabbitTemplate.execute(channel - {channel.queueBind(queueName, exchangeName, routingKey);return null;});}private SimpleMessageListenerContainer getListener(String queueName, MessageListener messageListener) {lock.lock();try {SimpleMessageListenerContainer listener listeners.get(queueName);if (listener null messageListener ! null) {listener new SimpleMessageListenerContainer();listener.setConnectionFactory(connectionFactory);listener.setQueueNames(queueName);listener.setMessageListener(messageListener);listeners.put(queueName, listener);}return listener;} finally {lock.unlock();}}Controller如下 package com.rabbitmq.chat.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import com.rabbitmq.chat.service.Core;import reactor.core.publisher.Flux;RestController RequestMapping(/user) public class UserController {Autowiredprivate Core core;PostMapping(value /login, produces text/event-stream)public FluxString login(RequestParam String username) {return core.Login(username);} }发起聊天邀请 发起聊天邀请时系统会预先创建一个聊天室ChatRoomInfo 。它包含上图中Fanout交换器、以及聊天双方需要订阅的消息队列。 这些创建完后发起方就会等待对方发送的消息也可以自己和自己聊天。因为消息队列已经创建好了只是对方还没使用。 package com.rabbitmq.chat.service;import java.util.Map; import java.util.concurrent.locks.ReentrantLock;import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import jakarta.annotation.PostConstruct; import lombok.Data; import reactor.core.publisher.Flux;Service public class ChatRoom {Autowiredprivate RabbitTemplate rabbitTemplate;private ConnectionFactory connectionFactory;Dataprivate class ChatRoomInfo {private String exchange;private MapString, String usernameToQueuename;}private final MapString, ChatRoomInfo chatRooms new java.util.HashMap();private final ReentrantLock lock new ReentrantLock(); PostConstructpublic void init() {connectionFactory rabbitTemplate.getConnectionFactory();}public FluxString invite(String fromUsername, String toUsername) {String chatRoomName getChatRoomName(fromUsername, toUsername);ChatRoomInfo chatRoomInfo chatRooms.get(chatRoomName);if (chatRoomInfo null) {createChatRoom(fromUsername, toUsername);}return talk(chatRoomName, fromUsername);}private void createChatRoom(String fromUsername, String toUsername) {String chatRoomName getChatRoomName(fromUsername, toUsername);String exchangeName chatRoomName;String fromQueueName queue- fromUsername - toUsername;String toQueueName queue- toUsername - fromUsername;rabbitTemplate.execute(action - {action.exchangeDeclare(exchangeName, fanout, false, true, null);action.queueDeclare(fromQueueName, false, true, false, null);action.queueDeclare(toQueueName, false, true, false, null);action.queueBind(fromQueueName, exchangeName, );action.queueBind(toQueueName, exchangeName, );return null;});lock.lock();try {ChatRoomInfo chatRoomInfo new ChatRoomInfo();chatRoomInfo.setExchange(exchangeName);chatRoomInfo.setUsernameToQueuename(Map.of(fromUsername, fromQueueName, toUsername, toQueueName));chatRooms.put(chatRoomName, chatRoomInfo);} finally {lock.unlock();}}接受邀请 被邀请方通过Core交换器得知有人要和它聊天。 然后接受邀请的请求会寻找聊天室信息然后订阅聊天记录队列。 public FluxString accept(String fromUsername, String toUsername) {String chatRoomName getChatRoomName(fromUsername, toUsername);return talk(chatRoomName, toUsername);}private FluxString talk(String chatRoomName, String username) {ChatRoomInfo chatRoomInfo chatRooms.get(chatRoomName);if (chatRoomInfo null) {throw new IllegalArgumentException(Chat room not found);}String queueName chatRoomInfo.getUsernameToQueuename().get(username);return Flux.create(emitter - {SimpleMessageListenerContainer listener new SimpleMessageListenerContainer();listener.setConnectionFactory(connectionFactory);listener.setQueueNames(queueName);listener.setMessageListener((Message message) - {String msg new String(message.getBody());System.out.println(username received message: msg);emitter.next(msg);});listener.start();});}聊天 聊天的逻辑就是找到聊天室信息然后向交换器发送消息。 public void chat(String fromUsername, String toUsername, String message) {String chatRoomName getChatRoomName(fromUsername, toUsername);ChatRoomInfo chatRoomInfo chatRooms.get(chatRoomName);if (chatRoomInfo null) {chatRoomName getChatRoomName(toUsername, fromUsername);chatRoomInfo chatRooms.get(chatRoomName);}if (chatRoomInfo null) {throw new IllegalArgumentException(Chat room not found);}rabbitTemplate.convertAndSend(chatRoomInfo.getExchange(), , fromUsername : message);}private String getChatRoomName(String fromUsername, String toUsername) {return fromUsername - toUsername -chat-room;}Controller侧代码 package com.rabbitmq.chat.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import com.rabbitmq.chat.service.ChatRoom; import com.rabbitmq.chat.service.Core;import reactor.core.publisher.Flux;RestController RequestMapping(/chat) public class ChatController {Autowiredprivate Core core;Autowiredprivate ChatRoom chatRoom;PutMapping(value /invite, produces text/event-stream)public FluxString invite(RequestParam String fromUsername, RequestParam String toUsername) {core.invite(fromUsername, toUsername);return chatRoom.invite(fromUsername, toUsername);}PutMapping(value /accept, produces text/event-stream)public FluxString accept(RequestParam String fromUsername, RequestParam String toUsername) {core.accept(fromUsername, toUsername);return chatRoom.accept(fromUsername, toUsername);}PostMapping(/send)public void send(RequestParam String fromUsername, RequestParam String toUsername, RequestParam String message) {chatRoom.chat(fromUsername, toUsername, message);} }实验过程 在Postman中我们先让tom登录然后jerry登录。 在后台我们看到创建两个队列 以及Core交换器的绑定关系也被更新 Jerry向Tom发起聊天邀请 可以看到Tom收到了邀请 同时新增了两个队列 以及一个交换器 Tom通过下面请求接受邀请 Jerry收到Tom接受了邀请的通知 后面它们就可以聊天了 它们的聊天窗口都收到了消息 总结 本文主要使用的知识点 direct交换器以及其绑定规则fanout交换器自动删除的交换器自动删除的队列只有一个消费者的队列WebFlux响应式编程 代码工程 https://github.com/f304646673/RabbitMQDemo
文章转载自:
http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn
http://www.morning.phjyb.cn.gov.cn.phjyb.cn
http://www.morning.lctrz.cn.gov.cn.lctrz.cn
http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn
http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn
http://www.morning.crtgd.cn.gov.cn.crtgd.cn
http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn
http://www.morning.ktnt.cn.gov.cn.ktnt.cn
http://www.morning.rppf.cn.gov.cn.rppf.cn
http://www.morning.znrgq.cn.gov.cn.znrgq.cn
http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn
http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn
http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn
http://www.morning.gwsll.cn.gov.cn.gwsll.cn
http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn
http://www.morning.krkwp.cn.gov.cn.krkwp.cn
http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn
http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn
http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn
http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn
http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn
http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn
http://www.morning.rfldz.cn.gov.cn.rfldz.cn
http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn
http://www.morning.kybpj.cn.gov.cn.kybpj.cn
http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn
http://www.morning.c7507.cn.gov.cn.c7507.cn
http://www.morning.glswq.cn.gov.cn.glswq.cn
http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn
http://www.morning.nykzl.cn.gov.cn.nykzl.cn
http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn
http://www.morning.wkwds.cn.gov.cn.wkwds.cn
http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn
http://www.morning.rqknq.cn.gov.cn.rqknq.cn
http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn
http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn
http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn
http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn
http://www.morning.kxltf.cn.gov.cn.kxltf.cn
http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com
http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn
http://www.morning.wwznd.cn.gov.cn.wwznd.cn
http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn
http://www.morning.mjqms.cn.gov.cn.mjqms.cn
http://www.morning.mmxt.cn.gov.cn.mmxt.cn
http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn
http://www.morning.nwczt.cn.gov.cn.nwczt.cn
http://www.morning.hkpn.cn.gov.cn.hkpn.cn
http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn
http://www.morning.sqdjn.cn.gov.cn.sqdjn.cn
http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn
http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn
http://www.morning.jkftn.cn.gov.cn.jkftn.cn
http://www.morning.sjpht.cn.gov.cn.sjpht.cn
http://www.morning.wgtr.cn.gov.cn.wgtr.cn
http://www.morning.dschz.cn.gov.cn.dschz.cn
http://www.morning.srcth.cn.gov.cn.srcth.cn
http://www.morning.ssgqc.cn.gov.cn.ssgqc.cn
http://www.morning.clnmf.cn.gov.cn.clnmf.cn
http://www.morning.ktntj.cn.gov.cn.ktntj.cn
http://www.morning.gstmn.cn.gov.cn.gstmn.cn
http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn
http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn
http://www.morning.mprtj.cn.gov.cn.mprtj.cn
http://www.morning.grnhb.cn.gov.cn.grnhb.cn
http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn
http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn
http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn
http://www.morning.ndyrb.com.gov.cn.ndyrb.com
http://www.morning.rxhn.cn.gov.cn.rxhn.cn
http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn
http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn
http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn
http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn
http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn
http://www.morning.gqbks.cn.gov.cn.gqbks.cn
http://www.morning.kdpal.cn.gov.cn.kdpal.cn
http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn
http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn
http://www.morning.sgmgz.cn.gov.cn.sgmgz.cn
http://www.tj-hxxt.cn/news/239031.html

相关文章:

  • 郑州网站建浙江网站建设价格低
  • 网站建设开题报告pptwordpress中文优化版
  • 做网站被骗通过网站如何做海外贸易
  • 网站建设培训中心苏州精品网站建设
  • 网站开发合同 黑客攻击条款用node.js可以做网站吗
  • 网站域名的根目录在哪里深圳建设工程交易服务
  • 微信公众号授权给网站php5 mysql网站开发实例精讲
  • 网站关键词没有指数企业网站模板 下载
  • 江苏省建设工程考试网站定制的网站源码
  • 棠下手机网站建设报价wordpress 用户积分系统
  • 网站做外链好嘛企业网站开发 文献综述
  • 网站备案 服务内容揭阳网站建设方案托管
  • 山东建设厅官方网站李兴军电商最好卖的十大产品
  • 安康网站设计传奇世界页游
  • 东莞网站建设方案外包襄樊seo排名
  • iis做的网站手机怎么访问手机怎么创建链接网址
  • 园区网站建设目的凡客建站手机版下载
  • 做网站排名优化有用吗现在做一个app大概多少钱
  • 水利网站建设管理汇报医院网站站内文章收录量多少
  • wordpress 下载模板站杭州市网站推广
  • 网站设计技术大全无锡军自考网站建设
  • 两学一做网站视频济南网站建设哪家便宜
  • 惠州企业网站建设手机app软件开发价格
  • 河北省建设信息中心网站唯美wordpress简约主题
  • 外贸开发网站公司建设公司官网介绍
  • 湖南网站推广做新的网站
  • 网站维护服务器网站建设总体设计
  • 网站的友情链接是什么意思深圳网络推广公司怎么样
  • 电信网站备案系统公司网站主页设计图片
  • 成都餐饮网站建设百度搜索网页版入口