rabbitmq 入门教程一(只有队列,不涉及路由)

为何要学习rabbitmq

因为在spring cloud bus中使用到的是rabbitmq和kafka,所以就需要搞一下rabbitmq和kafka。先上图

rabbitmq 入门教程一(只有队列,不涉及路由)_第1张图片

 生产者(Producer)
    Producer(生产者),产生消息并向RabbitMq发送消息。就是clientA和clientB
消费者(consumer)
    Consumer(消费者),等待RabbitMq消息到来并处理消息。就是client1,client2,client3
消息队列Queue
    Queue(队列),依存于RabbitMQ内部,虽然消息通过RabbitMQ在你的应用中传递,但是它们只能存储在queue中。队列不受任
    何限制,可以存储任何数量的消息—本质上是一个无限制的缓存。很多producers可以通过同一个队列发送消息,相同的很
    多consumers可以从同一个队列上接收消息。
 这篇文章主要介绍的是最简单的是:

在这里插入图片描述

pom文件

<dependencies>
       <dependency>
           <groupId>com.rabbitmqgroupId>
           <artifactId>amqp-clientartifactId>
           <version>4.1.0version>
       dependency>
   dependencies>

Producer代码:

package com.bobo.rabbitmq1;

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

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeoutException;

/**
* 消息队列生产者,这个例子只是简单的通过queue使用,
* 所以是exchange是“”
* @author [email protected]
* @create 2018-11-04 13:36
**/
public class Send {

   public static void main(String[] args) throws IOException, TimeoutException {
       ConnectionFactory connectionFactory = new ConnectionFactory();
       connectionFactory.setHost("localhost");
       Connection connection = connectionFactory.newConnection();
       Channel channel = connection.createChannel();
       channel.queueDeclare("hello",false,false,false,null);
       String message = "hello world";
       for (int i =0 ;i <10;i++) {
           channel.basicPublish("","hello",null,message.getBytes(Charset.forName("UTF-8")));
       }
       System.out.println(" [x] Sent '" + message + "'");
       //关闭连接
       channel.close();
       connection.close();
   }
}

channel.queueDeclare这个方法就是声明了一个对列。

consumer代码

package com.bobo.rabbitmq1;

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;

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

/**
* 消息接收者
* @author [email protected]
* @create 2018-11-04 13:46
**/
public class Receiver {

   public static void main(String[] args) throws IOException, TimeoutException {
       ConnectionFactory connectionFactory = new ConnectionFactory();
       connectionFactory.setHost("localhost");
       Connection connection = connectionFactory.newConnection();
       Channel channel = connection.createChannel();
       channel.queueDeclare("hello", 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("hello", true, consumer);
   }
}

在这里,其实还是获取到queue,这里是使用了DefaultConsumer这个类,然后重写了handleDelivery方法,这个方法中的body就是
消息体,可以再这里对消息进行处理。

你可能感兴趣的:(rabbitmq,rabbitmq,queue)