Java Call RABBITMQ

Source: Internet
Author: User
Tags message queue rabbitmq

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

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.