Java implementation uses RABBITMQ to send and consume messages __java

Source: Internet
Author: User
Tags rabbitmq
1. Introduction

RABBITMQ is a message agent. Essentially, it receives messages from the producer and then delivers them to the consumer. During this time, it can be sent, cached, or persisted to store the messages according to the rules you make.

The terminology used by RABBITMQ.

1). Producing means more than just sending a message. The program that sends the message is called producer. We depict it as shown below.

2). Queue is the name of a message box. It survives in the RABBITMQ. Although messages flow through RABBITMQ and your applications, they can only be saved in Queue. The Queue does not have any bounds, you can save as many messages as you want, and it is essentially an infinite cache. Many producers can send messages to a queue, and many consumers can receive messages from a queue. We depict it as shown below.


3). The meaning of consuming is similar to receiving. A consumer mainly refers to the program that waits to receive messages. We depict it as shown below.


Note: Producers, consumers, and agents do not necessarily have to be on the same machine, and in most applications it is true.


2. "Hello World"

In this part of the use of the guide, we have to write two programs in Java, one is the producer, he sent a message, the other is the consumer, it receives the message, and print the message. Instead of ignoring the details of some Java APIs, we'll focus on what we're going to do, which is sending a "Hello world" message.

In the figure below, "P" represents the producer, and "C" stands for the consumer. In the middle is a Queue, a message buffer.


Java Client class Library

AMQP is an open source, universal messaging protocol. There are several AMQP clients written in different languages. We are going to use the Java version of the client provided by RABBITMQ.

Download the client (http://www.rabbitmq.com/java-client.html), unzip it to your working directory, and locate the jar file.

$ unzip Rabbitmq-java-client-bin-*.zip
$ cp rabbitmq-java-client-bin-*/*.jar./

This client is also available in Maven's central repository.

GroupId:com.rabbitmq

Artifactid:amqp-client

Now that we have the client and the dependencies, we start writing code. Sending

We call the sender of the message send, the message receiver called RECV. The sender of the message connects to RABBITMQ, sends a message, and then exits.

Create Send.java files and import the classes we need.

Import com.rabbitmq.client.ConnectionFactory;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.Channel;

Create a name for the Queue

public class Send {
  private final static String queue_name = "Hello";

  public static void Main (string[] argv)
      throws java.io.IOException {
      ...
  }}

Then we create a connection.

ConnectionFactory factory = new ConnectionFactory ();
Factory.sethost ("localhost");
Connection Connection = Factory.newconnection ();
Channel Channel = Connection.createchannel ();

Here, we have established a connection with the agent of this machine. If we want to connect to agents on different machines, we just need to develop the domain name or IP address of that machine.

Next, we create a pipeline. To send a message, we also need to declare a queue, and then we can post the message to the queue.

Channel.queuedeclare (Queue_name, False, False, false, NULL);
String message = "Hello world!";
Channel.basicpublish ("", queue_name, NULL, message.getbytes ());
System.out.println ("[X] Sent '" + Message + "'");

The process of a Queue declaration is idempotent and is created only if it does not exist. The content of the message is a byte array, which means that you can use any encoding you want to use.

Finally, we close this pipe and connect.

Here is our complete Send.java file.

Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;

Public class Send {
Private final static String queue_name = ' Hello ';
  public static void Main (string[] Ar GV) throws Exception {
        ConnectionFactory factory = new ConnectionFactory ();
  &NB Sp     Factory.sethost ("localhost");
        Connection Connection = Factory.newconnection ();
        Channel Channel = Connection.createchannel ();
        Channel.queuedeclare (Queue_name, False, False, false, NULL);
        String message = "Hello world!";
        Channel.basicpublish ("", queue_name, NULL, Message.getbytes ("UTF-8"));
        SYSTEM.OUT.PRINTLN ("[X] Sent '" + Message + "");
        channel.close ();
        connection.close ();
       }
}

receiving not like the sender of a message just sends a message, our receivers need to constantly listen to the messages and print them out.



Similar to the classes and send.java that you need to import in Recv.java this 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.