消息队列RabbitMQ详解

转载至简书:感谢,作者 预流; 原文请戳 这里

RabbitMQ 安装

一般来说安装 RabbitMQ 之前要安装 Erlang ,可以去Erlang官网下载。接着去RabbitMQ官网下载安装包,之后解压缩即可。根据操作系统不同官网提供了相应的安装说明:Windows、Debian / Ubuntu、RPM-based Linux、Mac

Java 客户端访问

RabbitMQ 支持多种语言访问,以 Java 为例看下一般使用 RabbitMQ 的步骤。

  1. maven工程的pom文件中添加依赖
<dependency>
    <groupId>com.rabbitmqgroupId>
    <artifactId>amqp-clientartifactId>
    <version>4.1.0version>
dependency>
  1. 消息生产者
package com.example.will.RabbitMQ;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args)throws IOException, TimeoutException{
        ConnectionFactory factory=new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setHost("localhost");
        Connection conn=factory.newConnection();
        Channel channel=conn.createChannel();
        String exchangeName="hello-exchange";
        channel.exchangeDeclare(exchangeName,"direct",true);
        String routingKey="will02";
        //发布消息
        byte[] messageBodyBytes="quit".getBytes();
        channel.basicPublish(exchangeName,routingKey,null,messageBodyBytes);
        channel.close();
        conn.close();
    }
}
  1. 消费者端代码:
package com.example.will.RabbitMQ;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.ConnectException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args)throws IOException, TimeoutException{
        ConnectionFactory factory=new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setHost("localhost");
        //建立服务器连接
        Connection conn=factory.newConnection();
        //获得信道
        final Channel channel=conn.createChannel();
        //声明交换器
        String exchangeName="hello-exchange";
        channel.exchangeDeclare(exchangeName,"direct" ,true);
        String queueName=channel.queueDeclare().getQueue();
        String routingKey="will02";
        channel.queueBind(queueName,exchangeName,routingKey);
        while (true){
            //消费信息
            boolean autoAck=false;
            String consumerTag="";
            channel.basicConsume(queueName,autoAck,consumerTag,new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String routingKey=envelope.getRoutingKey();
                    String contentType=properties.getContentType();
                    System.out.println("消费的路由键:"+routingKey);
                    System.out.println("消费的内容类型:"+contentType);
                    long deliverruTag=envelope.getDeliveryTag();
                    //确认消息
                    channel.basicAck(deliverruTag,false);
                    System.out.println("消费的消息体内容:");
                    String bodyStr=new String(body,"UTF-8");
                    System.out.println(bodyStr);
                }
            });
        }
    }
}
  1. 启动RabbitMQ服务器
>rabbitmq-server
  1. 运行 Consumer
    先运行 Consumer ,这样当生产者发送消息的时候能在消费者后端看到消息记录。
  2. 运行 Producer
    接着运行 Producer ,发布一条消息,在 Consumer 的控制台能看到接收的消息:
    消息队列RabbitMQ详解_第1张图片

RabbitMQ集群请看原文:https://www.jianshu.com/p/79ca08116d57

你可能感兴趣的:(#,java学习笔记)