SpringBoot Rabbitmq发送消息

官方文档:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp

引入依赖:

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

发送消息代码:

@RestController
@RequestMapping("/")
public class SenderMsgController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping(value = "/{str}")
    public void testSend(@RequestParam("str") String str) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            int millis = 500;
            Thread.sleep(new Long(millis));
            if (i%2 == 1) {
                String isr = "{\n" + "\t\"dd\":" + i + "}";

                amqpTemplate.convertAndSend("DirectExchange","test.1",isr,new MyMessageConverter());

            }else{
                String isr = "{\n" + "\t\"dd\":" + i + "}";
                amqpTemplate.convertAndSend("DirectExchange","test.2",isr,new MyMessageConverter());
            }
            System.out.println("第"+i+"次发送");
        }
    }
}
MyMessageConverter:
//MessagePostProcessor 接口可以对发送请求之前的Message 进行操作,这里我设置了contenttype为json格式
public class MyMessageConverter implements MessagePostProcessor {
    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_JSON);
        return message;
    }
}

创建交换机、队列并互相绑定设置路由key

@Component
public class AmqpAdminConfig {

    @Autowired
    public AmqpAdmin amqpAdmin;

    @Bean
    public DirectExchange createDirectExchange(){
        DirectExchange directExchange = new DirectExchange("DirectExchange", false, false);
        amqpAdmin.declareExchange(directExchange);
        return directExchange;
    }

//    @Bean
//    public void createFanoutExchange(){
//        amqpAdmin.declareExchange(new FanoutExchange("FanoutExchange",false,false));
//    }

    @Bean
    public Queue createQueue1(){
        Queue queue = new Queue("queue-1", false, false, false);
        amqpAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Queue createQueue2(){
        Queue queue = new Queue("queue-2", false, false, false);
        amqpAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public void createBinding1(){
        Binding bind = BindingBuilder.bind(createQueue1()).to(createDirectExchange()).with("test.1");
        amqpAdmin.declareBinding(bind);
    }
    @Bean
    public void createBinding2(){
        Binding bind = BindingBuilder.bind(createQueue2()).to(createDirectExchange()).with("test.2");
        amqpAdmin.declareBinding(bind);
    }

}

根据官方文档知道AmqpTemplate 和AmqpAdmin  已经自动配置,可直接注入使用,AmqpTemplate 封装了发送与接收的各种操作,AmqpAdmin  封装了针对交换机和消息队列的各种操作

原文地址:https://www.cnblogs.com/shiguotao-com/p/10445130.html

时间: 2024-07-30 18:55:18

SpringBoot Rabbitmq发送消息的相关文章

SpringBoot+RabbitMQ实现消息可靠性投递

摘抄自简书:https://www.jianshu.com/p/9feddd4af8ee RabbitMQ是目前主流的消息中间件,非常适用于高并发环境.各大互联网公司都在使用的MQ技术,晋级技术骨干.团队核心的必备技术! 谈到消息的可靠性投递,无法避免的,在实际的工作中会经常碰到,比如一些核心业务需要保障消息不丢失,接下来我们看一个可靠性投递的流程图,说明可靠性投递的概念: Step 1: 首先把消息信息(业务数据)存储到数据库中,紧接着,我们再把这个消息记录也存储到一张消息记录表里(或者另外一

Spring-boot JMS 发送消息慢的问题解决

1:在<ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用>(http://www.cnblogs.com/yshyee/p/7277801.html)中,采用以下代码进行JMS消息发送: @Service public class Producer { @Autowired private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destin

RabbitMQ发送消息+python

接口使用两个queue监听信息,且有两个测试环境,所以需要向mq中发送测试数据: python使用pika包:Pika is a RabbitMQ (AMQP-0-9-1) client library for Python. 可以参照: https://github.com/pika/pika import pika connection = pika.BlockingConnection() channel = connection.channel() channel.basic_publi

给RabbitMQ发送消息时,设置请求头Header。

消费者 由于消费者那里,@Payload是接受的消息体,使用了@Header注解,需要请求头,生产者这边就要设置,如下代码: 这是RabbitTemplate中的converAndSend(exchang,routingKey,消息体,消息头)方法. @Override public void convertAndSend(String exchange, String routingKey, final Object message, final MessagePostProcessor me

Java秒杀系统实战系列~整合RabbitMQ实现消息异步发送

摘要: 本篇博文是“Java秒杀系统实战系列文章”的第八篇,在这篇文章中我们将整合消息中间件RabbitMQ,包括添加依赖.加入配置信息以及自定义注入相关操作组件,比如RabbitTemplate等等,最终初步实现消息的发送和接收,并在下一篇章将其与邮件服务整合,实现“用户秒杀成功发送邮件通知消息”的功能! 内容: 对于消息中间件RabbitMQ,想必各位小伙伴没有用过.也该有听过,它是一款目前市面上应用相当广泛的消息中间件,可以实现消息异步通信.业务服务模块解耦.接口限流.消息分发等功能,在微

springboot项目整合rabbitMq涉及消息的发送确认,消息的消费确认机制

1.引入maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>2.在application.yml的配置: spring: rabbitmq: host: 106.52.82.241 port: 5672 username: yang

springboot核心技术(五)-----消息(rabbitmq)

消息 1. 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2. 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination) 当消息发送者发送消息以后,将由消息代理接管,消息代理保证消息传递到指定目 的地. 3. 消息队列主要有两种形式的目的地 1. 队列(queue):点对点消息通信(point-to-point) 2. 主题(topic):发布(publish)/订阅(subscribe)消息通信 异步处理 1.同步处理方式 2.多

rabbitmq 学习-9- RpcClient发送消息和同步接收消息原理

rabbitmq 学习-9- RpcClient发送消息和同步接收消息原理

Bluemix结合RabbitMq实现消息发送与接收实例

什么是RabbitMq? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消 息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术.排队指的是应用程序通过 队列来通信.队列的使用除去了接收和发送应用程序同时执行的要求. 什么是Bluemix? BlueMix 是 IBM 基于 Cloud Foundr