RabbitMQ入门教程

1、下载安装RabbitMQ

  windows下

  先 下载Erlang 64位 其它去这里下载 http://www.erlang.org/downloads 

  然后 下载RabbitMQ  官网 http://www.rabbitmq.com/install-windows.html

  rabbitMq jar包  下载地址

  maven依赖

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

 安装完成后运行 rabbitMq安装目录下sbin/rabbitmq-server.bat  或者 在cmd下:进入sbin目录,运行rabbitmq-server start

 

2.Java入门实例

发送端:Producer.java 连接到RabbitMQ(此时服务需要启动),发送一条数据,然后退出。

package top.lilixin.maintest;

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

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

/**
 * @Project: rabbitMq
 * @Author: lilixin
 * @Date: 2016年8月17日
 * @Copyright: 2016 www.lilixin.top Inc. All rights reserved.
 */
public class Producer {
private static final String QUEUE_NAME = "hello world";
    
    public static void main(String[] args) throws IOException, TimeoutException {
         //创建到rabbitmq的连接
        ConnectionFactory factory = new ConnectionFactory();
        //设置rabbitmq所在主机ip或地址
        factory.setHost("localhost");
        //创建一个连接
        Connection conn = factory.newConnection();
        //创建一个频道
        Channel channel = conn.createChannel();
        //指定一个队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //发送的消息
        String msg = "this is my first rabbitmq msg";
        //往队列里发一条消息
        channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        System.out.println(" [x] Sent '" + msg + "'"); 
        //关闭频道和连接
        channel.close();
        conn.close();
    }
}

 

值得注意的是队列只会在它不存在的时候创建,多次声明并不会重复创建。信息的内容是字节数组,也就意味着你可以传递任何数据。 

接收端:Receiver.java 不断等待服务器推送消息,然后在控制台输出。

package top.lilixin.maintest;

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

import com.rabbitmq.client.AMQP.Queue.DeclareOk;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * @Project: rabbitMq
 * @Author: lilixin
 * @Date: 2016年8月17日
 * @Copyright: 2016 www.lilixin.top Inc. All rights reserved.
 */
public class Receiver {

    private static final String QUEUE_NAME = "hello world";

    public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
            ConsumerCancelledException, InterruptedException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        // 设置mq的host或者 ip
        factory.setHost("localhost");
        // 创建一个连接
        Connection conn = factory.newConnection();
        // 创建一个channel
        Channel channel = conn.createChannel();
        // 声明一个队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        // 创建队列消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 指定消费队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
        // 循环
        while (true) {
            // nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)
            Delivery delivery = consumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("[x] Received '" + msg + "'");
        }

    }
}

分别运行Send.java和Recv.java 顺序无所谓。前提RabbitMQ服务开启。

 

  

posted @ 2016-08-17 11:33  adwars  阅读(1299)  评论(0编辑  收藏  举报