rabbitMQ学习笔记(2):最简单的helloworld

上一篇文章提到了rabbitMQ的体系结构和一些核心概念,这篇文章就通过一个最简单的Java版helloWorld实例来看实际代码中这些概念的体现。


前期准备

1、在自己的电脑上安装rabbitMQ server
2、下载rabbitMQ 的java API,就是一个jar包,并在工程中导入

introduction

上一篇说到MQ解决的主要问题是降低系统之间、模块之间的耦合度,在这个层面上,可以将rabbitMQ理解为一个message broker,producer将message发送给rabbitMQ,rabbitMQ做为中间方将message传递给consumer,从而使producer和consumer之间解耦。message在两者之间传递的时候可以依据相应的规则进行路由,缓存甚至持久化
producer可以简单理解为发送消息的程序,在本文的图像中以下图代表producer:
       
   queue是模块间通信的“信箱”,其生命周期只存在于rabbitMQ server中,本质而言,queue就是一个无穷缓冲队列,一个或者多个producer/consumer都可以无限制的连接上mq,这里说的无限制是在数量上而言,连接MQ server还是要遵循rabbitMQ各种语言的API的规定。在本文的图像中以下图代表queue:

consumer就是连接在MQ上等待传送过来进行消费的:

note:producer consumer 和queue不一定存在于同一台机器上,并且在大多数情况下是不在同一台机器上的。对于大型系统而言,各个模块很有可能部署在不同机器上,模块和模块之间的通信远比同一台机器上的模块之间通信复杂,在实际生产环境中运用MQ也会有各种各样的问题与挑战。


传递一个最简单的HelloWorld

这部分我们不去深究rabbitMQ java api的细节,仅仅实现一个简单的模型


producer将一个message发送到queue中,queue将message传递给consumer供其消费。在本篇的程序中consumer仅仅将message打印出来。
直接上代码

producer的代码:


public class Sender {

	private final static String QUEUE_NAME = "hello";

	  public static void main(String[] argv) throws Exception {
	    //create a connection to a server 
//		  The connection abstracts the socket connection, and takes care of protocol version negotiation and 
//		  authentication and so on for us. Here we connect to a broker on the local machine - hence the locallhost. 
//		  If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.
		ConnectionFactory factory = new ConnectionFactory();
	    factory.setHost("localhost");
	    Connection connection = factory.newConnection();
//	    Next we create a channel, which is where most of the API for getting things done resides.
	    Channel channel = connection.createChannel();

//	    To send, we must declare a queue for us to send to; then we can publish a message to the queue:
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
	    String message = "Hello World!";
	    //publish the message to the queue
	    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
	    System.out.println(" [x] Sent '" + message + "'");

	    channel.close();
	    connection.close();
	  }

}

consumer的代码:



public class Receiver {
	
	private static final String QUEUE_NAME = "hello";
	private static final String HOST_ADDRESS = "localhost"; 
	public static void main(String[] args) throws IOException, TimeoutException {
		//Setting up is the same as the sender; 
//		we open a connection and a channel, and declare the queue from which we're going to consume. 
//		Note this matches up with the queue that send publishes to.
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(HOST_ADDRESS);
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
//		Note that we declare the queue here, as well. 
//		Because we might start the receiver before the sender, 
//		we want to make sure the queue exists before we try to consume messages from it.
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		System.out.println("[*] waiting for message .To exit press CTRL+C");
//		We're about to tell the server to deliver us the messages from the queue. 
//		Since it will push us messages asynchronously, we provide a callback in the form of an object that 
//		will buffer the messages until we're ready to use them. That is what a DefaultConsumer subclass does.
		Consumer consumer = new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, 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);
		
		
		
	}

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值