Rabbitmq的helloworld

1、在linux机器上安装rabbitmq,并启动服务:
这里写图片描述
2、下载MQ 客户端jar包:amqp-client-3.5.4.jar
3、通过rabbitmq输出”Hello World!”

这里写图片描述

其中P代表生产者、C表示消费者、中间红色部分代表消息队列

4、生产者客户端的发送消息程序如下:

package com.mq;

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 Send {  
    private final static String QUEUE_NAME = "hello";  

    public static void main(String[] args) throws IOException {  
        try {
        ConnectionFactory factory = new ConnectionFactory();  
        factory.setHost("192.168.11.32");  
        Connection connection;

            connection = factory.newConnection();

        Channel channel = connection.createChannel();  

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);  
        String message = "Hello World!";  
        for(int i=0;i<10;i++){
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());  
        System.out.println(" [x] Sent '" + message + "'");  
        }

        channel.close();  
        connection.close(); 
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }  
}

运行给过如下:

 [x] Sent 'Hello World!'
 [x] Sent 'Hello World!'
 [x] Sent 'Hello World!'
 [x] Sent 'Hello World!'
 [x] Sent 'Hello World!'
 [x] Sent 'Hello World!'

这里写图片描述
5、消费者客户端接收消息程序如下:

package com.mq;


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

public class Reqv {  
  private final static String QUEUE_NAME = "hello";  

  public static void main(String[] argv) throws Exception {  

      ConnectionFactory factory = new ConnectionFactory();  
      factory.setHost("192.168.11.32");  
      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");  

      QueueingConsumer consumer = new QueueingConsumer(channel);  
      channel.basicConsume(QUEUE_NAME, true, consumer);  

      while (true) {  
          QueueingConsumer.Delivery delivery = consumer.nextDelivery();  
          String message = new String(delivery.getBody());  
          System.out.println(" [x] Received '" + message + "'");  
      }  
  }  
}  

运行结果如下:

 [*] Waiting for messages. To exit press CTRL+C
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'
 [x] Received 'Hello World!'

如果消费者出现“[x] Received ‘Hello World!’”说明已接收到此消息信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值