RabbitMQ整合Spring Booot点对点模式

Posted toov5

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ整合Spring Booot点对点模式相关的知识,希望对你有一定的参考价值。

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.toov5</groupId>
  <artifactId>rabbitmq</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.6.5</version>
        </dependency>
    </dependencies>
   
</project>

创建连接的工具类:

//没有做成单例的  VirtualHost 需要复用
public class MQConnectionUtils 
    //创建新的连接
    public static Connection newConnection() throws IOException, TimeoutException 
         //创建连接工厂
    ConnectionFactory factory= new ConnectionFactory();
    //链接地址
    factory.setHost("192.168.91.6");
    //用户名称
    factory.setUsername("admin");
    //用户密码
    factory.setPassword("admin");
    //amqp端口号
    factory.setPort(5672);
    //连接virtualhost
    factory.setVirtualHost("/admin_toov5");
    Connection connection = factory.newConnection();
        return connection;
     

Producer类:

public class Producer 
    //队列名称
    private static final String UEUE_NAME = "test_queue";
    
    public static void main(String[] args) throws IOException, TimeoutException 
        //创建新的连接
    Connection connection = MQConnectionUtils.newConnection();
       //创建Channel
        Channel channel = connection.createChannel();
        //创建队列
        channel.queueDeclare(UEUE_NAME, false, false, false, null);
        //创建message
        String msg = "toov5_message";
        System.out.println("生产者投递消息"+msg);
        //生产者发送消息
        channel.basicPublish("",UEUE_NAME, null, msg.getBytes());
        //关闭通道和连接
         channel.close();
         connection.close();
    

运行结果,看下这个队列:

技术图片

 

 模拟Get message:

技术图片

 

 

Consumer跟 Producer基本类似:

public class Consumer 
  
        //队列名称
        private static final String QUEUE_NAME = "test_queue";    
        public static void main(String[] args) throws IOException, TimeoutException 
            System.out.println("消费者启动..........");
            //创建新的连接
        Connection connection = MQConnectionUtils.newConnection();
           //创建Channel
            Channel channel = connection.createChannel();
            // 消费者关联队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            
              DefaultConsumer defaultConsumerr = new DefaultConsumer(channel) 
                  //监听获取消息
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
                            byte[] body) throws IOException 
                        String msg =new String(body,"UTF-8");
                        System.out.println("消费者获取生产者消息:"+msg);
                    
              ;
            //牵手模式设置  默认自动应答模式  true:自动应答模式  
              channel.basicConsume(QUEUE_NAME, true, defaultConsumerr);              
              
//            //关闭通道和连接
//             channel.close();
//             connection.close();
        

运行后的结果:

技术图片

 

以上是关于RabbitMQ整合Spring Booot点对点模式的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMQ应用Demo:使用queue点对点通知消息

RabbitMQ应用Demo:使用queue点对点通知消息

SpringBoot整合RabbitMq

C#利用RabbitMQ实现点对点消息传输

rabbitMQ 点对点 一个队列可以多个消费者吗?

SpringBoot与RabbitMQ整合