RabbitMQ Example Tutorial: Hello RabbitMQ World Java Implementation

Source: Internet
Author: User
Tags message queue rabbitmq



RABBITMQ to achieve Hello World is actually very simple. Only one server is required to send the message, and another client receives a message.






The overall design process is as follows:





The message producer sends a hello to the message queue, and the message consumer receives the message from the queue.






  Download dependent jar Packages






RABBITMQ The Java Client library is required to send messages using Java implementations. Currently, the latest version of the Java Client Library for RABBIZMQ is 3.5.5. Can be downloaded from the MAVEN repository or directly to the official website.


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





  Creating senders with Java





package com.favccxx.favrabbit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Sender {

	private final static String QUEUE_NAME = "hello";

	public static void main(String[] argv) throws IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("localhost");

		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		String message = "Hello RabbitMQ World!";
		channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
		System.out.println(" [x] Sent ‘" + message + "‘");

	}
}





  RABBITMQ Console Monitoring Message Queuing






Running the above code, you can see the message just sent from the RABBITMQ console.








Using Java to receive messages


package com.favccxx.favrabbit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Receiver {

	private final static String QUEUE_NAME = "hello";

	public static void main(String[] argv)
			throws java.io.IOException, java.lang.InterruptedException, TimeoutException {

		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("localhost");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

		Consumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
					byte[] body) throws IOException {
				String message = new String(body, "UTF-8");
				System.out.println(" [x] Received ‘" + message + "‘");
			}
		};
		channel.basicConsume(QUEUE_NAME, true, consumer);
	}
}




Run the message sender and the message receiver separately, and you can see the real-time situation from the RABBITMQ console.



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/2A/wKioL1YWY_Cz1LVDAAIKIpPqV5w568.jpg "title=" 4. RabbitMQ super.png "alt=" Wkiol1ywy_cz1lvdaaikippqv5w568.jpg "/>









So far, RABBITMQ's Hello World work is over, is there some goodwill in the message queue?












This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1701004



RabbitMQ Example Tutorial: Hello RabbitMQ World Java Implementation


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.