zoukankan      html  css  js  c++  java
  • RabbitMQ生产者消费者模型构建(三)

    ConnectionFactory:获取连接(地址,端口号,用户名,密码,虚拟主机等)

    Connection:一个连接

    Channel:数据通信信道,可发送、接收消息

    Queue:具体的消息存储队列

    Producer&Consumer:生产和消费者

    //生产端代码
    
    //创建连接工厂,进行设置
     ConnectionFactory connectionFactory = new ConnectionFactory();
     connectionFactory.setHost("127.0.0.1");
     connectionFactory.setPort(5672);
     connectionFactory.setVirtualHost("/");
     connectionFactory.setUsername("guest");
     connectionFactory.setPassword("guest");
    
     //创建连接
     Connection connection = connectionFactory.newConnection();
    
     //创建通道
     Channel channel = connection.createChannel();
    
     //发送数据
     String str = "hello world";
     channel.basicPublish("", "test001", null, str.getBytes());
    
     //关闭连接
     channel.close();
     connection.close();
    //消费者端代码
    
    //1 创建一个ConnectionFactory, 并进行配置
     ConnectionFactory connectionFactory = new     ConnectionFactory();
     connectionFactory.setHost("127.0.0.1");
     connectionFactory.setPort(5672);
     connectionFactory.setVirtualHost("/");
            
     //2 通过连接工厂创建连接
     Connection connection = connectionFactory.newConnection();
            
     //3 通过connection创建一个Channel
     Channel channel = connection.createChannel();
            
     //4 声明(创建)一个队列
     String queueName = "test001";
     //String queue, boolean durable, boolean exclusive, boolean  autoDelete, Map<String, Object> arguments
     //队列名称 (true持久化,队列不会消失), 独占(顺序队列,只有一个队列可以连接)
     channel.queueDeclare(queueName, true, false, false, null);
            
     //5 创建消费者
     QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
            
     //6 设置Channel
     channel.basicConsume(queueName, true, queueingConsumer);
            
     while(true){
        //7 获取消息
        Delivery delivery = queueingConsumer.nextDelivery();
        String msg = new String(delivery.getBody());
        System.err.println("消费端: " + msg);
        Map<String, Object> headers = delivery.getProperties().getHeaders();
        System.err.println("headers get my1 value: " + headers.get("my1"));
                
        //Envelope envelope = delivery.getEnvelope();
     }
    //pom.xml
    
    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--     <dependency>
                   <groupId>com.rabbitmq</groupId>
                   <artifactId>amqp-client</artifactId>
                   <version>3.6.5</version>
                 </dependency>-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.4.2</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </build>
  • 相关阅读:
    leetcode 350. Intersection of Two Arrays II
    leetcode 278. First Bad Version
    leetcode 34. Find First and Last Position of Element in Sorted Array
    leetcode 54. Spiral Matrix
    leetcode 59. Spiral Matrix II
    leetcode 44. Wildcard Matching
    leetcode 10. Regular Expression Matching(正则表达式匹配)
    leetcode 174. Dungeon Game (地下城游戏)
    leetcode 36. Valid Sudoku
    Angular Elements
  • 原文地址:https://www.cnblogs.com/luhan777/p/11156147.html
Copyright © 2011-2022 走看看