@Test public void test_delay_message() throws Exception { System.out.println("================================================================"); long totalMsgs = 10000; QUEUE_TOTAL = 32; // 构造消息体 MessageBody = StoreMessage.getBytes(); MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); // 每个物理映射文件 4K messageStoreConfig.setMapedFileSizeCommitLog(1024 * 32); messageStoreConfig.setMapedFileSizeConsumeQueue(1024 * 16); messageStoreConfig.setMaxHashSlotNum(100); messageStoreConfig.setMaxIndexNum(1000 * 10); MessageStore master = new DefaultMessageStore(messageStoreConfig, null); // 第一步,load已有数据 boolean load = master.load(); assertTrue(load); // 第二步,启动服务 master.start(); for (int i = 0; i < totalMsgs; i++) { MessageExtBrokerInner msg = buildMessage(); msg.setDelayTimeLevel(i % 4); PutMessageResult result = master.putMessage(msg); System.out.println(i + "\t" + result.getAppendMessageResult().getMsgId()); } System.out.println("write message over, wait time up"); Thread.sleep(1000 * 20); // 开始读文件 for (long i = 0; i < totalMsgs; i++) { try { GetMessageResult result = master.getMessage("GROUP_A", "TOPIC_A", 0, i, 1024 * 1024, null); if (result == null) { System.out.println("result == null " + i); } assertTrue(result != null); result.release(); System.out.println("read " + i + " OK"); } catch (Exception e) { e.printStackTrace(); } } Thread.sleep(1000 * 15); // 关闭存储服务 master.shutdown(); // 删除文件 master.destroy(); System.out.println("================================================================"); }
public MessageExtBrokerInner buildMessage() { MessageExtBrokerInner msg = new MessageExtBrokerInner(); msg.setTopic("AAA"); msg.setTags("TAG1"); msg.setKeys("Hello"); msg.setBody(MessageBody); msg.setKeys(String.valueOf(System.currentTimeMillis())); msg.setQueueId(Math.abs(QueueId.getAndIncrement()) % QUEUE_TOTAL); msg.setSysFlag(4); msg.setBornTimestamp(System.currentTimeMillis()); msg.setStoreHost(StoreHost); msg.setBornHost(BornHost); return msg; }
private MessageExtBrokerInner messageTimeup(MessageExt msgExt) { MessageExtBrokerInner msgInner = new MessageExtBrokerInner(); msgInner.setBody(msgExt.getBody()); msgInner.setFlag(msgExt.getFlag()); msgInner.setProperties(msgExt.getProperties()); TopicFilterType topicFilterType = MessageExt.parseTopicFilterType(msgInner.getSysFlag()); long tagsCodeValue = MessageExtBrokerInner.tagsString2tagsCode(topicFilterType, msgInner.getTags()); msgInner.setTagsCode(tagsCodeValue); msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgExt.getProperties())); msgInner.setSysFlag(msgExt.getSysFlag()); msgInner.setBornTimestamp(msgExt.getBornTimestamp()); msgInner.setBornHost(msgExt.getBornHost()); msgInner.setStoreHost(msgExt.getStoreHost()); msgInner.setReconsumeTimes(msgExt.getReconsumeTimes()); msgInner.setWaitStoreMsgOK(false); msgInner.clearProperty(Message.PROPERTY_DELAY_TIME_LEVEL); // 恢复Topic msgInner.setTopic(msgInner.getProperty(Message.PROPERTY_REAL_TOPIC)); // 恢复QueueId String queueIdStr = msgInner.getProperty(Message.PROPERTY_REAL_QUEUE_ID); int queueId = Integer.parseInt(queueIdStr); msgInner.setQueueId(queueId); return msgInner; }