Maven integrates RabbitMQ to realize production and consumption messages

Create a Maven project

insert image description here
insert image description here

add dependencies

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.8.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>

producer code

Produce a "hello world" message using a producer

/**
* 生产者:发消息
*/
public class Producer {
    
    
 //队列名称
 public static final String QUEUE_NAME = "hello";

 //发消息
 public static void main(String[] args) throws IOException, TimeoutException {
    
    
     //创建一个连接工厂
     ConnectionFactory factory = new ConnectionFactory();
     //工厂IP连接RabbitMq队列
     factory.setHost("");
     //用户名
     factory.setUsername("admin");
     //密码
     factory.setPassword("admin");
     //创建连接
     Connection connection = factory.newConnection();
     //获取信道
     Channel channel = connection.createChannel();
     
     /**
      * 生成一个队列
      * 1.队列名称
      * 2.队列里面的消息是否持久化(磁盘),默认情况消息存在内存中
      * 3.该队列是否只供一个消费者消费,true可以多个消费者消费;false:只能一个消费者消费
      * 4.是否自动删除,最后一个消费者断开连接以后,该队一句是否自动删除 true是,false否
      * 5.其他参数
      */
     channel.queueDeclare(QUEUE_NAME,true,false,false,null);
 	 //发消息
     String message = "hello world";
  
         /**
      * 发送一个消费
      * 1.发送到哪个交换机
      * 2.路由key值是哪个,本次是队列名称
      * 3.其他参数信息
      * 4.发送的消息
      */
     channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
     System.out.println("消息发送完毕!");

 }
}

consumer code

public class Consumer {
    
    
    //队列的名称
    public static final String QUEUE_NAME = "hello";

    //接收消息
    public static void main(String[] args) throws IOException, TimeoutException {
    
    
        //创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //工厂IP连接RabbitMq队列
        factory.setHost("");
        //用户名
        factory.setUsername("admin");
        //密码
        factory.setPassword("admin");
        //创建连接
        Connection connection = factory.newConnection();
        //获取信道
        Channel channel = connection.createChannel();

        //声明,接收消息
        DeliverCallback deliverCallback = (consumerTag,message)->{
    
    
            System.out.println(new String(message.getBody()));
        };

        //取消消息的回调
        CancelCallback cancelCallback = consumerTag->{
    
    
            System.out.println("消息消费被中断");
        };

        /**
         *消费者消费信息
         * 1.消费哪个队列
         * 2.消费成功后是否自动应答,true/false
         * 3.消费者未成功消费的回调
         * 4.消费者取消消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}

test

Run the producer code first to produce messages

insert image description here
If successful, run the consumer code again and consume the message.
insert image description here
The test is successful.

Articles in the same series

Principle part

Introduction to MQ (message queue) Introduction
to RabbitMQ Introduction to
RabbitMQ Four core concepts and working principles

Operating part

Docker for Windows installs RabbitMq
SpringBoot integrates RabbitMQ to realize production and consumption messages
RabbitMQ delay queue and actual combat
RabbitMQ release confirmation-switch confirmation
RabbitMQ-message return (queue confirmation)
RabbitMQ-backup switchRabbitMQ
-priority queue

Guess you like

Origin blog.csdn.net/m0_68681879/article/details/129554246