rabbitmq异步处理_怎么解决js异步方法执行顺序

rabbitmq异步处理_怎么解决js异步方法执行顺序RabbitMQ即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用。使用RabbitMQ实现异步更新文章浏览量,提升阅读文章时的响应速度。从直接更新数据库耗时450ms到异步更新数据库耗时50ms,明显提升接口性能,非常的nice~………

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

使用RabbitMQ异步执行业务

1.导入依赖

<!-- 引入RabbitMq依赖 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.编写RabbitMQ配置文件

# rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.virtual-host=/handsomeforum
# 手动指定ack
spring.rabbitmq.listener.simple.acknowledge-mode=manual
# 开启confirm机制
spring.rabbitmq.publisher-confirm-type=simple
# 开启return机制
spring.rabbitmq.publisher-returns=true

3.编写RabbitMQ配置类

package com.handsome.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** * @Author Handsome * @Date 2022/7/4 10:12 * @Version 1.0 */
@SuppressWarnings({ 
   "all"})
	@Configuration
	public class RabbitMQConfig { 
   
	// 1.创建交换机
	@Bean
		public TopicExchange getTopicExchange() { 
   
		return new TopicExchange("handsomeforum-topic-exchange", true, false);
	}

	// 2.创建队列
	@Bean
		public Queue getQueue() { 
   
		return new Queue("handsomeforum-queue", true, false, false, null);
	}

	// 3.将交换机和队列绑定在一起
	@Bean
		public Binding getBinding() { 
   
		return BindingBuilder.bind(getQueue()).to(getTopicExchange()).with("handsomeforum.*");
	}

}

4.设置Return和Confirm机制

package com.handsome.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/** * @Author Handsome * @Date 2022/7/4 12:11 * @Version 1.0 */
@SuppressWarnings({ 
   "all"})
