Introduction to rabbitmq-channel method

First introduce several methods of rabbitmq:

     // Declare a queue - // queue queue name
         // when durable is true, the queue will not disappear when the server restarts (whether it is persistent)
         // exclusive whether the queue is exclusive, if it is true, it can only be used by one connection, and other connections are established An exception will be thrown 
         when // autoDelete When there is no consumer use, the queue is automatically deleted 
        channel.queueDeclare(QUEUE_NAME, false , false , false , null );
     /* 
         * Publish a message to the server
         * Parameter 1: exchange name, if it is empty, the default exchange is used
         * Parameter 2: routing key
         * Parameter 3: other attributes
         * Parameter 4: message body
         * RabbitMQ has an exchange by default, called default exchange, which is represented by an empty string, which is a direct exchange type,
         * Any message sent to this exchange will be routed to the queue corresponding to the name of the routing key. If there is no corresponding queue, the message will be discarded
         */  
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        // The server will only send one message to the consumer at the same time (capable people work more mode)   
        channel.basicQos( 1 );
     /* message consumption complete confirmation
         * autoAck whether to automatically confirm true automatic confirmation false manual confirmation
         * Mode 1: Automatic confirmation As long as the message is obtained from the queue, regardless of whether the message is successful after the consumer obtains the message, it is considered that the message has been successfully consumed.
         * Mode 2: Manual confirmation
         * After the consumer gets the message from the queue, the server will mark the message as unavailable and wait for the consumer's feedback. If the consumer has no feedback, the message will remain unavailable.
         * If automatic confirmation is selected, the message may be lost when the consumer takes the message and executes the process of downtime! !
         */
        channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

When manually confirming, be sure to confirm the submission after the message processing is completed, and add the following code:

    // The message processing is completed, and the submission is confirmed manually
        // deliveryTag The index of the message
        // multiple: whether the batch is true: all messages smaller than deliveryTag will be acked at one time. 
       channel.basicAck(envelope.getDeliveryTag(), false );
     /*
         * Swap created with fanout type
         * exchange: the name of the exchange
         * type: switch type (direct/topic/fanout)
         */
        channel.exchangeDeclare(EXCHANGE_NAME, ConfigKey.EX_FANOUT);
    /*
       * Get a temporary queue name.
       * channel.queueDeclare(): Create a non-persistent, independent, auto-delete queue name
       * This queue is temporary and random, once we disconnect the consumer, the queue will be deleted immediately
       * Random queue name, such as amq.gen-jzty20brgko-hjmujj0wlg
       */
       String queueName = channel.queueDeclare().getQueue();
        /*
         * Bind the queue to the exchange
         * queue: queue name
         * exchange: the name of the exchange
         * routingKey: the key value that the queue is bound to the switch
         */
        channel.queueBind(queueName, EXCHANGE_NAME, "black");

 

Guess you like

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