The following transfers are from: http://blog.csdn.net/yangbutao/article/details/10395599
RABBITMQ in consumer by establishing a connection to the queue, creating a channel object, acquiring a message through the channel channels, consumer can proactively retrieve messages from the queue in a declarative manner, in the form of API polling poll. You can also subscribe to passively from the queue to consume messages, 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 the channel is a logical concept that supports the creation of multiple MQ channel on a TCP connection the following are two consumption modes based on channel.
1, Subscribe subscription mode 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 via RPC, RabbitMQ server, after receiving the message, according to the content type of the message, this is a subscription message, so that when the MQ When a queue has a message, it automatically sends the message through the socket (long connection) channel. See methods in Channeln public string Basicconsume (String queue, Boolean autoack, String Consumertag, & nbsp; 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 a message: After the connection is created, the Mainloop background thread is started, the loop gets the packet (frame) from the socket (Framehandler), and the call to Channel.handleframe (frame frame) processing message, 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)//For message consumption processing } } Channeln.handlecompleteinboundcommand ---channeln.processasync ----Dispatcher.handledelivery ---queueingconsumer.handledelivery ---this._queue.add (new Delivery (envelope, properties, body));//The message is eventually placed in the queue each consumer has a blockqueue, Used to cache messages obtained 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
RabbitMQ consumer two ways to get messages (Poll,subscribe) parsing