Getting started with rabbitmq helloworld

Source: Internet
Author: User

This series of tutorials mainly come from the translation of tutorials on the official website. Due to my limited level of English, the translation content is for your reference only.

"Hello World" of rabbitmq

1. Install rabbitmq in Windows

Download Erlang, address: http://www.erlang.org/download/otp_win32_R15B.exe, double-click to install (first installed)

Download rabbitmq, address: http://www.rabbitmq.com/releases/rabbitmq-server/v3.3.4/rabbitmq-server-3.3.4.exe, double-click to install

Download the rabbit-client.jar, which needs to be imported during Java code. Address: http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.3.4/rabbitmq-java-client-bin-3.3.4.zip

After the installation is complete, sbin in the installation directory of rabbitmq will first have: rabbitmq-server.bat

For example:


Under cmd: Enter the sbin directory and run rabbitmq-Server start


2. Introduction

Rabbitmq is the intermediary of information transmission. Essentially, he receives messages from producers and forwards them to consumers (consumers). In other words, he can forward, buffer, and persist messages according to your specified rules.

Some common terms of rabbitmq:
Producing means sending. A message sending program is a producer ). Generally, it indicates producer:


A queue is similar to a mailbox. Depends on the inside of rabbitmq. Although messages are transmitted in your application through rabbitmq, they can only be stored in queue. The queue is unrestricted and can store any number of messages-essentially an unlimited cache. Many producers can send messages through the same queue, while many of the same consumers can receive messages from the same queue. Queue:


Consuming is similar to receiving. Consumer is basically a program waiting to receive messages. Consumer:


Note: producers, consumers, and brokers do not need to be deployed on the same machine. In most practical applications, it will not be deployed on the same machine.


2. Java entry instance
One producer sends a message, and one receiver receives the message and prints it on the console. For example:


Note: You need to download rabbitmq-Java-client-bin-*. Zip from the official website and put the jar into the classpath of the project. Sender:Send. Java connects to rabbitmq (the service needs to be started at this time), sends a piece of data, and then exits.
Package COM. zhy. rabbit. _ 01; import COM. rabbitmq. client. channel; import COM. rabbitmq. client. connection; import COM. rabbitmq. client. connectionfactory; public class send {// queue name private final static string queue_name = "hello"; public static void main (string [] argv) throws Java. io. ioexception {/*** create a connection to connect to mabbitmq */connectionfactory factory = new connectionfactory (); // set the IP address or host name factory of the host where mabbitmq is located. sethost ("L Ocalhost "); // create a connection = factory. newconnection (); // create a channel = connection. createchannel (); // specify a queue channel. queuedeclare (queue_name, false, null); // sent message string message = "Hello world! "; // Send a message channel to the queue. basicpublish ("", queue_name, null, message. getbytes (); system. out. println ("[x] sent '" + message + "'"); // closes the channel and connection channel. close (); connection. close ();}}

It is worth noting that the queue will only be created when it does not exist. Multiple declarations will not be created again. The content of the information is a byte array, which means that you can transmit any data.

Acceptor:Recv. Java keeps waiting for the server to push messages and then output them on the console.

Package COM. zhy. rabbit. _ 01; import COM. rabbitmq. client. channel; import COM. rabbitmq. client. connection; import COM. rabbitmq. client. connectionfactory; import COM. rabbitmq. client. queueingconsumer; public class Recv {// queue name private final static string queue_name = "hello"; public static void main (string [] argv) throws Java. io. ioexception, Java. lang. interruptedexception {// open the connection and create a channel, which is the same as the connectionfactory factor as the sender. Y = new connectionfactory (); factory. sethost ("localhost"); connection = factory. newconnection (); channel = connection. createchannel (); // declares a queue. It is mainly used to prevent message recipients from running this program first. A queue is created when the queue does not exist. Channel. queuedeclare (queue_name, false, null); system. out. println ("[*] waiting for messages. to exit press Ctrl + C "); // create the queue consumer queueingconsumer consumer = new queueingconsumer (Channel); // specify the consumption queue channel. basicconsume (queue_name, true, consumer); While (true) {// nextdelivery is a blocking method (internal implementation is actually the take method of blocking the queue) queueingconsumer. delivery delivery = consumer. nextdelivery (); string message = new string (delivery. getbody (); system. out. println ("[x] received'" + message + "'");}}}

It doesn't matter whether the order of sending. Java and Recv. Java is run separately. Before the rabbitmq service is enabled.

Running result:

[X] sent 'Hello world! '

----------------------------------------

[*] Waiting for messages. To exitpress Ctrl + c

[X] received 'Hello world! '


The next article describes how to use a queue to distribute time-consuming tasks to different workers (consumers ).





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.