RABBITMQ Introduction (i)--basic concept

Source: Internet
Author: User
Tags rabbitmq

RABBITMQ have been installed for a while, but have not had time to study. Today formally began to enter the RABBITMQ study, through the blog post some of their learning experience recorded, for later review. Since you are also starting from scratch, so it does not involve a deep theoretical knowledge, after all, to learn how to use the first. About RABBITMQ is what, what is the use, everyone can own Google.


First, let's take a look at the simplest flowchart of RABBITMQ, which is one of the few components we usually use most.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/DF/wKioL1cLjfSiLxMhAADu0xTie_w054.png "title=" 1.png " alt= "Wkiol1cljfsilxmhaadu0xtie_w054.png"/>exchange: Exchanger. There are many switches in the RABBITMQ, and we can also create our own switches. When we send a message to RABBITMQ, we must specify the corresponding exchange, and the message will reach our specified exchange.

Queue: Queues. RABBITMQ is a message server, also known as Message Queuing. The messages we send to rabbitmq end up in a variety of queues, and stored in them (if the route cannot find the corresponding queue then the data is lost), waiting for the consumer to fetch.

Routing key: the route key. Used to establish a correspondence between queue and exchange. When the message arrives at Exchange, how does Exchange know which queue to put the message to? In fact, different types of exchange will match the matching rules of the binding on its body routing key, found to put this message in the routing key corresponding to the queue.

These three components are the most basic components we use later and are the RABBITMQ. A more detailed explanation will be found in the following article.



Let's take a look at a simple Producer-consumer example that RABBITMQ official gives us, and I've simply modified it, and through this example we can understand the basic usage of RABBITMQ.

package com.jaeger.helloworld;import java.io.ioexception;import  Java.util.concurrent.timeoutexception;import org.junit.test;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  helloworldtest {private static final string queue_name =  "Hello_queue"; private static final string host =  "172.19.64.21";p rivate static final  String USER =  "Jaeger";p rivate static final string password =   "root";p rivate static final int port = 5672; @Testpublic  void  Send ()  {connectionfactory connectionfactory = new connectionfactory (); connectiOnfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT);try {connection connection =  Connectionfactory.newconnection (); Channel channel = connection.createchannel (); Channel.queuedeclare (QUEUE_NAME, false,  False, false, null); string message =  "hello  world!"; Channel.basicpublish ("",  queue_name, null, message.getbytes ("Utf-8")); System.out.println ("sent "  + message +  "'"); Channel.close (); Connection.close ();}  catch  (ioexception e)  {e.printstacktrace ();}  catch  (timeoutexception e)  {e.printstacktrace ();}} @Testpublic  void receive ()  throws InterruptedException {ConnectionFactory  Connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT);try {connection connection =  Connectionfactory.newconnection (); Channel channel = connection.createchannel (); Channel.queuedeclare (QUEUE_NAME, false,  False, false, null); Consumer consumer = new defaultconsumer (channel)  {@Overridepublic  void  Handledelivery (STRING CONSUMERTAG, ENVELOPE ENVELOPE, AMQP. Basicproperties properties,byte[] body)  throws IOException {String message  = new string (body,  "UTF-8"); System.out.println ("received "  + message +  "'");}; Channel.basicconsume (Queue_name, true, consumer); Thread.Sleep (;  //a} catch ) (ioexception e)  {e.printstacktrace ();  catch  (timeoutexception e)  {e.printstacktrace ();}}

We first run the Send method, add a message to RABBITMQ, and then look at the next rabbitmq backstage, and you can see that the message has been added:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/E2/wKiom1cLmu3QxrOHAAAgNHQh5B8791.png "title=" 2.PNG " alt= "Wkiom1clmu3qxrohaaagnhqh5b8791.png"/>

We run the Receive method again, at a where we let the main thread sleep, let the Handledelivery this callback method have the opportunity to execute, or the main thread end directly, the callback method has no chance to execute. In the actual application of the general consumer is a single thread has been listening to the corresponding queue. Here's the result:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/E2/wKiom1cLnJjAVlBqAAAmNSuJaIY679.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1clnjjavlbqaaamnsujaiy679.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/E0/wKioL1cLnUyBx8NTAAAdsF4y_CQ425.png "style=" float: none; "title=" 4.PNG "alt=" Wkiol1clnuybx8ntaaadsf4y_cq425.png "/>

This example only gives us an idea of the basic usage of RABBITMQ, and there are many concepts that are not explained here, and I will explain them in the later articles. If you are anxious, you can first Google. 650) this.width=650; "src=" Http://img.baidu.com/hi/tsj/t_0007.gif "alt=" T_0007.gif "/>

This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1762756

RABBITMQ Introduction (i)--basic concept

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.