RabbitMQ学习(一)-- producer&&consumer

目录

一、producer

1,引入包

2,创建main

3,测试

二、consumer

1,代码

2,测试


一、producer

1,引入包

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

2,创建main

package com.wayne;

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

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

public class Producer_hello_world {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1,创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2,设置参数(host,port,virtual host,username,guest)
        factory.setHost("10.211.55.7");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");

        //3,创建连接
        Connection connection = factory.newConnection();

        //4,创建channel
        Channel channel = connection.createChannel();

        //5,创建队列Queue
        //String queue--队列名称,
        // boolean durable--是否持久化,
        // boolean exclusive--是否独占(只有一个消费者)当connection关闭时,是否删除队列, boolean autoDelete--是否允许自动删除(没有消费者时),
        // Map<String, Object> arguments-- 参数
        channel.queueDeclare("hello_world", true, false, false, null);

        //6,发送消息
        //String exchange--交换机名称,简单模式下,使用默认值 ""空字符串
        // String routingKey--路由名称,当使用默认的 ""(空字符串)的exchange时,和队列名称保持一致。
        // BasicProperties props--属性,
        // byte[] body--实际发送消息内容
        channel.basicPublish("","hello_world",null,"hello rabbitmq".getBytes());

        //7,关闭连接,释放资源
        channel.close();
        connection.close();
    }
}

3,测试

运行之后,打开控制界面

 

二、consumer

1,代码

package com.wayne;

import com.rabbitmq.client.*;

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

public class Consumer_hello_world {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1,创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2,设置参数(host,port,virtual host,username,guest)
        factory.setHost("10.211.55.7");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");

        //3,创建连接
        Connection connection = factory.newConnection();

        //4,创建channel
        Channel channel = connection.createChannel();

        //5,创建队列Queue
        //String queue--队列名称,
        // boolean durable--是否持久化,
        // boolean exclusive--是否独占(只有一个消费者)当connection关闭时,是否删除队列, boolean autoDelete--是否允许自动删除(没有消费者时),
        // Map<String, Object> arguments-- 参数
        channel.queueDeclare("hello_world", true, false, false, null);

        //6,创建消费者
        //String queue--队列名称, boolean autoAck--是否发送自动确认信息, Consumer callback--回调对象)
        channel.basicConsume("hello_world",true,new DefaultConsumer(channel){

            //糊掉对象中的方法
            //String consumerTag, -- 消费者标签
            // Envelope envelope, -- 一些信息 例如交换机、路由信息
            // AMQP.BasicProperties properties, 配置信息
            // byte[] body --实际的消息体
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumerTag:"+consumerTag);
                System.out.println("Exchange:"+envelope.getExchange());
                System.out.println("routerKey:"+envelope.getRoutingKey());
                System.out.println("properties:"+properties);
                System.out.println("body:"+new String(body));
            }
        });


    }
}

2,测试

控制台:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值