Mysql simulated queue bitsCN.com
Mysql simulated queue
Java code
-- Initialize 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.
BitsCN.com