首頁 > 軟體

RabbitMQ範例教學:Hello RabbitMQ World之Java實現

2020-06-16 17:51:31

  RabbitMQ要實現Hello World,其實也很簡單。只需一個伺服器來傳送訊息,另外有個用戶端接收訊息即可。

  整體的設計流程如下:

  訊息生產者傳送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控制台就可以看到剛剛傳送的訊息。

  使用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 World工作就結束了,是不是對訊息佇列有了一些好感了呢?

CentOS 5.6 安裝RabbitMQ http://www.linuxidc.com/Linux/2013-02/79508.htm

RabbitMQ用戶端C++安裝詳細記錄 http://www.linuxidc.com/Linux/2012-02/53521.htm

Python嘗試RabbitMQ http://www.linuxidc.com/Linux/2011-12/50653.htm

RabbitMQ叢集環境生產範例部署 http://www.linuxidc.com/Linux/2012-10/72720.htm

Ubuntu下PHP + RabbitMQ使用 http://www.linuxidc.com/Linux/2010-07/27309.htm

在CentOS上安裝RabbitMQ流程 http://www.linuxidc.com/Linux/2011-12/49610.htm

RabbitMQ概念及環境搭建 http://www.linuxidc.com/Linux/2014-12/110449.htm

RabbitMQ入門教學  http://www.linuxidc.com/Linux/2015-02/113983.htm


IT145.com E-mail:sddin#qq.com