• Rabbitmq 消息模式Simple 模式


    Simple 模式是最简单的一个模式,由一个生产者,一个队列,一个消费者组成,生产者将消息通过交换机(如不定义交换机,会使用默认的交换机)把消息存储到队列,消费者从队列中取出消息进行处理。
    
    用 Java demo 实现此模式
    package com.tszr.rabbitmq;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.util.concurrent.TimeoutException;
    
    public class Send {
        private final static String QUEUE_NAME = "queue1";
    
        public static void main(String[] args){
            // 1、创建连接工程
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("127.0.0.1");
            factory.setVirtualHost("/");
    
            Connection connection = null;
            Channel channel = null;
    
            try {
                // 2、创建连接、通道
                connection = factory.newConnection();
                channel = connection.createChannel();
                // 3、声明队列
                channel.queueDeclare(QUEUE_NAME, false, false, false, null);
                // 消息内容
                String message = "Hello world";
                // 4、发送消息到指定队列
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println(" [x] Sent '" + message + "'");
            } catch (TimeoutException | IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭通道
                if (channel != null && channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                // 关闭连接
                if (connection != null && connection.isOpen()) {
                    try {
                        connection.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    package com.tszr.rabbitmq;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.DeliverCallback;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    public class Recv {
        private final static String QUEUE_NAME = "queue1";
    
        public static void main(String[] args) throws IOException, TimeoutException {
            // 1、创建连接工程
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("127.0.0.1");
            factory.setVirtualHost("/");
    
            // 2、获取 Connection和 Channel
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            // 3、声明队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [x] Received '" + message + "'");
            };
            channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
            });
        }
    }
    <dependencies>
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-rabbit</artifactId>
                <version>2.3.1</version>
            </dependency>
        </dependencies>

     

  • 相关阅读:
    最短路一类问题总结
    [Luogu] UVA1205 Color a Tree
    [Luogu] UVA1193 Radar Installation
    [Luogu] P2859 [USACO06FEB]Stall Reservations S
    [Luogu] P1248 加工生产调度
    [Luogu] P3694 邦邦的大合唱站队
    [Luogu] CF229D Towers
    windows server清理文件
    python连接ES进行数据过滤删除,新增查询、创建索引功能
    ES以更新时间为标准,分离数据
  • 原文地址:https://www.cnblogs.com/tszr/p/16419823.html
Copyright © 2020-2023  润新知