RabbitMQ consumer two ways to get messages (Poll,subscribe) parsing

Source: Internet
Author: User
Tags socket rabbitmq

RABBITMQ consumer creates a channel object by establishing a connection to the queue, obtains a message through the channel channels,
Consumer can proactively get messages from the queue in a declarative way with API polling poll, or passively consume messages from a queue by subscribing,
Recently read the Java-based client related source code, a simple analysis.
The programming model pseudo-code is as follows:
ConnectionFactory factory = new ConnectionFactory ();
Connection conn = Factory.newconnection ();
Channel Channel=conn.createchannel ();
Creating connection requires specifying the physical address and port of MQ, which is the socket TCP physical connection, and channel is a logical concept that supports the creation of multiple MQ channel on a TCP connection
The following are based on two modes of consumption on the channel.

1, Subscribe subscription method
Boolean autoack = false;
Channel.basicconsume (QueueName, Autoack, "Myconsumertag",
New Defaultconsumer (channel) {
@Override
public void Handledelivery (String consumertag,
Envelope Envelope,
Amqp. Basicproperties Properties,
Byte[] body)
Throws IOException
{
String Routingkey = Envelope.getroutingkey ();
String contentType = Properties.contenttype;
Long Deliverytag = Envelope.getdeliverytag ();
(Process the message ...)
Channel.basicack (Deliverytag, false);
}
});

The way to subscribe is actually to register consumer with the queue, send the registered consumer message to the queue server through RPC, RabbitMQ server after receiving the message, according to the message's content type to judge this is a subscription message,
This will automatically send the message through the socket (long connection) channel when the queue in MQ has a message.
See Methods in Channeln
public string Basicconsume (String queue, Boolean autoack, String Consumertag,
Boolean nolocal, Boolean exclusive, map<string, object> arguments,
Final Consumer Callback)
Throws IOException
{
......
RPC (Method)
New Basic.Consume.Builder ()
. Queue (Queue)
. Consumertag (Consumertag)
. nolocal (Nolocal)
. Noack (Autoack)
. Exclusive (Exclusive)
. Arguments (arguments)
. Build (),
k);

try {
return k.getreply ();
} catch (Shutdownsignalexception ex) {
Throw wrap (ex);
}
}

Consumer the process of receiving messages:
After the connection is created, the Mainloop background thread is started, the loop gets the packet (frame) from the socket (Framehandler), and the message is processed by the Channel.handleframe (frame frame).
public void Handleframe (frame frame) throws IOException {
Amqcommand command = _command;
if (Command.handleframe (frame)) {//protocol to the message assemble
_command = new Amqcommand (); Prepare for the next one
Handlecompleteinboundcommand (command);//handling of message consumption
}
}
Channeln.handlecompleteinboundcommand
---channeln.processasync
----Dispatcher.handledelivery
---queueingconsumer.handledelivery
---this._queue.add (new Delivery (envelope, properties, body));//messages are eventually put into the queue
Each consumer has a blockqueue that caches the messages retrieved from the socket.
Next, the consumer object can invoke the API to fetch the message sequentially from the client-side cache _queue, and consume it, see Queueingconsumer.nextdelivery ()

For this kind of long connection way, do not see the heartbeat function, in order to prevent long connection due to network and other causes of connection failure


2, Poll API mode
Channeln:
GetResponse basicget (String queue, Boolean autoack)
This is a simpler way to get messages from the MQ server side of the queue directly through RPC

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.