rabbitMQ的简单模式

rabbitMQ的简单模式

1.简单模式(一个生产者对应一个消费者)

在这里插入图片描述

从图上可以看到只有三个角色:
   p 【product】: 生产者  发生消息的
   红色[queue]: 队列。  存储消息的
   C [consumer]: 消费者  消费消息

1.首先引入rabbitMQ的依赖架包

在这里插入图片描述

2.在主项目文件中添加子项目

在这里插入图片描述

3.编写生产者代码

package com.aaa.qy129;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //配置连接信息
        factory.setHost("192.168.81.166");
        //创建连接对象
        Connection connection = null;
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //创建信道
        Channel channel = null;
        try {
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建队列
        /**
         * String queue, 队列的名称
         * boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
         * boolean exclusive, 是否独占 false
         * boolean autoDelete, 是否自动删除  如果长时间没有发生消息 则自动删除
         * Map<String, Object> arguments 额外参数  先给null
         */
        try {
            channel.queueDeclare("zmj",true,false,false,null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //发生消息
        String str="仲梦君是大帅哥!!!!!!!";
        /**
         * String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
         * String routingKey, 路由key  如果没有交换机的绑定 使用队列的名称
         * BasicProperties props, 消息的一些额外配置 目前先不加 null
         * byte[] body 消息的内容
         */
        try {
            channel.basicPublish("","zmj",null,str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4.编写消费者代码

package com.aaa.qy129;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumers {
    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //配置连接
        factory.setHost("192.168.81.166");
        //创建连接对象
        Connection connection = null;
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //创建信道
        Channel channel = null;
        try {
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body接受消息
                System.out.println("消息内容:"+new String(body));
            }
        };
        try {
            channel.basicConsume("zmj",true,callback);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
集成 RabbitMQ 并不是很难,下面是一个简单的 Spring Boot 集成 RabbitMQ 的示例: 1. 首先,需要在 `pom.xml` 文件中添加 RabbitMQ 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 在 `application.yml` 文件中添加 RabbitMQ 的配置: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 创建一个消息生产者(即发送消息的一方): ```java import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Producer { @Autowired private AmqpTemplate rabbitTemplate; public void send(String message) { rabbitTemplate.convertAndSend("queue-name", message); } } ``` 4. 创建一个消息消费者(即接收消息的一方): ```java import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "queue-name") public class Consumer { @RabbitHandler public void receive(String message) { System.out.println("Received message: " + message); } } ``` 5. 最后,在任何一个组件中注入 `Producer` 并调用 `send` 方法即可发送消息,消息会被发送到名为 `queue-name` 的队列中。当有消息到达队列时,`Consumer` 中的 `receive` 方法会被自动调用,接收消息并进行处理。 以上就是一个简单的 Spring Boot 集成 RabbitMQ 的示例。需要注意的是,以上代码只是一个简单的示例,实际应用中还需要考虑很多其他因素,例如消息可靠性、消息确认机制、消息重试机制等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值