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.

  • 相关阅读:
    散列点斜率算法解剖
    c# 3d图像显示
    .net core 运行不需命令行
    c# 异步调用(异步模式,基于事件)
    分配在堆上还是分配在栈上及其区别
    c# 自定义按钮,渐变颜色(含中心向四周渐变,单方向渐变)
    玩转Web值jquery(一)---对表单中的某一标签批量处理(以input为例)
    设计模式 模版方法模式 展现程序员的一天
    【Android进阶】Android面试题目整理与讲解(二)
    【Android进阶】让程序运行效率更高的编程技巧总结
  • 原文地址:https://www.cnblogs.com/malcolmfeng/p/7872623.html
Copyright © 2011-2022 走看看