This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge


Related articles

RabbitMQ series: RabbitMQ series


preface

  • The main idea behind work queues (also known as task queues) is to avoid performing a resource-intensive task immediately and having to wait for it to complete.

  • Instead, we schedule tasks for later. We encapsulate the task as a message and send it to the queue.

  • A work process running in the background will pop up the job and eventually execute it.

  • When there are multiple worker threads, the worker threads work together on these tasks.

  • As a future senior programmer, let’s start by optimizing the simple queue pattern.

    • This is our producers and consumers, obviously, a lot of duplicated code.
  • To optimize the

    • We’re going to extract the queue name
    • Public static final String JIANDAN_MODEL = "dayu"; public static final String JIANDAN_MODEL = "dayu"; Public static final String WORK_MODEL = "WORK_MODEL "; }Copy the code
    • Extract connection factory, establish connection, establish channel
    • /** * public part create utility class *@author DingYongJun *@date 2021/8/1 */ public class RabbitMqUtils {// get a connected channel public static Channel getChannel() throws Exception {// create a ConnectionFactory. ConnectionFactory factory = new ConnectionFactory(); Factory. SetHost (" IP address "); factory.setUsername("admin"); factory.setPassword("111111"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); return channel; }}Copy the code
    • This is the general structure
  • Nice! Then use it however you want!

I. Producers

  • /** * this is a test producer *@author DingYongJun *@date 2021/8/1 */ public class DyProducerTest_02 {/** * here for convenience, @param args */ public static void main(String[] args) throws Exception{// Use the tool class to create the Channel channel = RabbitMqUtils.getChannel(); /** * generate a queue * 1. Queue name * 2. Whether messages in the queue are persistent * 3. Is the queue only for one consumer to consume is it shared true Multiple consumers can consume * 4. Indicates whether the queue is automatically deleted after the last consumer disconnect. True Indicates whether the queue is automatically deleted * 5. Other parameters * / channel. QueueDeclare (QueueNameConstant WORK_MODEL, false, false, false, null); /** * send a message * 1. Send to the switch * 2. For (int I =0; int I =0; int I =0; i<6; I++){String message=" I'm the producer and I've got good news for you!" +i; Thread.sleep( 1000 ); channel.basicPublish("",QueueNameConstant.WORK_MODEL,null,message.getBytes()); System.out.println(" Message sent "); }}}Copy the code

Ii. Consumers

  • To test the rotation distribution message, here we set up two consumers to consume the message.

  • Consumers A

  • /** * This is a test consumer *@author DingYongJun *@date 2021/8/1 */ public class DyConsumerTest_02 {public static void Main (String[] args) throws Exception{// Use the tool class to create a Channel. Channel = Rabbitmqutils.getChannel (); System.out.println(" This is consumer A, I'm waiting to receive A message!" ); DeliverCallback deliverCallback = (String var1, Delivery var2)->{ String message= new String(var2.getBody()); System.out.println(message); }; CancelCallback CancelCallback = (String var1)->{system.out.println (" message consumption was interrupted "); }; /** * Consumer message * 1. Which queue to consume * 2. Whether to automatically reply after the consumption is successful true means automatic reply false manual reply * 3. */ Thread. Sleep (1000); channel.basicConsume(QueueNameConstant.WORK_MODEL,true,deliverCallback,cancelCallback); }}Copy the code
  • Consumer B code consistent, no longer paste out to take up space!

  • Start both consumer A and consumer B

  • Start the producer and send six messages

  • Consumers A

  • Consumers B

  • The result is very obvious, you one, I one, everyone is very orderly one! There was no mayhem or robbery or anything. Ha, ha, ha

Third, summary

  • Polling distribution is the process of sending messages from a message queue to all consumers in turn. A message can only be accessed by one consumer.

  • The characteristics of

    • A message can only be received by one consumer.
    • The queue uses a polling method to send messages evenly to consumers.
    • The consumer does not receive the next message until he or she has finished processing one.
  • The production end

    • Statement of the queue
    • Create a connection
    • Create channels
    • Channel declaration queue
    • Set the message
  • The consumer end

    • Statement of the queue
    • Create a connection
    • Create channels
    • Channel declaration queue
    • Override the message consumption method
    • Execute message method

I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah