• RabbitMQ 1-入门学习


    环境:

      软件环境MacOS ,Homebrew包管理工具

      IDE: Eclipse

      项目:Maven项目

    1.安装RabbitMQ Server:

      方式一:通过homebrew :终端执行:brew install rabbitmq   完成后,执行 brew services start rabbitmq 即可开启服务。

      方式二:通过手动下载安装。链接:http://www.rabbitmq.com/install-standalone-mac.html 下载安装文件并安装即可。

    2.搭建maven项目:

      依赖如下:

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

      创建Send.java 源码如下:

    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    public class Send {
    
      private final static String QUEUE_NAME = "hello";
    
      public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        System.out.println(" [x] Sent '" + message + "'");
    
        channel.close();
        connection.close();
      }
    }

      创建Recv.java 源码如下:

    import java.io.IOException;
    
    import com.rabbitmq.client.AMQP;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Consumer;
    import com.rabbitmq.client.DefaultConsumer;
    import com.rabbitmq.client.Envelope;
    
    public class Recv {
    
      private final static String QUEUE_NAME = "hello";
    
      public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
        Consumer consumer = new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
              throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
          }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
      }
    }

    3.运行项目:

      先run Recv.java ,再run Send.java.

  • 相关阅读:
    布隆过滤器
    springboot+redis实现分布式锁
    springboot+redis实现消息队列
    工作启示文章收藏
    redis常用命令
    前方的路
    分布式系统中对cookie和session的思考
    用Markdown来写自由书籍-开源技术的方案
    Centos 7.0添加yum本地安装源
    爹地,我找到了!15个极好的Linux find命令示例
  • 原文地址:https://www.cnblogs.com/malcolmfeng/p/7872623.html
Copyright © 2020-2023  润新知