rabbitmq入门案例

本文介绍了如何使用Java进行RabbitMQ的入门实践,包括生产者和消费者的代码实现。通过添加RabbitMQ依赖,创建消息队列,编写生产者发送消息到队列以及消费者从队列接收并处理消息的代码,详细展示了RabbitMQ的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖:

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

生产者主要代码:

package com.soft.simple;

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

/**
 * Created by Administrator on 2020/9/6 0006.
 */
public class SimpleProvider {
    static String QUEUE_NAME = "simple";

    public static void main(String[] args) throws Exception {
        /*1、创建工厂(设置相关参数)*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        /*主机:默认是localhost*/
        connectionFactory.setHost("localhost");
        /*端口:默认是5672*/
        connectionFactory.setPort(5672);
        /*虚拟主机:默认是/*/
        connectionFactory.setVirtualHost("/guest");
        /*用户名:guest*/
        connectionFactory.setUsername("guest");
        /*密码:guest*/
        connectionFactory.setPassword("guest");

        /*2、新建连接*/
        Connection connection = connectionFactory.newConnection();

        /*3、创建频道*/
        Channel channel = connection.createChannel();

        /*4、申明队列*/
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        /*5、发送消息*/
        String message = "hello rabbitMQ!!!";
        /*
        * exchange: 交换机名称,如果是空字符串表示使用默认的交换机
        * 路由key:简单模式可以使用队列名
        * 消息的其他属性
        * 消息的字节数组*/
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

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

消费者主要代码:

package com.soft.simple;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * Created by Administrator on 2020/9/6 0006.
 */
public class SimpleComsumer {

    public static void main(String[] args) throws Exception {
        /*创建工厂,并设置参数*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/guest");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        /*新建连接*/
        Connection connection = connectionFactory.newConnection();
        /*创建频道*/
        Channel channel = connection.createChannel();
        /*申明队列*/
        channel.queueDeclare(SimpleProvider.QUEUE_NAME, true, false, false, null);
        /*创建消费者*/
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(consumerTag + "------" + envelope + "------" + properties + "------" + new String(body));
            }
        };
        /*监听*/
        channel.basicConsume(SimpleProvider.QUEUE_NAME, true, defaultConsumer);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值