@Component
@Slf4j
public class PublisherConfirmAndReturnConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback { 
   
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    private void initMethod() { 
   
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) { 
   
        if (ack) { 
   
            log.info("消息已经送到Exchange中~");
        } else { 
   
            log.error("消息没有送到Exchange中~");
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { 
   
        log.error("消息没有送到Queue中~");
    }
}

5.将消息发送到交换机

// 发消息给mq异步更新文章浏览量
Map mqMap = new HashMap<>();
mqMap.put("bid", bid);
rabbitTemplate.convertAndSend("handsomeforum-topic-exchange",
"handsomeforum.readblog",
JSONObject.toJSONString(mqMap));

6.消费者消费消息

package com.handsome.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.handsome.pojo.Blog;
import com.handsome.pojo.Question;
import com.handsome.pojo.UserLogin;
import com.handsome.service.BlogService;
import com.handsome.service.QuestionService;
import com.handsome.service.UserLoginService;
import com.handsome.service.VerifyMailboxService;
import com.handsome.utils.HandsomeUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.IOException;

/** * @Author Handsome * @Date 2022/7/4 10:24 * @Version 1.0 */
@SuppressWarnings({ 
   "all"})
@Component
@Slf4j
public class Consumer { 
   

    // 配置文件中取值
    @Value(value = "${handsome.myMailbox}")
    private String myMailbox;

    @Autowired
    JavaMailSenderImpl mailSender;
    @Autowired
    VerifyMailboxService verifyMailboxService;

    @Autowired
    UserLoginService userLoginService;
    @Autowired
    BlogService blogService;
    @Autowired
    QuestionService questionService;

    // 自动ack
// @RabbitListener(queues = "handsomeforum-queue")
// public void getMessage(Object message) { 
   
// System.out.println("接收到的消息->" + message);
// }

    @RabbitListener(queues = "handsomeforum-queue")
    public void getMessage(@Payload String msg, Channel channel, Message message) throws IOException { 
   
        // 1.获取routingKey
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        // 2.使用switch
        switch (routingKey) { 
   
            // 更新文章浏览量
            case "handsomeforum.readblog":
                JSONObject readblogObject = JSON.parseObject(msg);
                String bid = (String) readblogObject.get("bid");
                Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
                blog.setViews(blog.getViews() + 1);
                // todo: redis缓存. 防止阅读重复
                blogService.updateById(blog);
                // 手动ack channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                break;
        }
    }
}

7.RabbitMQ的作用

RabbitMQ即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用。
使用RabbitMQ实现异步更新文章浏览量,提升阅读文章时的响应速度。从直接更新数据库耗时450ms到异步更新数据库耗时50ms,明显提升接口性能,非常的nice~

RabbitMq忘记用户名和密码怎么办?

1.启动RabbitMq容器

rabbitmq异步处理_怎么解决js异步方法执行顺序

2.进入RabbitMq容器

rabbitmq异步处理_怎么解决js异步方法执行顺序

3.创建账号

rabbitmq异步处理_怎么解决js异步方法执行顺序

rabbitmqctl add_user newadmin newpassword

4.设置用户角色

rabbitmq异步处理_怎么解决js异步方法执行顺序

rabbitmqctl set_user_tags newadmin administrator

5.设置用户权限

rabbitmq异步处理_怎么解决js异步方法执行顺序

rabbitmqctl set_permissions -p / newadmin "." "." ".*"

6.newadmin为新管理员账号 newpassword为密码 进行登录

rabbitmq异步处理_怎么解决js异步方法执行顺序

7.登录成功

rabbitmq异步处理_怎么解决js异步方法执行顺序

8.找回原用户名

rabbitmq异步处理_怎么解决js异步方法执行顺序

9.更新root用户密码

rabbitmq异步处理_怎么解决js异步方法执行顺序

10.用root用户登录

rabbitmq异步处理_怎么解决js异步方法执行顺序

11.删除newadmin用户

rabbitmq异步处理_怎么解决js异步方法执行顺序

12.成功找回root用户,非常非常的nice~

我的学习论坛

HandsomeForum:用Java编写的学习论坛,打造我们自己的圈子!(http://huangjunjie.vip:66)
文章链接(使用RabbitMQ异步执行业务):http://huangjunjie.vip:66/blog/read/66incxp18s5nfhqgwt
文章链接(RabbitMq忘记用户名和密码怎么办?):http://huangjunjie.vip:66/question/read/0y4jzhrj5ipdu86wd1

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186143.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 高校 网络安全_网络安全之道

    高校 网络安全_网络安全之道助力高校行业网络安全

    2022年8月30日
    2
  • IT行业分析(华为裁员)「建议收藏」

    IT行业分析(华为裁员)「建议收藏」近来忙于各种麻烦事务,知识图谱的多路归并一直未能跟进。但在写多路归并前,我想是时候先总结下我的这几年。总结的经验,不仅包括我个人这几年的见闻感悟与得失,也是80后这代的一个缩影,以及对社会的一些思考。首先直插正题,华为的裁员。华为我确实没呆过,不过这不妨碍我认定华为是一个人员流动性非常大的公司,和BAT、美团、京东、360、小米等等互联网公司一样大。谁更大我不知

    2022年7月18日
    34
  • 游戏行业,室内设计,哪个3d建模师更有前景?工资是不是很高啊

    游戏行业,室内设计,哪个3d建模师更有前景?工资是不是很高啊游戏建模职业分类及发展:进入游戏建模行业你可以选择不同的发展方向,比如:(1)手绘3D美术设计师:制作纯手绘风格游戏的所有3D物品如:角色、道具、建筑、山体;(2)次世代3D美术设计师:制作写实次世代风格游戏的所有3D物品,如:角色、道具、建筑。(3)关卡设计师:根据游戏风格要求,使用模型资源,搭建3D游戏世界(4)模型师:制作3D打印、影视动画中的所有模型。如:角色、道具、建筑、山体。次世代美术设计师做什么?次世代游戏:“次世代游戏”指代和同类游戏相比下更加先进的游戏.

    2022年5月19日
    36
  • 选择有这些特点的it行业人力外包公司没错

    选择有这些特点的it行业人力外包公司没错互联网的快速发展加快了传统企业信息化进程,很多传统企业自己组建软件技术部,既缺少技术开发经验,又缺乏软件项目管理经验,因此软件外包成为这些公司的首选。但完全的项目外包,使得其与软件外包公司的沟通变的不畅通,软件外包公司又缺乏传统企业的业务经验,且保密性很差,所以不少传统企业会选择和it行业人力外包公司合作来引进it人才,那么什么样的it行业人力外包公司值得选择?一、选择有一定年限的it行业人力外包公司为什么要选择一个成立时间长的it行业人力外包公司呢?因为it行业人力外包公司成立的时间越长,越能

    2022年5月19日
    44
  • 网络安全未来发展前景_十四五国家网络安全规划

    网络安全未来发展前景_十四五国家网络安全规划第1章:中国网络安全行业发展综述1.1网络安全行业概述1.1.1网络安全产业的概念分析1.1.2网络安全产品和服务分类(1)依据主要功能及形态分类(2)依据安全防御生命周期技术能力分类1.2网络安全行业发展环境分析1.2.1行业政策环境分析(1)国际政策(2)国内政策1.2.2行业社会环境分析(1)境内感染网络病毒终端数(2)境内被篡改网站数量(3)境内被植入后门网站数量(4)安全漏洞数量1.2.3行业经济环境分析(1)国内生产总值分析(2)工业增加值分析.

    2022年10月5日
    0
  • 免备案cdn的有没有,免备案cdn是适用于什么行业呢[通俗易懂]

    免备案cdn的有没有,免备案cdn是适用于什么行业呢[通俗易懂]蔚可云CDN可以进行网站加速,当然不仅仅是网站,APP也是可以进行加速,提高访问速度提升用户体验,CDN对于互联网公司是离不开的,在一定程度上可有效促进用户的转化,当然CDN还可以用于防御DDOS与CC攻击。那么问题来了,如果是没有备案的网站能不能进行CDN加速呢?大家都知道,随着互联网的发展,必须对其进行规范化,根据工信部的要求,如果你的域名没进行备案,还没取得ICP备案号,那你的网站在国内可能会被禁止用户无法打开的。当然也有例外,蔚可云就可提供免备案的CDN加速产品,支持3…

    2022年9月10日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号