RabbitMQ, rabbitmqandroid

Source: Internet
Author: User

RabbitMQ, rabbitmqandroid

RabbitMQ is a robust, easy-to-use, open-source message broker that supports multiple operating systems and languages.


Of course, the premise is that the machine is running rabbitmq-server.

 

Click the image below to download it:

 

What is the relationship between rabbitMQ and AMQP? What is rabbitMQ responsible?
That is the one between provider and consumer.

 

Message broker, such as ActiveMQ and RabbitMQ, can simply send and receive messages.

 

I followed the official Tutorial to write a simple Hello World.
Implement a small program from producer to queue to consumer.

 

Do not forget to add the java client:

<dependency>    <groupId>com.rabbitmq</groupId>    <artifactId>amqp-client</artifactId>    <version>3.3.4</version></dependency>

 

Provider:

Private final static String QUEUE_NAME = "hello"; public static void main (String [] args) throws IOException {// create Connection ConnectionFactory factory = new ConnectionFactory (); factory. setHost ("localhost"); Connection connection Connection = factory. newConnection (); Channel channel = connection. createChannel (); // defines the target queue channel. queueDeclare (QUEUE_NAME, false, null); String message = "Hello Worl D! "; System. out. println ("[x] Sent '" + message + "'"); channel. basicPublish ("", QUEUE_NAME, null, message. getBytes (); channel. close (); connection. close ();}

 

The process of the consumer er is roughly the same as that of the provider. You only need to note that the QUEUE_NAME must be the same:

private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception {     ConnectionFactory factory = new ConnectionFactory();    factory.setHost("localhost");    Connection connection = factory.newConnection();    Channel channel = connection.createChannel();     channel.queueDeclare(QUEUE_NAME, false, false, false, null);     QueueingConsumer consumer = new QueueingConsumer(channel);    channel.basicConsume(QUEUE_NAME, true, consumer);     while (true) {        QueueingConsumer.Delivery delivery = consumer.nextDelivery();        String message = new String(delivery.getBody());        System.out.println(" [x] Received '" + message + "'" + new Date());    }}

 

If nothing happens, consumer can print "Hello World! ".
(PS: If the disk is too full, messages may not be received. You can set the disk_free_limit item in the configuration file .)

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.