Search in sources :

Example 11 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class Recover method recoverMessage.

public ConcurrentHashMap<String, Message> recoverMessage(List<Offset> makedOffsets) {
    long startTime = System.currentTimeMillis();
    ConcurrentHashMap<String, Message> msgMap = new ConcurrentHashMap<>(1024 * 128);
    System.out.println("offsets:" + makedOffsets);
    int recoverNum = 0;
    int queueId;
    TopicAndFilter topicAndFilter;
    for (Offset offset : makedOffsets) {
        topicAndFilter = new TopicAndFilter(offset.getTopic(), offset.getFilter());
        queueId = offset.getQueueIndex();
        // //恢复topicAndgroup2subScribe
        // LinkedList<Offset> offsetList;
        // if(!topicAndgroup2subScribe.containsKey(topic+"@"+group)){
        // offsetList=new LinkedList<Offset>();
        // topicAndgroup2subScribe.put(topic+"@"+group, offsetList);
        // }else{
        // offsetList=topicAndgroup2subScribe.get(topic+"@"+group);
        // }
        // offsetList.add(Integer.parseInt(queueId),offset);
        // //恢复topicAndgroup2sendMsgQueue
        // LinkedList<BlockingQueue<String>> sendQueuesList;
        // if(!topicAndgroup2sendMsgQueue.containsKey(topic + "@" + group)){
        // sendQueuesList=new LinkedList<BlockingQueue<String>>();
        // topicAndgroup2sendMsgQueue.put(topic + "@" + group, sendQueuesList);
        // }else
        // {
        // sendQueuesList=topicAndgroup2sendMsgQueue.get(topic + "@" + group);
        // }
        // BlockingQueue<String>sendQueue=new LinkedBlockingQueue<String>();
        // sendQueuesList.add(Integer.parseInt(queueId),sendQueue);
        // 消息入内存
        int currentOffset = offset.getCurrentoffset();
        int MaxOffset = offset.getMaxOffset();
        Message msg;
        // currentOffset偏移量
        int i = 0;
        List<byte[]> blist = mstore.readByteNormal(topicAndFilter.toString(), String.valueOf(queueId), currentOffset + 1, MaxOffset);
        recoverNum += blist.size();
        System.out.println(blist.size());
        for (byte[] b : blist) {
            try {
                msg = (Message) fst.asObject(b);
                System.out.println("恢复消息" + msg);
            } catch (Exception e) {
                continue;
            }
            msgMap.put(new TopicAndFilter(msg.getTopic(), msg.getProperties()).toString(), msg);
            // msgid2offset.put(msg.getMsgId(), currentOffset+i);
            i++;
        }
    }
    long endTime = System.currentTimeMillis();
    System.out.println("恢复cost:" + (endTime - startTime) + " ,一共恢复" + recoverNum + "条");
    return msgMap;
}
Also used : TopicAndFilter(com.alibaba.middleware.race.mom.util.TopicAndFilter) Message(com.alibaba.middleware.race.mom.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class MsgFileUtil method getMsg.

/*
	 * 根据请求参数传回List<Message>
	 * 
	 * 
	 */
public List<Message> getMsg(String topic, String queueId, String groupId, Long offset, Long maxOffset) throws IOException {
    List<Message> resultMsg = new LinkedList<Message>();
    RandomAccessFile indexFile, dataFile;
    // 根据是否有queueId判定文件位置
    if (queueId == null || queueId.equals("") || queueId.isEmpty()) {
        indexFile = getFile(topic, null, queueId, "index.file");
        dataFile = getFile(topic, null, queueId, "data.file");
    } else {
        indexFile = getFile(null, groupId, null, "index.file");
        dataFile = getFile(null, groupId, null, "data.file");
    }
    while (offset <= maxOffset && offset < indexFile.length()) {
        indexFile.seek(offset);
        long dataOffset = indexFile.readLong();
        int dataSize = indexFile.readInt();
        // 根据dataoffset读出 msg
        dataFile.seek(dataOffset);
        byte[] msgBody = new byte[dataSize];
        dataFile.read(msgBody);
        // 读出内容将index文件的type类型设置为已经读“1”
        indexFile.seek(offset + 12);
        indexFile.writeInt(1);
        Message msgTemp = JSON.parseObject(msgBody, Message.class);
        resultMsg.add(msgTemp);
        offset += 16;
    }
    return resultMsg;
}
Also used : Message(com.alibaba.middleware.race.mom.Message) RandomAccessFile(java.io.RandomAccessFile) LinkedList(java.util.LinkedList)

Aggregations

Message (com.alibaba.middleware.race.mom.Message)12 Producer (com.alibaba.middleware.race.mom.Producer)7 SendResult (com.alibaba.middleware.race.mom.SendResult)7 ConsumeResult (com.alibaba.middleware.race.mom.ConsumeResult)6 Consumer (com.alibaba.middleware.race.mom.Consumer)6 MessageListener (com.alibaba.middleware.race.mom.MessageListener)6 TopicAndFilter (com.alibaba.middleware.race.mom.util.TopicAndFilter)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SendCallback (com.alibaba.middleware.race.mom.SendCallback)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1