Mysql simulated queue

Source: Internet
Author: User

Mysql simulation queue Java code -- initialization data drop table if exists t_msg_queues; create table t_msg_queues (msg_id bigint unsigned not null AUTO_INCREMENT, msg_content VARCHAR (255) not null, owner_thread_id int not null default-1, primary key (msg_id) ENGINE = innodb default charset = utf8; SET @ maxRandom = POWER (10, 6 ); insert into 't_ msg_queues '('msg _ content') VALUES (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom); -- get an unprocessed message set session autocommit = 1; SET @ msgID =-1; UPDATE t_msg_queues SET owner_thread_id = GREATEST (CONNECTION_ID (), (@ msgID: = msg_id) * 0) WHERE owner_thread_id =-1 order by msg_id LIMIT 1; -- If @ msgID is-1, it indicates that there is no message to be processed. Otherwise, it indicates the msg_id to be processed.

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.