bitsCN.com
mysql模拟队列
Java代码
-- 初始化数据
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)));
-- 获取1条未处理的消息
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;
-- 此时@msgID如果为-1,代表没有待处理的消息,否则就代表本次需要处理的msg_id
bitsCN.com
本文原创发布php中文网,转载请注明出处,感谢您的尊重!
本文介绍了一种使用MySQL数据库模拟消息队列的方法,并通过Java代码实现与队列的数据交互。具体包括创建队列表、插入随机数据及通过更新语句获取并锁定未处理消息的过程。
4951

被折叠的 条评论
为什么被折叠?



