RabbitMq的简单模式

在这里插入图片描述
RabbitMQ共有六种工作模式:简单模式(Simple)、工作队列模
式(Work Queue)、发布订阅模式(Publish/Subscribe)、路由
模式(Routing)、通配符模式(Topics)、远程调用模式(RPC,
不常用)

导入依赖:

 <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.14.0</version>
        </dependency>
    </dependencies>

package com.wyt.mq.simple;

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

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/***
 *wyt
 */
//生产者
public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建链接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.130.132");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("root");
        connectionFactory.setVirtualHost("/");
        //2.创建链接
        Connection connection = connectionFactory.newConnection();
        //3.建立信道
        Channel channel = connection.createChannel();

        //4.创建队列,如果队列已经存在,则使用该队列
        /**
         * 队列名
         * 是否持久化
         * 是否私有化 f表示所有消费者都可访问,t表示只有第一次拥有他的消费者才可访问
         * 是否自动删除
         * 其他额外参数
         */
        channel.queueDeclare("simple_queue",false,false,false,null);

        //5.发送信息
        String message="hello message";
        /***
         * 交换机,“”表示默认交换机
         * 路由键,一个简单模式,使用队列名就行
         * 额外参数
         * 传递的消息字节数组
         */
        channel.basicPublish("","simple_queue",null,message.getBytes(StandardCharsets.UTF_8));

        //6.关闭信道和链接
        channel.close();
        connection.close();
        System.out.println("====发送成功===");
    }
}

package com.wyt.mq.simple;

import com.rabbitmq.client.*;

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

/***
 *wyt
 */
//消费者
public class Customer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建链接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.130.132");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("root");
        connectionFactory.setVirtualHost("/");
        //2.创建链接
        Connection connection = connectionFactory.newConnection();
        //3.建立信道
        Channel channel = connection.createChannel();
        //4.监听队列
        /**
         * 监听的队列名
         * 是否自动签收,false的话,需要手动确认消息已收到
         * Consumer的实现类,重写该方法表示接受到消息后如何消费
         */
        channel.basicConsume("simple_queue",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message=new String(body,"UTF-8");
                System.out.println("接受消息为:"+message);
            }
        });
        //5.
    }
}

在这里插入图片描述
其特点:一个生产者对应一个消费者

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值