RabbitMQ实例教程:Hello RabbitMQ World之Java实现

RabbitMQ要实现Hello World,其实也很简单。只需一个服务器来发送消息,另外有个客户端接收消息即可。

整体的设计流程如下:

RabbitMQ实例教程:Hello RabbitMQ World之Java实现

消息生产者发送Hello到消息队列,消息消费者从队列中接收消息。

下载依赖Jar包

RabbitMQ要用Java实现发送消息,就必须使用Java客户端库。目前RabbizMQ的Java客户端库最新版为为 3.5.5 。可以从Maven仓库下载,也可以直接去官网下载

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

使用Java创建发送者

package com.favccxx.favrabbit;
 
import java.io.IOException;
import java.util.concurrent.TimeoutException;
 
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
 
public class Sender {
 
    private final static String QUEUE_NAME = "hello";
 
    public static void main(String[] argv) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
 
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello RabbitMQ World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
 
    }
}

RabbitMQ控制台监控消息队列

运行上面的代码,从RabbitMQ控制台就可以看到刚刚发送的消息。

RabbitMQ实例教程:Hello RabbitMQ World之Java实现RabbitMQ实例教程:Hello RabbitMQ World之Java实现

使用Java接收消息

package com.favccxx.favrabbit;
 
import java.io.IOException;
import java.util.concurrent.TimeoutException;
 
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 Receiver {
 
    private final static String QUEUE_NAME = "hello";
 
    public static void main(String[] argv)
            throws java.io.IOException, java.lang.InterruptedException, TimeoutException {
 
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        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);
    }
}

分别运行消息发送者和消息接收者,从RabbitMQ控制台可以看到实时的状况。

RabbitMQ实例教程:Hello RabbitMQ World之Java实现

到此为止,RabbitMQ的Hello World工作就结束了,是不是对消息队列有了一些好感了呢?

RabbitMQ 的详细介绍:请点这里
RabbitMQ 的下载地址:请点这里

相关推荐