Search in sources :

Example 6 with MessageQueue

use of com.alibaba.rocketmq.common.message.MessageQueue in project warn-report by saaavsaaa.

the class TestProcess method TestPull.

@Test
public void TestPull() throws MQClientException {
    // "192.168.1.44:9876";
    final String address = "192.168.1.45:9876";
    final String topic = "topicTest";
    final String consumerGroup = "cgr";
    final String subExpression = "*";
    PullConsumer consumer = new PullConsumer(address, consumerGroup);
    Set<MessageQueue> mqs = consumer.getMessageQueue(topic);
    for (MessageQueue mq : mqs) {
        System.out.println("Consume from the queue: " + mq);
        SINGLE_MQ: while (true) {
            try {
                PullResult pullResult = consumer.pullBlockIfNotFound(mq, subExpression, getMessageQueueOffset(mq), 32);
                System.out.println(pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch(pullResult.getPullStatus()) {
                    case FOUND:
                        List<MessageExt> msgs = pullResult.getMsgFoundList();
                        for (MessageExt ext : msgs) {
                            String key = ext.getKeys();
                            String keys = ext.getProperty("KEYS");
                            String value = new String(ext.getBody());
                            System.out.println("key : " + key + "; KEYS :" + keys + "; body : " + value + "\n");
                        }
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    for (Map.Entry<MessageQueue, Long> one : offseTable.entrySet()) {
        System.out.println(one.getValue());
    }
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) List(java.util.List) PullConsumer(rocket.PullConsumer) HashMap(java.util.HashMap) Map(java.util.Map) PullResult(com.alibaba.rocketmq.client.consumer.PullResult) MQBrokerException(com.alibaba.rocketmq.client.exception.MQBrokerException) RemotingException(com.alibaba.rocketmq.remoting.exception.RemotingException) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException) Test(org.junit.Test)

Example 7 with MessageQueue

use of com.alibaba.rocketmq.common.message.MessageQueue in project warn-report by saaavsaaa.

the class PullConsumerCluter method getMessage.

public List<MessageExt> getMessage(final String topic, final Long offset, final String subExpression) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
    List<MessageExt> result = new ArrayList<MessageExt>();
    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic);
    for (MessageQueue mq : mqs) {
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, subExpression, offset, 1);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            List<MessageExt> extList = pullResult.getMsgFoundList();
            result.addAll(extList);
        }
    }
    return result;
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) ArrayList(java.util.ArrayList) PullResult(com.alibaba.rocketmq.client.consumer.PullResult)

Example 8 with MessageQueue

use of com.alibaba.rocketmq.common.message.MessageQueue in project jstorm by alibaba.

the class MetaSimpleClient method rebalanceMqList.

/**
 * rebalanceMqList must run after commit
 *
 * @throws MQClientException
 */
public void rebalanceMqList() throws Exception {
    LOG.info("Begin to do rebalance operation");
    Set<MessageQueue> newMqs = getMQ();
    Set<MessageQueue> oldMqs = currentOffsets.keySet();
    if (oldMqs.equals(newMqs) == true) {
        LOG.info("No change of meta queues " + newMqs);
        return;
    }
    Set<MessageQueue> removeMqs = new HashSet<MessageQueue>();
    removeMqs.addAll(oldMqs);
    removeMqs.removeAll(newMqs);
    Set<MessageQueue> addMqs = new HashSet<MessageQueue>();
    addMqs.addAll(newMqs);
    addMqs.removeAll(oldMqs);
    LOG.info("Remove " + removeMqs);
    for (MessageQueue mq : removeMqs) {
        Long offset = frontOffsets.remove(mq);
        updateOffsetToZk(mq, offset);
        backendOffset.remove(mq);
    }
    LOG.info("Add " + addMqs);
    for (MessageQueue mq : addMqs) {
        long offset = getOffsetFromZk(mq);
        frontOffsets.put(mq, offset);
        backendOffset.put(mq, offset);
    }
}
Also used : MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) HashSet(java.util.HashSet)

Example 9 with MessageQueue

use of com.alibaba.rocketmq.common.message.MessageQueue in project jstorm by alibaba.

the class MetaSimpleClient method initOffset.

protected Map<MessageQueue, Long> initOffset() throws MQClientException {
    Set<MessageQueue> queues = getMQ();
    Map<MessageQueue, Long> ret = new HashMap<MessageQueue, Long>();
    Set<MessageQueue> noOffsetQueues = new HashSet<MessageQueue>();
    if (metaSpoutConfig.getStartTimeStamp() != null) {
        Long timeStamp = metaSpoutConfig.getStartTimeStamp();
        for (MessageQueue mq : queues) {
            long offset = consumer.searchOffset(mq, timeStamp);
            if (offset >= 0) {
                LOG.info("Successfully get " + mq + " offset of timestamp " + new Date(timeStamp));
                ret.put(mq, offset);
            } else {
                LOG.info("Failed to get " + mq + " offset of timestamp " + new Date(timeStamp));
                noOffsetQueues.add(mq);
            }
        }
    } else {
        noOffsetQueues.addAll(queues);
    }
    if (noOffsetQueues.size() == 0) {
        return ret;
    }
    for (MessageQueue mq : noOffsetQueues) {
        long offset = getOffsetFromZk(mq);
        ret.put(mq, offset);
    }
    return ret;
}
Also used : MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) HashMap(java.util.HashMap) Date(java.util.Date) HashSet(java.util.HashSet)

Aggregations

MessageQueue (com.alibaba.rocketmq.common.message.MessageQueue)9 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)6 PullResult (com.alibaba.rocketmq.client.consumer.PullResult)5 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 MQBrokerException (com.alibaba.rocketmq.client.exception.MQBrokerException)2 RemotingException (com.alibaba.rocketmq.remoting.exception.RemotingException)2 List (java.util.List)2 Test (org.junit.Test)2 FailedException (backtype.storm.topology.FailedException)1 PullConsumer (cn.tellwhy.third.rocket.PullConsumer)1 DefaultMQPullConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPullConsumer)1 PullStatus (com.alibaba.rocketmq.client.consumer.PullStatus)1 OffsetStore (com.alibaba.rocketmq.client.consumer.store.OffsetStore)1 ByteBufferInputRowParser (io.druid.data.input.ByteBufferInputRowParser)1 Firehose (io.druid.data.input.Firehose)1 InputRow (io.druid.data.input.InputRow)1