Detailed explanation of common methods of rabbitmq channel interface




Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments) throws IOException;
  • 1
  • 2

explain:


Method role:
declare a queue

Parameter: queue
Meaning: Queue name

Parameter : durable
Meaning: whether to persist, if set to true, the queue still exists after the server restarts

Parameter : exclusive
meaning: whether it is an exclusive queue (exclusive queue), only the queue visible to itself, that is, no Allow access to other users

If the exclusive statement is true, the characteristics of the queue are:

1. Only visible to the connection that first declared it (Connection)
2. It will be automatically deleted when its connection is disconnected.

For details of this parameter, please go to: RabbitMQ: Exclusive Queue (Exclusive Queue)

Parameter: autoDelete
Meaning: When there is no consumer to use, the queue is automatically deleted

Parameters: arguments
meaning: other parameters

api explanation

write picture description here

void basicQos(int prefetchCount) throws IOException;
  • 1

explain:


Method effect:

how many messages to get at once
Parameters: prefetchCount
Meaning: will tell RabbitMQ not to push more than prefetchCount messages to a consumer at the same time
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
  • 1

explain:


Method effect:

Subscribe to messages and consume
Parameters: queue
Meaning: the subscribed queue
Parameters: autoAck
Meaning: Whether to enable automatic response, the default is enabled, if manual response is required, it should be set to false

Note: In order to ensure that the message must be processed by the consumer, rabbitMQ provides a message confirmation function,

that is, after the consumer has processed the task, it will give the server a feedback, and the server will delete the message.

If the consumer does not respond after a timeout, then the server Will resend the message to other consumers.

When autoAck is set to true, as long as the message is processed by the consumer, regardless of success, the server will delete the message.

When autoAck is set to false, only the message is processed. And it will be deleted after feedback results.
Parameters: callback
Meaning: The callback method to be executed after receiving the message
Take a look at the callback method source code:
 void handleDelivery(String consumerTag,
                        Envelope envelope,
                        AMQP.BasicProperties properties,
                        byte[] body)
        throws IOException;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Eh, I don't quite understand it, anyway, after receiving the news, you have to write it here!

I had an example of RPC before, you can refer to how this method is implemented there
http://blog.csdn.net/leisure_life/article/details/78657935
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
  • 1

explain:


Method effect:

post a message
Parameters: exchange
Meaning: Specify the forwarder name - ExchangeName, where an empty string means that the message will be handed over to the default Exchange
Parameters: routingKey
Meaning: which queue to publish to
Parameters: props
Meaning: other configuration parameters related to the message, routing headers, etc.
Parameters: body
Meaning: message body

Source code :
/**
     * Publish a message.
     *
     * Publishing to a non-existent exchange will result in a channel-level
     * protocol exception, which closes the channel.
     *
     * Invocations of <code>Channel#basicPublish</code> will eventually block if a
     * <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
     *
     * @see com.rabbitmq.client.AMQP.Basic.Publish
     * @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
     * @param exchange the exchange to publish the message to
     * @param routingKey the routing key
     * @param props other properties for the message - routing headers etc
     * @param body the message body
     * @throws java.io.IOException if an error is encountered
     */
    void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
 void basicAck(long deliveryTag, boolean multiple) throws IOException;
  • 1

explain:


Method effect:

In addition, it is necessary to manually send a response to the server after each message is processed.
Parameters: deliveryTag
Meaning: the number of the similar number of the current message, the number of the similar number generated by the server for each message
Parameters: multiple
Meaning: Whether to reply less than the current deliveryTag
Note : This should be used after the response mechanism is turned on,
 boolean ack = false ; //打开应答机制  
 channel.basicConsume(QUEUE_NAME, ack, consumer);  
  • 1
  • 2

When multiple is set to false, only the message corresponding to deliveryTag will be responded, and the server will delete the message after receiving the response.

Source code :

 /**
     * Acknowledge one or several received
     * messages. Supply the deliveryTag from the {@link com.rabbitmq.client.AMQP.Basic.GetOk}
     * or {@link com.rabbitmq.client.AMQP.Basic.Deliver} method
     * containing the received message being acknowledged.
     * @see com.rabbitmq.client.AMQP.Basic.Ack
     * @param deliveryTag the tag from the received {@link com.rabbitmq.client.AMQP.Basic.GetOk} or {@link com.rabbitmq.client.AMQP.Basic.Deliver}
     * @param multiple true to acknowledge all messages up to and
     * including the supplied delivery tag; false to acknowledge just
     * the supplied delivery tag.
     * @throws java.io.IOException if an error is encountered
     */
    void basicAck(long deliveryTag, boolean multiple) throws IOException;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324827142&siteId=291194637