1. First I downloaded the rabbitmq-server from the official
Http://www.rabbitmq.com/download.html
Of course, the company is still based on Java development, so the download version is based on Linux below, but I am here to download an Ubuntu version, because I developed the machine is Ubuntu.
Download well, install soon, Ubuntu under direct dpkg-i Rabbitmq-server_3.5.0-1_all.deb
Of course other Linux systems can download tar.gz package decompression, but also need to install Erlang. You can start RABBITMQ by executing the following command after unpacking.
Unzip the directory sbin found Rabbitmq-server run can, of course, there is no need to elaborate installation, you can refer to the official documentation for installation.
Http://www.rabbitmq.com/install-generic-unix.html
Of course, in order for us to visualize the operation of Rabbitmq-server and meet other development needs we can enable plugins
Rabbitmq-plugins enables, so we can open http://127.0.0.1:15672/
You can see the HTTP admin interface after logging in with Guest,guest.
2. Understand how RABBITMQ works
The first step is to install the service, then we will have to produce news and consumer news.
But to develop must understand how the RABBITMQ works. Of course, if I write a lengthy note to record dozens of pages, so if English is better, you can first look at the official documents.
Http://www.rabbitmq.com/getstarted.html
Of course, if the English is really slag, may wish to refer to the following links:
http://rabbitmq-into-chinese.readthedocs.org/zh_CN/latest/
After reading these documents, it is estimated that the RABBITMQ understand the same.
Of course, there are a lot of people on the Internet to write blog, can be referenced.
3.Java Client Development
Of course, according to the above documents we can open up the client we need, but here I mainly describe how to use SPRING-AMQP to develop.
First we need to introduce the SPRING-AMQP package, in order to see the debug information, we also introduce log4j
<dependency> <groupId>org.springframework.amqp</groupId> <artifactid>spring-rabbit</ Artifactid> <version>${spring-rabbit.version}</version></dependency><dependency> < Groupid>log4j</groupid> <artifactId>log4j</artifactId> <version>1.2.17</version ></dependency>
After the introduction of the development, of course, SPRING-AMQP How to configure here do not want to lengthy, so here only two ways to send
Sending messages asynchronously
In fact this way is very easy, most of the online articles are based on the configuration file, but if we use code to create, then more understanding of SPRING-AMQP design principles, can use code creation will be more able to use the configuration file
cachingconnectionfactory factory=new cachingconnectionfactory ();
Creating a link factory, using a factory with caching capabilities, can create multiple channel based on channel to handle
Set Rabbitmq-server link Address
Factory.setaddresses ("localhost:5672");
Set login username to create user on Rabbit-server http page
Factory.setusername ("Guest");
Set the login password (can not be set on this computer)
Factory.setpassword ("Guest");
Number of channel launches and cache
Factory.setchannelcachesize (channelsize);
//Create admin for queue binding, exchange bindings
Rabbitadmin admin=new rabbitadmin (Factory);
Create a queue named Qingting-queue, whether durable is a persistent message to the server
Queue queue=new Queue ("Qingting-queue", durable);
A queue is created on the Rabbitmq-servers
Admin.declarequeue (queue);
Create Exchange and subscribe to the service side, where you create the topic type of exchange, which is the route key can be a regular expression
Exchange exchange=new topicexchange ("Qingting-exchange", durable,autodelete);
An exchange with a name of Exchangename is created on the Rabbitmq-server, and the type is topic, and the feature has a D representation durable
Admin.declareexchange (Exchange);
/**
* Using Routekey to bind queues to exchange, this time, queue and exchange on Rabbitmq-server have a mapping relationship through Routekey
*/
admin.declarebinding (bindingbuilder.Bind(queue). to (Exchange). With ("qingting.*"). Noargs ());
Create a template to send messages, or to receive messages and other actions
Template = new Rabbittemplate (factory);
Here we talk about sending messages, we send messages through exchange, and we set up Routekey, because it is topic type of exchange, through
The Routekey of the set is matched with the above bound bindroutekey, and if matched, the message is pushed to the queuename queue.
Template.setexchange ("Qingting-exchange");
Template.setroutingkey ("Qingting.route.key");
Set to convert the message to a JSON type, and of course you can set up other converters or implement them yourself
Template.setmessageconverter (New Jackson2jsonmessageconverter ());
With the above configuration, we can send a sync message.
Template.convertandsend ();
Because the message converter is set up above, it is best to use the method of modification, and of course the Template.send () method actually uses it to jackson2jsonmessageconverter the message content body into JSON and convert it to a byte array.
The
Also sets the message's property configuration in Conenttype to Application/json and sets some information such as the object type of the message content in the head, because the consumer receives the message, You can also set the JSON converter to convert the contents of the message body into objects directly from the value of the head in the received message property, and you can see the message sent on the RABBITMQ administration page with the following information
Exchange:qingting-exchange//exchangerouting Key:qingting.route.key//template settings for the above template settings Routekeyp roperties headers: __keytypeid__:java.lang.object//Go to JSON with information for easy conversion __typeid__:java.util.hashmap __contenttypeid__:java.lang.object content_encoding:utf-8 content_type:application/json Payload {"Userna Me ":" 81a40c08-5a01-46fa-93c3-981fcccb3c8c "," Index ": 0}
So you can see that the bound queue on the HTTP page will have a message to the bound queue, because there is no consumer subscription queue at this time, we set the persistent durable, so we will stay in the server
Rabbitmq of contact