zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    【Python爬虫】:爬取干货集中营上的全部美女妹子(翻页处理)
    python进阶之py文件内置属性
    Delphi
    Goland debug 报错:could not launch process: decoding dwarf section info at offset 0x0: too short
    Goland 生成可执行文件
    代理加速配置详解
    关掉所有代码折叠folding
    前端工程化概述(转发)
    Reactjs —— 前端框架切换
    TODO——多线程
  • 原文地址:https://www.cnblogs.com/malcolmfeng/p/7872623.html
Copyright © 2011-2022 走看看