RabbitMQ

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>RabbitMQ</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

</project>

Rabbit工具类:RabbitMQUtils

在提供者和消费者中有一段相同代码,可以抽取封装成工具类

package com.example.newRabbit;

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

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

public class RabbitMQUtils {

    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory(); //获取创建连接的工厂
        connectionFactory.setHost("localhost"); //设置服务主机地址
        connectionFactory.setPort(5672); //设置端口,默认5672
        connectionFactory.setVirtualHost("/"); //设置虚拟主机地址
        connectionFactory.setUsername("chocoMoss"); //设置用户名
        connectionFactory.setPassword("123456"); //设置密码
        Connection connection = connectionFactory.newConnection(); //创建连接
        return connection;
    }
}

提供者:Rabbit_provider

queueDeclare 里的参数1 和 basicPublish 的参数2要相同

package com.example.newRabbit;

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

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

public class Rabbit_provider {
    /**
     * 消息生成者
     */

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = RabbitMQUtils.getConnection();

        //创建通道
        Channel channel = connection.createChannel();

        /**
         * 声明队列
         * 参数1:队列名
         * 参数2:是否定义持久化化队列
         * 参数3:是否独占本次连接
         * 参数4:是否在不使用的时候自动删除队列
         * 参数5:队列其他参数
         */
        channel.queueDeclare("chocoMoss",true,false,false,null);

        //创建消息
        String message= "hello world";

        /**
         * 消息发送
         * 参数1:交换机名称,如果没有指定则使用默认Default Exchage
         * 参数2:路由key,简单模式可以传递队列名称
         * 参数3:消息其他属性
         * 参数4:消息内容
         */

        channel.basicPublish("","chocoMoss",null,message.getBytes());

        //关闭资源
        channel.close();
        connection.close();
    }
}

请添加图片描述

消费者:Rabbit_consumer

package com.example.newRabbit;

import com.rabbitmq.client.*;

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

public class Rabbit_consumer {
    /**
     * 消费者
     */
    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = RabbitMQUtils.getConnection();

        //创建通道
        Channel channel = connection.createChannel();

        //创建队列
        channel.queueDeclare("chocoMoss",true,false,false,null);

        //创建消费者,并设置消息处理
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            /**
             * @param consumerTag 消息者标签,在channel.basicConsume时候可以指定
             * @param envelope 消息包的内容,可从中获取消息id,消息routingkey,交换机,消息和重传标志(收到消息失败后是否需要重新发送)
             * @param properties 属性信息
             * @param body 消息
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                //路由的key
                String routingKey = envelope.getRoutingKey();

                //获取交换机信息
                String exchange = envelope.getExchange();

                //获取消息ID
                long deliveryTag = envelope.getDeliveryTag();

                //获取消息信息
                String message = new String(body,"UTF-8");

                System.out.println("routingKey:"+routingKey+",exchange:"+exchange+",deliveryTag:"+deliveryTag+",message:"+message);
            }
        };

        /**
         * 消息监听
         * 参数1:队列名称
         * 参数2:是否自动确认,设置为true为表示消息接收自动向mq回复接收到了,mq接收到会删除消息,设置为false则需要手动确认
         * 参数3:消息接收到后回调
         */
        channel.basicConsume("chocoMoss",true,defaultConsumer);

        //关闭资源(不建议关闭,建议一直监听)
//        channel.close();
//        connection.close();

    }
}

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值