PHP uses MySQL to implement Message Queuing

Source: Internet
Author: User
Tags echo date message queue rand stmt

Message Queuing is commonly used in traffic peaks (seconds to kill scenes), asynchronous communications, and other places.

The overall structure is as follows:

Similar to the relationship between consumers and producers, the first time the producer in the message queue is not full, the production of products into the message queue, the consumer when the message queue is not empty, the product from the message queue to consume. The usual way to step out of the team is to keep polling and timed operations.

Here is an example of a takeaway delivery:

How good is a restaurant with a good business? A minute there are 500 people order, so that the shopkeeper certainly can't handle, so, the first time not to inform the user is enough to take orders, first put all the orders to save, only tell them are processing, but, there is a problem, there are some other restaurants specialized to do things (jealous), So check whether the order is legal.

The above scenario can be achieved: After the user orders, the background will create a random order_id to the order, and the initial status of the order (status) is pending (0); When the merchant views the order, change the status to 1, indicating that it is processing When the merchant confirms that the order is acceptable, it will change the status of the order to 2, indicating the successful receipt.

First create a order_list order table in the database with the following table structure:

Mysql> desc order_list;+----------+------------+------+-----+----------+-------+| Field    | Type       | Null | Key | Default  | Extra |+----------+------------+------+-----+----------+-------+| order_id | Int (one)    | NO   | PRI | NULL     |       | | mobile   | INT (8)     | NO   |     | 88888888 |       | | status   | tinyint (1) | YES  |     | 0        |       | +----------+------------+------+-----+----------+-------+3 rows in Set (0.05 sec)

There are two PHP programs, namely producer.php (user, producer); consumer.php (merchant, consumer)

Once a user orders, the order_list to add a piece of data, here to facilitate testing, and then execute the following PHP program, representing the user side (producer.php), every 2 seconds the next single;

<?php $pdo =new PDO ("Mysql:host=localhost;dbname=test", "root", "root"), $stmt = $pdo->prepare ("INSERT INTO Order_ List (Order_id,mobile,status) VALUES (?,?,?) "); while (1) {$order _id=rand (10000,99999), $mobile =rand (11111111,99999999), $stmt->execute (Array ($order _id, $mobile , 0)), Echo date ("Y-m-d h:i:s", Time ()). " An order was added with the order number {$order _id} and the phone number {$mobile}\n "; sleep (2);} ?>

After the user submits the order, the rest is handed over to the merchant, the Merchant (consumer.php) business is too busy, every 4 seconds to process an order:

<?php $pdo =new PDO ("Mysql:host=localhost;dbname=test", "root", "root"), $stmt = $pdo->prepare ("Update order_list Set status=? where status=? Limit 1 "), $stmt _select= $pdo->prepare (" Select order_id from Order_list where Status=1 ");//The order number being processed while (1) {$init =0 ;//The initial status$lock=1;//is marked as being processed $success=2;//successfully received//to ensure the consistency of the data, before processing the order, we must first lock an order, the status from 0 to 1, and then to process//processing completed, Then change the status from 1 to 2$stmt->execute (Array ($lock, $init));//Lock the order to be processed $stmt_select->execute (); $result = $stmt _ Select->fetch (PDO::FETCH_ASSOC);//query is processing the order number $order_id= $result [' order_id '];echo date ("y-m-d h:i:s"). " Ready to process the order with the order number {$order _id}\n ", Sleep (3),//processing 3 seconds $stmt->execute (Array ($success, $lock)), Echo date (" Y-m-d h:i:s "). Order Processing completed, the order number is {$order _id}\n "; sleep (1);//rest 1 seconds}?>

This makes it possible to use MySQL to implement a simple message queue that looks at how to implement Message Queuing using Redis, similar to this method.

PHP uses MySQL to implement Message Queuing

Related Article

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.