RabbitMQ Hello World

直接先贴代码

//Send.java
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

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

  public static void main(String[] argv) throws Exception {
      	      
    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 World!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
    
    channel.close();
    connection.close();
  }
}
     在main方法中,首先new了一个ConnectionFactory factory=new ConnectionFactory(),这个ConnectFactory是一个Connection"生产工厂",可以new出Connection出来。然后这个factory设置了一个host,由于在本地测试,所以选择了localhost作为host地址。然后new了一个Connection  Connection connection =factory.newConnection(),这是定义了一个RabbitMQ中的connection,你可以将其看成是一个TCP连接,其实质也就是一个TCP连接。在connection的基础上create了一个Channel。connection和Channel是RabbitMQ里重要概念之一。前面这4段代码是基础代码中的基础代码。

      RabbitMQ中有3个重要概念:producing生产者,可以理解为Sender,即Message发送者,consuming,消费者,即信息接收者Receiver。queue队列,可以理解为消息队列,是连接producing和consuming之间的连接器,还有一个概念是exchange,这个exchange可以连接为一个分发器或者说路由器router,exchange决定了Message发送到哪个queue里。

     channel queueDeclare(QUEUE_NAME, false,false,false,null),这里做了queue申明。这里什么的queue有一个String类似的名称QUEUE_NAME,这个queue需要在P端和C端都申明一下,同一个queue name的才能够进行Message的传递。

     chanel.basicPublish("",QUEUE_NAME,null message.getByte()) 这个代码就是将message内容发送到了队列queue里面了。

     完事之后滚比channel和connection。

//Recv.java
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;

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

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

    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");
    
    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 + "'");
    }
  }
}
      在Consuming端接收Message,同样的创建了ConnectionFactory Connection Channel ,申明declare了queue。

      然后new了一个QueueingConsumer consumer=new QueueingConsumer(channel)

      QueueingConsumer是一个buffer,里面存储了Message

      在一个死循环中一直接收Message,在while(true)中定义了QueueConsumer.Delivery delivery=consumer.nextDelivery();然后在delivery中取出mesage内容body:delivery.getBody()。

      在while循环中QueueingConsumer.nextDelivery()会一直阻塞,一直等到下一个message被delivery过来。



版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/mooreliu/p/4849969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值