zoukankan      html  css  js  c++  java
  • RabbitMQ入门-队列

    先建工程

    下一步,输入坐标

    下一步,输入工程名

    先看一下最终目录

    修改pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <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.example.demo</groupId>
        <artifactId>rebbitmq-hello</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>com.rabbitmq</groupId>
                <artifactId>amqp-client</artifactId>
                <version>5.1.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.25</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.25</version>
            </dependency>
    
        </dependencies>
    
    </project>

    Send类:

    package com.example.demo;
    
    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 Send {
    
        private static final String QUEUE_NAME = "hello";
    
        public static void main(String[] args) throws IOException, TimeoutException {
            ConnectionFactory factory = new ConnectionFactory();    // 连接工厂
            factory.setHost("localhost");
            Connection connection = factory.newConnection();        // 获取连接
            Channel channel = connection.createChannel();
    
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);    // 声明队列,只有他不存在的时候创建
            String msg = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            System.out.println("Sending:" + msg);
            channel.close();
            connection.close();
    
        }
    
    }

    Receive类:

    package com.example.demo;
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    public class Receive {
    
        private static final String QUEUE_NAME = "hello";
    
        public static void main(String[] args) throws IOException, TimeoutException {
            ConnectionFactory factory = new ConnectionFactory();    // 连接工厂
            factory.setHost("localhost");
            Connection connection = factory.newConnection();        // 获取连接
            Channel channel = connection.createChannel();
    
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);    // 声明队列,只有他不存在的时候创建
    
            Consumer consumer = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String recv = new String(body, "UTF-8");
                    System.out.println("Receive:" + recv);
                }
            };
    
            channel.basicConsume(QUEUE_NAME, true, consumer);
        }
    }

    先启动Send,查看控制台

    兔子管控台

    再启动Receive

    兔子管控台:已经消费掉

  • 相关阅读:
    asterisk 使用 g729 g723
    读书笔记《一线架构师》
    能和LoadRunner匹敌的VS2010/2012Web负载测试
    总结 设计模式,企业应用架构模式,架构模式
    聊聊Memcached的应用
    WPF小试牛刀
    读书笔记《Hadoop开源云计算平台》
    高性能与可扩展
    闲聊下架构、框架,以及架构师...
    自己写框架 实践 (Event Framework)
  • 原文地址:https://www.cnblogs.com/LUA123/p/8471715.html
Copyright © 2011-2022 走看看