Search in sources :

Example 1 with PullConsumer

use of cn.tellwhy.third.rocket.PullConsumer 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(cn.tellwhy.third.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)

Aggregations

PullConsumer (cn.tellwhy.third.rocket.PullConsumer)1 PullResult (com.alibaba.rocketmq.client.consumer.PullResult)1 MQBrokerException (com.alibaba.rocketmq.client.exception.MQBrokerException)1 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)1 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)1 MessageQueue (com.alibaba.rocketmq.common.message.MessageQueue)1 RemotingException (com.alibaba.rocketmq.remoting.exception.RemotingException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1