1: Required Jar
: http://repo1.maven.org/maven2/com/rabbitmq/amqp-client/3.0.4/
MAVEN configuration:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.0.4</version>
</dependency>
Note: The version of the package is different, some classes cannot be found,
Listed as: import Com.rabbitmq.client.QueueingConsumer;-----in version 5.0.0, in version 3.0.4, other versions need to be tested by themselves
2: Create a Java project and directly introduce the following code test
========================= Test class Start ===========================
Package RABBITMQ;
Import java.io.IOException;
Import java.util.concurrent.TimeoutException;
Import Org.junit.Test;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;
Import com.rabbitmq.client.ConsumerCancelledException;
Import Com.rabbitmq.client.QueueingConsumer;
Import com.rabbitmq.client.ShutdownSignalException;
public class Testmq {
Public final static String queue_name= "Rabbitmq_test";
/**
* Establish a connection, create a message queue, and send a message to the message queue
*/
@Test
public void Linksend () {
try {
Create a connection factory
ConnectionFactory factory = new ConnectionFactory ();
Setting RABBITMQ Related Information
Factory.sethost ("192.168.146.134");
Factory.setusername ("admin");
Factory.setpassword ("123");
Factory.setport (5672);
To create a new connection
Connection Connection = Factory.newconnection ();
Create a Channel
Channel channel = Connection.createchannel ();
Declaring a queue
Channel.queuedeclare (Queue_name, False, False, false, NULL);
Send a message to the queue
String message = "Hello RabbitMQ";
Channel.basicpublish ("", queue_name, NULL, Message.getbytes ("UTF-8"));
System.out.println ("Producer Send +" + message + "'");
Close channels and connections
Channel.close ();
Connection.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* Establish a connection, create a message queue, and receive a message in a message queue
*/
@Test
public void Testreceive () {
try {
Create a connection factory
ConnectionFactory factory = new ConnectionFactory ();
Setting RABBITMQ Related Information
Factory.sethost ("192.168.146.134");
Factory.setusername ("admin");
Factory.setpassword ("123");
Factory.setport (5672);
To create a new connection
Connection Connection = Factory.newconnection ();
Create a Channel
Channel channel = Connection.createchannel ();
Declaring the queue to be connected
Channel.queuedeclare (Queue_name, False, False, false, NULL);
System.out.println ("Wait for message to produce:");
Create a consumer object for reading messages
Queueingconsumer consumer = new Queueingconsumer (channel);
Channel.basicconsume (Queue_name, true, consumer);
Gets the message in the message queue that is printed in the console
Queueingconsumer.delivery Delivery = Consumer.nextdelivery ();
String message = new String (Delivery.getbody ());
SYSTEM.OUT.PRINTLN ("Receive Message ==============" + message);
Close channels and connections
Channel.close ();
Connection.close ();
} catch (Shutdownsignalexception | Consumercancelledexception
| IOException | Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
========================= Test class End ===========================
Java Call RABBITMQ