当前位置: 首页 > 工具软件 > Dark Exchange > 使用案例 >

RabbitMQ生产者和消费者原始写法_Dark_Blue

狄信然
2023-12-01

生产者

要导入的依赖

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
    </dependencies>

代码:


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

import java.io.IOException;

/**
 * @auther July
 * @create 2021-**-**
 * @desc rabbitmq生产者测试发送消息
 */

public class Producer01 {

    private static final String QUEUE = "helloworld";

    public static void main(String[] args) {


        //通过连接工厂创建新的连接和mq建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置虚拟机路径
        connectionFactory.setVirtualHost("/");
        //设置主机IP
        connectionFactory.setHost("127.0.0.1");
        //设置端口号
        connectionFactory.setPort(5672);
        //设置用户名
        connectionFactory.setUsername("guest");
        //设置密码
        connectionFactory.setPassword("guest");

        Connection connection = null;
        Channel channel = null;
        try {
            //创建连接
            connection = connectionFactory.newConnection();
            //创建连接通道
            channel = connection.createChannel();
            /**
             * 声明队列
             * 1.队列名称
             * 2.是否持久化
             * 3.该队列是否独占此通道
             * 4.不使用时是否删除此队列
             * 5.队列扩展参数
             *
             */
            channel.queueDeclare(QUEUE,true,false,false,null);
            String message = "hello world!";
            /**
             * 发布消息
             * 1.交换机名称
             * 2.队列名称
             * 3.基本属性
             * 4.要发送的信息
             */
            channel.basicPublish("", QUEUE, null, message.getBytes());
            System.out.println("send to mq :" + message);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                try {
                    channel.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

消费者

要导入的依赖:

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>

代码:


import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @auther July
 * @create 2021-**-**
 * @desc rabbitmq消费者测试
 */
public class Custom01 {

    private static final String QUEUE = "helloworld";

    public static void main(String[] args) {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");

        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE, true, false, false, null);

            //接收到消息之后执行的方法
            DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //获得交换机
                    String exchange = envelope.getExchange();
                    //获取消息在mq中唯一的id
                    long deliveryTag = envelope.getDeliveryTag();
                    //获取路由key
                    String routingKey = envelope.getRoutingKey();
                    //获取消息体
                    String message = new String(body, "utf-8");
                    System.out.println("result: " + message);
                }
            };

            channel.basicConsume(QUEUE, true, defaultConsumer);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
 类似资料: