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

RabbitMQ生产者和消费者

生产者

要导入的依赖

        <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();
        }

    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值