: Windows RABBITMQ installation and Getting Started

Source: Internet
Author: User
Tags rabbitmq

Installing RABBITMQ under 1.Windows requires the following steps

(1): Download Erlang because the RABBITMQ server code is written using Erlang in the Concurrency language: http://www.erlang.org/downloads, Double-click the. exe file to install it, create an environment variable named Erlang_home after installation, its value points to the installation directory for ERLANG, add%erlang_home%\bin to path, and finally open the command line, enter Erl, If the version information of Erlang appears, the Erlang locale is installed successfully;

(2): Download rabbitmq,:http://www.rabbitmq.com/, also double-click the. exe to install it (note here that the default installation directory is C:/Program files/...., there are spaces in this directory, We need to change the installation directory, it seems that RABBITMQ installation directory is not allowed to have spaces, I have stepped on this big hole before;

(3): Install Rabbitmq-plugins, this equivalent is a management interface, convenient for us to view the browser interface RABBITMQ each message queue and exchange work, the installation method is: Open a command line CD into RABBITMQ sbin directory ( My directory is: E:\software\rabbitmq\rabbitmq_server-3.6.5\sbin), enter: Rabbitmq-plugins enable rabbitmq_management command, Wait a moment, you will find the plugins installation successful prompt, the default is to install 6 plug-ins, if you install the plug-in process, the following error occurred:

The workaround is: first enter at the command line: Rabbitmq-service stop, then enter Rabbitmq-service Remove, then enter Rabbitmq-service install, Then input rabbitmq-service start, and finally re-enter Rabbitmq-plugins enable Rabbitmq_management try, I was the solution;

(4): After the plug-in installation, in the browser input http://localhost:15672 to verify, you will see the following interface, enter the user name: Guest, Password: Guest You can enter the management interface, of course, the user name password you can change;

2. After installing RABBITMQ, let's start with a brief look at some of the concepts involved in RABBITMQ

Producer: Message producer

Consumer: Message Consumers

Virtual Host: In RABBITMQ, the user can only make some permission settings at the virtual host level, such as which queues I can access, which requests I can handle, etc.

Broker: The message redirector, which is the function that our RABBITMQ serves, then what is the rule for the message to be forwarded? The following concepts need to be used;

Exchange: Switch, he is and producer directly to deal with, a bit similar to the function of the router, mainly for the forwarding operation of the chant, then producer in the end with which exchange to route it? This depends on the routing key (route key), each message has this key, we can also set ourselves, is actually a string;

Queue: Message Queuing, used to hold messages, he receives messages routed by Exchange, we can persist the contents of the queue, so what does queue receive the message of that Exchange route? This is the time to use the binding key (binding keys), binding key will queue and exchange binding, as for the binding method, RABBITMQ provides a variety of ways, you can see the RABBITMQ blog series (click to see);

These are some of the concepts involved in RABBITMQ, and a graph that shows the relationship between these concepts is:

3.RabbitMQ Simple to use

Producer (producer) End step:

(1): Create ConnectionFactory, and set some parameters, such as Hostname,portnumber and so on

(2): Create a connection connection with ConnectionFactory

(3): Create a channel with connection

(4): Create a queue and bind to channel

(5): Create a message and send it to the queue

Note that in our current example, the exchange switch is not used, RABBITMQ will create an empty string name by default for Exchange, and if we do not create our own exchange, the default is to use this exchange;

Producer-side code:

[Java]View PlainCopy
  1. Public class Sender {
  2. Private final static String queue_name = "Myqueue";
  3. public static void Main (string[] args) {
  4. Send ();
  5. }
  6. public static void Send ()
  7. {
  8. ConnectionFactory factory = null;
  9. Connection Connection = null;
  10. Channel channel = null;
  11. try {
  12. Factory = new ConnectionFactory ();
  13. Factory.sethost ("localhost");
  14. Connection = Factory.newconnection ();
  15. Channel = Connection.createchannel ();
  16. Channel.queuedeclare (Queue_name, false, false, false, null);
  17. String message = "My first message ...";
  18. Channel.basicpublish ("", queue_name, null, Message.getbytes ("UTF-8"));
  19. SYSTEM.OUT.PRINTLN ("message already sent ..." +message);
  20. } catch (IOException e) {
  21. E.printstacktrace ();
  22. } catch (TimeoutException e) {
  23. E.printstacktrace ();
  24. }finally{
  25. try {
  26. //Close Resources
  27. Channel.close ();
  28. Connection.close ();
  29. } catch (IOException e) {
  30. E.printstacktrace ();
  31. } catch (TimeoutException e) {
  32. E.printstacktrace ();
  33. }
  34. }
  35. }
  36. }

Consumer (consumer) End steps:

(1): Create ConnectionFactory, and set some parameters, such as Hostname,portnumber and so on

(2): Create a connection connection with ConnectionFactory

(3): Create a channel with connection

(4): Bind the queue and channel, note that the queue name here is consistent with the queue created earlier producer

(5): Create consumer consumer to receive messages while binding consumers and queue

Consumer-side code:

[Java]View PlainCopy
  1. Public class Receiver {
  2. Private final static String queue_name = "Myqueue";
  3. public static void Main (string[] args) {
  4. Receive ();
  5. }
  6. public static void receive ()
  7. {
  8. ConnectionFactory factory = null;
  9. Connection Connection = null;
  10. Channel channel = null;
  11. try {
  12. Factory = new ConnectionFactory ();
  13. Factory.sethost ("localhost");
  14. Connection = Factory.newconnection ();
  15. Channel = Connection.createchannel ();
  16. Channel.queuedeclare (Queue_name, false, false, false, null);
  17. Consumer Consumer = new Defaultconsumer (channel) {
  18. @Override
  19. public void Handledelivery (String consumertag, Envelope Envelope, basicproperties properties,
  20. byte[] body) throws IOException {
  21. System.out.println ("11111111111");
  22. String message = new String (Body, "UTF-8");
  23. SYSTEM.OUT.PRINTLN ("message received ..." +message);
  24. }};
  25. Channel.basicconsume (queue_name, true,consumer);
  26. } catch (IOException e) {
  27. E.printstacktrace ();
  28. } catch (TimeoutException e) {
  29. E.printstacktrace ();
  30. }finally{
  31. try {
  32. //Close Resources
  33. Channel.close ();
  34. Connection.close ();
  35. } catch (IOException e) {
  36. E.printstacktrace ();
  37. } catch (TimeoutException e) {
  38. E.printstacktrace ();
  39. }
  40. }
  41. }
  42. }

Well, this first here, the next I will be a brief introduction of something deeper, follow-up will also be RABBITMQ Native API encapsulation, easy for us to develop;

: Windows RABBITMQ installation and Getting Started

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.