RabbitMQ 采用java客户端生产消费案例(不集成spring)

1、进入到RabbitMQ的安装根目录下,进入sbin目录,双击 rabbitmq-server.bat 启动 rabbitmq 

如不会安装rabbitmq ,笔者上一篇文章中有详细介绍 ,附上链接:https://www.cnblogs.com/zhanh247/p/12037760.html

 

 双击后 cmd 马上关闭,属于正常现象,在任务管理器中有如下,说明 rabbitmq 是正常运行的

 

 

2、在pom.xml 文件中映入rabbitmq 坐标

    <!-- rabbitmq -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.5.3</version>
        </dependency>
        <!-- slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.2</version>
        </dependency>

3、编写生产者Provider

package com.hao.rabbitmq;

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

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

/**
 * @desc 生产者
 * @author zhanh247
 */
public class Provider {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");

        // 设置RabbitMQ地址
        factory.setHost("localhost");
        factory.setVirtualHost("/");

        // 建立到代理服务器连接
        Connection connection = factory.newConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换器
        String exchange = "hello-exchange";
        channel.exchangeDeclare(exchange, "direct", true);

        String routingKey = "hello-routingKey";
        // 发布消息
        byte[] body = "hello-body".getBytes();
        System.out.println("RabbitMQ 生产者启动成功。。。。");
        channel.basicPublish(exchange, routingKey, null, body);

        // 关闭信道和连接
        channel.close();
        connection.close();
    }

}

4、编写消费者 Consumer

package com.hao.rabbitmq;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

/**
 * @desc 消费者
 * @author zhanh247
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");

        // 设置RabbitMQ地址
        factory.setHost("localhost");
        factory.setVirtualHost("/");

        // 建立到代理服务器连接
        Connection connection = factory.newConnection();

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

        // 声明交换器
        String exchange = "hello-exchange";
        channel.exchangeDeclare(exchange, "direct", true);

        // 声明队列
        String queue = channel.queueDeclare().getQueue();
        String routingKey = "hello-routingKey";

        // 绑定队列
        channel.queueBind(queue, exchange, routingKey);
        System.out.println("RabbitMQ 消费者启动成功。。。。");
        while (true) {
            // 消费消息
            boolean autoAck = false;
            String consumerTag = "";
            channel.basicConsume(queue, autoAck, consumerTag, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
                        byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    String routingKey = envelope.getRoutingKey();
                    String contentType = properties.getContentType();
                    System.out.println("消费的路由键 routingKey: " + routingKey);
                    System.out.println("消费的内容类型 contentType: " + contentType);

                    long deliveryTag = envelope.getDeliveryTag();
                    channel.basicAck(deliveryTag, false);
                    String bodystr = new String(body, "UTF-8");
                    System.out.println("消费的消息内容 bodystr: " + bodystr);
                }
            });
        }
    }

}

5、先启动消费者看到如下,则说明生产者启动成功

6、启动生产者,接受到消息如下,则说明消费成功

 

 

posted on 2019-12-15 23:51  牛鼻子老赵  阅读(506)  评论(0编辑  收藏  举报