RABBITMQ Installation and Testing tutorials
Installing on MAC
I. Installation
1 2 3 4 5 6 7 8 9
|
Brew Install RABBITMQ
# # Enter the installation directory cd/usr/local/cellar/rabbitmq/3.7.5
# start Brew Services Start RABBITMQ # Current Window starts Rabbitmq-server
|
You need to turn on the plugin before starting the console
1
|
Enable Rabbitmq_management
|
Enter the console: http://localhost:15672/
User name and password:guest,guest
II. configuration and Testing 1. Add account
First, you have to start MQ.
1 2 3 4 5 6
|
# # Add Account ./rabbitmqctl Add_user Admin Admin # # Add access rights ".*" # # Set Super Permissions ./rabbitmqctl Set_user_tags Admin Administrator
|
2. Coded measurements
Pom introduces dependency
1 2 3 4
|
<dependency> <groupid>com.rabbitmq</groupid> <artifactid>amqp-client</artifactid> </dependency>
|
Start writing code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
PublicClassrabbitmqtest {
Message Queue name PrivateFinalstatic String queue_name ="Hello";
@Test PublicvoidSend()Throws Java.io.IOException, TimeoutException {
Create a connection Project ConnectionFactory factory =New ConnectionFactory (); Factory.sethost ("127.0.0.1"); Factory.setport (5672); Factory.setusername ("Admin"); Factory.setpassword ("Admin"); Create a connection Connection Connection = Factory.newconnection ();
Create a message Channel Channel channel = Connection.createchannel ();
Generate a message queue Channel.queuedeclare (Queue_name,TrueFalseFalseNULL);
for (int i =0; I <10; i++) { String message ="Hello World RabbitMQ Count:" + i;
A message is published, the first parameter indicates the route (Exchange name), and not "" indicates the use of the default message route Channel.basicpublish ("", Queue_name,NULL, Message.getbytes ());
System.out.println ("[x] Sent '" + Message +"‘"); }
Close message channels and connections Channel.close (); Connection.close ();
}
@Test PublicvoidConsumer()Throws Java.io.IOException, Java.lang.InterruptedException, timeoutexception {
Create a connection factory ConnectionFactory factory =New ConnectionFactory (); Factory.sethost ("127.0.0.1"); Factory.setport (5672); Factory.setusername ("Admin"); Factory.setpassword ("Admin");
Create a connection Connection Connection = Factory.newconnection ();
Creating a Message Channel Channel channel = Connection.createchannel ();
Message Queuing Channel.queuedeclare (Queue_name,TrueFalseFalseNULL); System.out.println ("[*] waiting for message. To exist press CTRL + C ");
Atomicinteger count =New Atomicinteger (0);
Information that consumers use to obtain message queue bindings for messages Consumer Consumer =New Defaultconsumer (channel) { @Override PublicvoidHandledelivery(String Consumertag, Envelope Envelope, AMQP.) Basicproperties Properties, Byte[] body)Throws IOException { String message =new string (body, try { System.out.println ( "[x] Received '" + message); } finally { System.out.println ( "[x] Done"); Channel.basicack (Envelope.getdeliverytag (), false); " Channel.basicconsume (queue_name, false, consumer); Thread.Sleep (1000 * 60); } /span> |
One thing to be aware of is:
- Production message:
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
- Consumer News:
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
- Both production and consumption declare channel, which requires the same configuration parameters, otherwise it cannot consume data
3. Output description
The plug-in data is executed first, and after execution is completed, it can be viewed from the console:
You can see that there is a queue, the column named Hello, a total of 10 data
The next step is to consume data, execute the consumer method, output the log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st
|
[*] WaitingFor message. To exist press CTRL + C [x] Received' Hello World RabbitMQ count:0 [x] Done [x] Received ' Hello World RabbitMQ count:1 [x] Done [x] Received' Hello World RabbitMQ count:2 [x] Done [x] Received ' Hello World RabbitMQ count:3 [x] do [x] Received ' Hello World RabbitMQ count:4 [x] do [x] Received ' Hello World RabbitMQ count:5 [x] done [x] Received ' Hello World RabbitMQ count:6 [x] do [x] Received ' Hello world RabbitMQ count:7 [x] do [x] Receive D ' Hello World RabbitMQ count:8 [x] done [x] Received ' Hello world RabbitMQ count:9 [x] done |
Looking back at the queue, we found that the data volume was 0.
4. Ack problem
For the ACK problem, if there is an exception when consuming data, and I do not want the data to be lost, this time it is necessary to consider the mechanism of manual ACK to ensure
You first need to set up a manual ack
1 2
|
Set Autoack to False False, consumer);
|
Second, after the consumption data is finished, the active Ack/nack
1 2 3 4 5
|
if (success) { FALSE); else { FALSE); }
|
III. other Grey-grey blog:https://liuyueyi.github.io/hexblog
A gray and gray personal blog, recording all the study and work in the blog, welcome everyone to visit
Statement
The letter is not as good as, the above content, purely opinion, because of limited personal ability, such as the discovery of bugs or better advice, always welcome criticism
- Weibo address: small Gray Gray Blog
- QQ: A grey/grey/3302797840
Scan attention
RABBITMQ Installation and testing tutorial in Mac Environment