首页 > 其他分享 >springboot rabbitmq配置

springboot rabbitmq配置

时间:2023-06-13 15:58:36浏览次数:36  
标签:rabbitTemplate springboot exchange 配置 RabbitKey rabbitmq new model public

YML

  rabbitmq:
    host: xxx.xxx.xxx.xxx
    port: 5672
    virtual-host: dev
    username: xxx
    password: xxx
    publisher-confirm-type: correlated
    publisher-returns: true
    listener:
      direct:
        acknowledge-mode: auto
      simple:
        acknowledge-mode: none

配置

public class RabbitConfig {

    @Resource
    private RabbitUtil rabbitUtil;

    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        rabbitTemplate.setConfirmCallback((correlationData,ack,cause)->{
            if(!ack) log.error("NFT_MQ_ConfirmCallback => cause={}", cause);
        });
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey)->{
            log.info("NFT_MQ_ReturnCallback => message={},replyCode={},replyText={},exchange={},routingKey={}", message, replyCode, replyText, exchange, routingKey);
            rabbitUtil.failMessage(message, exchange, routingKey, replyCode + " | " + replyText);
        });
        return rabbitTemplate;
    }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitKey.delay_exchange, "x-delayed-message", true, false, args);
    }

    @Bean
    public FanoutExchange globalFanoutExchange(){
        return new FanoutExchange(RabbitKey.global_fanout_exchange);
    }

}

发送

	JSONObject msgData = new JSONObject();
	msgData.put("loginType", loinType);
	msgData.put("loginPlat", "client");
	RabbitMessageModel model = RabbitMessageModel.loginModel(RabbitMessageDataType.userLogin, user.getId(), msgData);
	rabbitTemplate.convertAndSend(RabbitKey.global_fanout_exchange, "", model);

接收

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "global_fanout_queue_client", durable = "true"),
            exchange = @Exchange(name = RabbitKey.global_fanout_exchange, type = ExchangeTypes.FANOUT)
    ))
    public void globalFanoutQueue(RabbitMessageModel model) {
        RabbitMessageDataType type = RabbitMessageDataType.typeOf(model.getDataType());
        switch (type) {
            case workView: workView(model); break;
        }
    }

-------------------------------------------------------

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = RabbitKey.payment_cancel_delay + "_client", durable = "true"),
                    exchange = @Exchange(value = RabbitKey.delay_exchange, delayed = "true"),
                    key = RabbitKey.payment_cancel_delay
            )
    )
    public void cancel(RabbitMessageModel model) {
        Payment payment = paymentDao.findById(model.getDataId()).orElse(null);
        if(payment == null) return; //有可能事物回滚,没产生支付单
        paymentService.cancel(payment.getOrderNumber(), 1);
    }

标签:rabbitTemplate,springboot,exchange,配置,RabbitKey,rabbitmq,new,model,public
From: https://www.cnblogs.com/z8080/p/17477751.html

相关文章

  • Postcat X APISIX 合作插件 :一键同步,轻松配置到 APISIX
    近日,云流科技(广州)有限公司(简称“Eolink”)旗下的开源API管理工具Postcat和深圳支流科技有限公司(简称“API7支流科技”)在各自擅长的领域携手合作,推出了Postcat&ApacheAPISIX插件,用户只需要安装此插件,即可轻松地将Postcat产品中管理的API相关配置的数据同步到开源网关......
  • 完美解决SpringBoot上传图片之后,需要重服务才能访问
    上传图片后需要重新编译才能访问图片添加一个配置文件WebMvcConfigctrl+shift+alt+/选择Registry,勾选compiler.automake.allow.when.app.running勾选添加一个配置文件WebMvcConfigpackagecom.fans.common.config;importorg.springframework.context.annotation.Configu......
  • idea 配置tomcat 运行jsp项目
    1、复用idea打开jsp项目2、添加tomcat配置3、点击后会出现配置框,这里画框的地方都选上,版本选择1.8,其他的信息内容默认后,点击确认4、点击File->ProjectStructure,弹出界面选择Project,这里sdk选择1.8,语言选择85、选择Modules,右击项目名称-》选择Add-》选择Web6、配置Modules,选......
  • window下正常的springboot到mac下运行却报错
    Errorcreatingbeanwithname'defaultValidator'definedinclasspathresource[org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.NoClassDefFoun......
  • 公司已有springboot项目引入swagger
    公司已有springboot项目引入swagger1、swagger介绍官网:https://swagger.io/Swagger是一个用于生成、描述和调用RESTful接口的Web服务。通俗的来讲,Swagger就是将项目中所有(想要暴露的)接口展现在页面上,并且可以进行接口调用和测试的服务2、引入目的Swagger有以下3个重要的作......
  • windows下安装rabbitmq
    1、Erlang的安装因为RabbitMQ是用Erlang语言编写的,所以要安装RabbitMQ先要安装Erlang。下载地址:http://www.erlang.org/downloads下载完成后就双击一直next后安装[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sosn4mCm-1684822676988)(C:\Users\Adm......
  • 搭建springbootweb环境
    搭建springboot环境(idea环境)实现步骤:1.基础环境配置2.maven配置3.编写第一个程序helloworld(可能有两个小问题)4.运行(jar包运行,命令行运行)一.基础环境配置进入idea,点击file->new->project,在弹出的页面上,选择springinitiallzr并进行相关的配置点击next点击create,完成sp......
  • spring boot连接Mybatis数据库的配置文件(MySql、SQLserver、Oracle)
    序号类型地址1MySQLMySQL操作之概念、SQL约束(一)2MySQLMySQL操作之数据定义语言(DDL)(二)3MySQLMySQL操作之数据操作语言(DML)(三)4MySQLMySQL操作之数据查询语言:(DQL)(四-1)(单表操作)5MySQLMySQL操作之数据查询语言:(DQL)(四-2)(多表查询)6MySQLMySQL操作之数据控制语言:(DC)(五)7MySQLMySQL操作之数......
  • Pycharm配置远程调试
    原文链接在搞深度学习的时候,我们在本地开发,但是需要在服务器去运行工程,所以需要使用Pycharm进行远程配置,可以实现本地代码自动同步到服务器,并在本地使用服务器的解释器。条件:需要使用专业版Pycharm。远程部署点击菜单栏Tools——Deployment——Configuration点“+”,新建一个......
  • Pycharm配置远程调试
    原文链接在搞深度学习的时候,我们在本地开发,但是需要在服务器去运行工程,所以需要使用Pycharm进行远程配置,可以实现本地代码自动同步到服务器,并在本地使用服务器的解释器。条件:需要使用专业版Pycharm。远程部署点击菜单栏Tools——Deployment——Configuration点“+”,新建一个S......