rabbitmq

AMQP协议

在这里插入图片描述

rabbitmq的七种模式

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

引入依赖

在这里插入图片描述

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

hello world 模型

公共类

package com.guixiansong.rabbitmq;

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 Before {
    //创建连接rabbitmq的连接工厂对象
    ConnectionFactory connectionFactory = null;
    //获取连接对象
    Connection connection = null;
    Channel channel = null;
    public void before() throws IOException, TimeoutException {
        //创建连接rabbitmq的连接工厂对象
//        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory=new ConnectionFactory();
        //设置连接rabbitmq的主机
        connectionFactory.setHost("localhost");
        //设置连接rabbitmq的端口号
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机
        connectionFactory.setVirtualHost("gxs");
        //设置连接该虚拟主机的用户名和密码
        connectionFactory.setUsername("gxs");
        connectionFactory.setPassword("gxs");

        //获取连接对象
//        Connection connection = connectionFactory.newConnection();
        connection=connectionFactory.newConnection();
        //获取连接中通道
        channel = connection.createChannel();

        //通道绑定对应消息队列
        //参数一:队列名称,如果不存在则自动创建
        //参数二:用来定义队列特性是否要持久化  true:持久化队列    false:不持久化队列
        //参数三: exclusive:是否独占队列  true:独占队列   false:不独占队列
        //参数四:autoDelete:是否在消费完成后自动删除队列  true:自动删除   false:不自动删除
        //参数五:额外附加参数
        channel.queueDeclare("hello",false,false,false,null);
    }

    public void close() throws IOException, TimeoutException {
        this.channel.close();
        this.connection.close();
    }

    public Before() throws IOException, TimeoutException {
        this.before();
    }
}

生产者

package com.guixiansong.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;

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

public class Provider {
    @Test
    public void testSendMessage() throws IOException, TimeoutException {
        Before before = new Before();
        Channel channel=before.channel;

        //发布消息
        //参数一:交换机名称
        //参数二:队列名称
        //参数三:传递消息额外设置
        //参数四:消息的具体内容
        channel.basicPublish("","hello",null,"hello rabbitmq guixiansong!".getBytes());

        before.close();
    }
}

消费者

package com.guixiansong.rabbitmq;

import com.rabbitmq.client.*;
import org.junit.Test;

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

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        Before before = new Before();
        Channel channel=before.channel;

        //消费消息
        //参数1:消费的消息的那个队列的名称
        //参数2:开始消息的自动确认机制
        //参数3:消费时的回调接口
        channel.basicConsume("hello",true,new DefaultConsumer(channel){
            /**
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body 从消息队列中取出的消息
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("new String(body)="+new String(body));
            }
        });

//        before.close();
    }


    @Test
    public void testConsumerMessage() throws IOException, TimeoutException {
        Before before = new Before();
        Channel channel=before.channel;

        //消费消息
        //参数1:消费的消息的那个队列的名称
        //参数2:开始消息的自动确认机制
        //参数3:消费时的回调接口
        channel.basicConsume("hello",true,new DefaultConsumer(channel){
            /**
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body 从消息队列中取出的消息
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("new String(body)="+new String(body));
            }
        });

        before.close();
    }
}

注意点
消费者的方法只能用main方法,因为要一直连接rabbitmq,不然在测试方法结束的时候,消费的异步方法还没有执行,导致消息消费了,但是控制台还没有打印

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值