Java Code Examples for org.apache.rocketmq.client.consumer.PullResult#getMsgFoundList()

The following examples show how to use org.apache.rocketmq.client.consumer.PullResult#getMsgFoundList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionalMessageServiceImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private GetResult getHalfMsg(MessageQueue messageQueue, long offset) {
    GetResult getResult = new GetResult();

    PullResult result = pullHalfMsg(messageQueue, offset, PULL_MSG_RETRY_NUMBER);
    getResult.setPullResult(result);
    List<MessageExt> messageExts = result.getMsgFoundList();
    if (messageExts == null) {
        return getResult;
    }
    getResult.setMsg(messageExts.get(0));
    return getResult;
}
 
Example 2
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult createDiscardPullResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, "100000");
    }
    return result;
}
 
Example 3
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult createOpPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.setTags(TransactionalMessageUtil.REMOVETAG);
    }
    return result;
}
 
Example 4
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult createImmunityPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS, "0");
    }
    return result;
}
 
Example 5
Source File: TransactionalMessageServiceImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResult createImmunityPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS, "0");
    }
    return result;
}
 
Example 6
Source File: TransactionalMessageServiceImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 获取halfmessage结果
 * @param messageQueue half消息队列
 * @param offset offset
 * @return ;
 */
private GetResult getHalfMsg(MessageQueue messageQueue, long offset) {
    GetResult getResult = new GetResult();

    PullResult result = pullHalfMsg(messageQueue, offset, PULL_MSG_RETRY_NUMBER);
    getResult.setPullResult(result);
    List<MessageExt> messageExts = result.getMsgFoundList();
    if (messageExts == null) {
        return getResult;
    }
    getResult.setMsg(messageExts.get(0));
    return getResult;
}
 
Example 7
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private PullResult createDiscardPullResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, "100000");
    }
    return result;
}
 
Example 8
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private PullResult createOpPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.setTags(TransactionalMessageUtil.REMOVETAG);
    }
    return result;
}
 
Example 9
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private PullResult createImmunityPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS, "0");
    }
    return result;
}
 
Example 10
Source File: TransactionalMessageServiceImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResult createOpPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.setTags(TransactionalMessageUtil.REMOVETAG);
    }
    return result;
}
 
Example 11
Source File: TransactionalMessageServiceImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResult createDiscardPullResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, "100000");
    }
    return result;
}
 
Example 12
Source File: TransactionalMessageServiceImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private GetResult getHalfMsg(MessageQueue messageQueue, long offset) {
    GetResult getResult = new GetResult();

    PullResult result = pullHalfMsg(messageQueue, offset, PULL_MSG_RETRY_NUMBER);
    getResult.setPullResult(result);
    List<MessageExt> messageExts = result.getMsgFoundList();
    if (messageExts == null) {
        return getResult;
    }
    getResult.setMsg(messageExts.get(0));
    return getResult;
}
 
Example 13
Source File: SqlFilterIT.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterPullConsumer() throws Exception {
    int msgSize = 16;

    String group = initConsumerGroup();
    MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))");
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(group);
    consumer.setNamesrvAddr(nsAddr);
    consumer.start();
    Thread.sleep(3000);
    producer.send("TagA", msgSize);
    producer.send("TagB", msgSize);
    producer.send("TagC", msgSize);
    Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size());

    List<String> receivedMessage = new ArrayList<>(2);
    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic);
    for (MessageQueue mq : mqs) {
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pull(mq, selector, getMessageQueueOffset(mq), 32);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        List<MessageExt> msgs = pullResult.getMsgFoundList();
                        for (MessageExt msg : msgs) {
                            receivedMessage.add(new String(msg.getBody()));
                        }
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    assertThat(receivedMessage.size()).isEqualTo(msgSize * 2);
}
 
Example 14
Source File: PullConsumerDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    // First we init the flow control rule for Sentinel.
    initFlowControlRule();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(Constants.TEST_GROUP_NAME);

    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(Constants.TEST_TOPIC_NAME);
    for (MessageQueue mq : mqs) {
        System.out.printf("Consuming messages from the queue: %s%n", mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                if (pullResult.getMsgFoundList() != null) {
                    for (MessageExt msg : pullResult.getMsgFoundList()) {
                        doSomething(msg);
                    }
                }

                long nextOffset = pullResult.getNextBeginOffset();
                putMessageQueueOffset(mq, nextOffset);
                consumer.updateConsumeOffset(mq, nextOffset);
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    consumer.shutdown();
}
 
Example 15
Source File: TransactionalMessageServiceImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
/**
 * Read op message, parse op message, and fill removeMap
 *
 * @param removeMap Half message to be remove, key:halfOffset, value: opOffset.
 * @param opQueue Op message queue.
 * @param pullOffsetOfOp The begin offset of op message queue.
 * @param miniOffset The current minimum offset of half message queue.
 * @param doneOpOffset Stored op messages that have been processed.
 * @return Op message result.
 */
private PullResult fillOpRemoveMap(HashMap<Long, Long> removeMap,
    MessageQueue opQueue, long pullOffsetOfOp, long miniOffset, List<Long> doneOpOffset) {
    PullResult pullResult = pullOpMsg(opQueue, pullOffsetOfOp, 32);
    if (null == pullResult) {
        return null;
    }
    if (pullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL
        || pullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
        log.warn("The miss op offset={} in queue={} is illegal, pullResult={}", pullOffsetOfOp, opQueue,
            pullResult);
        transactionalMessageBridge.updateConsumeOffset(opQueue, pullResult.getNextBeginOffset());
        return pullResult;
    } else if (pullResult.getPullStatus() == PullStatus.NO_NEW_MSG) {
        log.warn("The miss op offset={} in queue={} is NO_NEW_MSG, pullResult={}", pullOffsetOfOp, opQueue,
            pullResult);
        return pullResult;
    }
    List<MessageExt> opMsg = pullResult.getMsgFoundList();
    if (opMsg == null) {
        log.warn("The miss op offset={} in queue={} is empty, pullResult={}", pullOffsetOfOp, opQueue, pullResult);
        return pullResult;
    }
    for (MessageExt opMessageExt : opMsg) {
        Long queueOffset = getLong(new String(opMessageExt.getBody(), TransactionalMessageUtil.charset));
        log.debug("Topic: {} tags: {}, OpOffset: {}, HalfOffset: {}", opMessageExt.getTopic(),
            opMessageExt.getTags(), opMessageExt.getQueueOffset(), queueOffset);
        if (TransactionalMessageUtil.REMOVETAG.equals(opMessageExt.getTags())) {
            if (queueOffset < miniOffset) {
                doneOpOffset.add(opMessageExt.getQueueOffset());
            } else {
                removeMap.put(queueOffset, opMessageExt.getQueueOffset());
            }
        } else {
            log.error("Found a illegal tag in opMessageExt= {} ", opMessageExt);
        }
    }
    log.debug("Remove map: {}", removeMap);
    log.debug("Done op list: {}", doneOpOffset);
    return pullResult;
}
 
Example 16
Source File: RocketMQMessageSource.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
protected synchronized Object doReceive() {
	if (messageQueueChooser.getMessageQueues() == null
			|| messageQueueChooser.getMessageQueues().size() == 0) {
		return null;
	}
	try {
		int count = 0;
		while (count < messageQueueChooser.getMessageQueues().size()) {
			MessageQueue messageQueue;
			synchronized (this.consumerMonitor) {
				messageQueue = messageQueueChooser.choose();
				messageQueueChooser.increment();
			}

			long offset = consumer.fetchConsumeOffset(messageQueue,
					rocketMQConsumerProperties.getExtension().isFromStore());

			log.debug("topic='{}', group='{}', messageQueue='{}', offset now='{}'",
					this.topic, this.group, messageQueue, offset);

			PullResult pullResult;
			if (messageSelector != null) {
				pullResult = consumer.pull(messageQueue, messageSelector, offset, 1);
			}
			else {
				pullResult = consumer.pull(messageQueue, (String) null, offset, 1);
			}

			if (pullResult.getPullStatus() == PullStatus.FOUND) {
				List<MessageExt> messageExtList = pullResult.getMsgFoundList();

				Message message = RocketMQUtil
						.convertToSpringMessage(messageExtList.get(0));

				AcknowledgmentCallback ackCallback = this.ackCallbackFactory
						.createCallback(new RocketMQAckInfo(messageQueue, pullResult,
								consumer, offset));

				Message messageResult = MessageBuilder.fromMessage(message).setHeader(
						IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK,
						ackCallback).build();
				return messageResult;
			}
			else {
				log.debug("messageQueue='{}' PullResult='{}' with topic `{}`",
						messageQueueChooser.getMessageQueues(),
						pullResult.getPullStatus(), topic);
			}
			count++;
		}
	}
	catch (Exception e) {
		log.error("Consumer pull error: " + e.getMessage(), e);
	}
	return null;
}
 
Example 17
Source File: PullConsumerDemo.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    // First we init the flow control rule for Sentinel.
    initFlowControlRule();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(Constants.TEST_GROUP_NAME);

    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(Constants.TEST_TOPIC_NAME);
    for (MessageQueue mq : mqs) {
        System.out.printf("Consuming messages from the queue: %s%n", mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                if (pullResult.getMsgFoundList() != null) {
                    for (MessageExt msg : pullResult.getMsgFoundList()) {
                        doSomething(msg);
                    }
                }

                long nextOffset = pullResult.getNextBeginOffset();
                putMessageQueueOffset(mq, nextOffset);
                consumer.updateConsumeOffset(mq, nextOffset);
                switch (pullResult.getPullStatus()) {
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case FOUND:
                    case NO_MATCHED_MSG:
                    case OFFSET_ILLEGAL:
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    consumer.shutdown();
}
 
Example 18
Source File: TransactionalMessageServiceImpl.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * Read op message, parse op message, and fill removeMap
 *
 * 主题填充removeMap、doneOpOffset数据结构
 * 避免重复调用事务回查
 * @param removeMap Half message to be remove, key:halfOffset, value: opOffset.
 * @param opQueue Op message queue.
 * @param pullOffsetOfOp The begin offset of op message queue.
 * @param miniOffset The current minimum offset of half message queue.
 * @param doneOpOffset Stored op messages that have been processed.
 * @return Op message result.
 */
private PullResult fillOpRemoveMap(HashMap<Long, Long> removeMap,
    MessageQueue opQueue, long pullOffsetOfOp, long miniOffset, List<Long> doneOpOffset) {

    PullResult pullResult = pullOpMsg(opQueue, pullOffsetOfOp, 32);
    if (null == pullResult) {
        return null;
    }
    if (pullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL
        || pullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
        //如果偏移量不合法或者没有
        log.warn("The miss op offset={} in queue={} is illegal, pullResult={}", pullOffsetOfOp, opQueue,
            pullResult);

        transactionalMessageBridge.updateConsumeOffset(opQueue, pullResult.getNextBeginOffset());
        return pullResult;
    } else if (pullResult.getPullStatus() == PullStatus.NO_NEW_MSG) {
        log.warn("The miss op offset={} in queue={} is NO_NEW_MSG, pullResult={}", pullOffsetOfOp, opQueue,
            pullResult);
        return pullResult;
    }
    List<MessageExt> opMsg = pullResult.getMsgFoundList();
    if (opMsg == null) {
        log.warn("The miss op offset={} in queue={} is empty, pullResult={}", pullOffsetOfOp, opQueue, pullResult);
        return pullResult;
    }
    //遍历消息
    for (MessageExt opMessageExt : opMsg) {
        //消息body是以前prepare的队列偏移量
        Long queueOffset = getLong(new String(opMessageExt.getBody(), TransactionalMessageUtil.charset));
        log.info("Topic: {} tags: {}, OpOffset: {}, HalfOffset: {}", opMessageExt.getTopic(),
            opMessageExt.getTags(), opMessageExt.getQueueOffset(), queueOffset);
        if (TransactionalMessageUtil.REMOVETAG.equals(opMessageExt.getTags())) {
            if (queueOffset < miniOffset) {
                doneOpOffset.add(opMessageExt.getQueueOffset());
            } else {
                //已经处理过了。添加到移除队列
                removeMap.put(queueOffset, opMessageExt.getQueueOffset());
            }
        } else {
            log.error("Found a illegal tag in opMessageExt= {} ", opMessageExt);
        }
    }
    log.debug("Remove map: {}", removeMap);
    log.debug("Done op list: {}", doneOpOffset);
    return pullResult;
}
 
Example 19
Source File: TransactionalMessageServiceImpl.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
/**
     * Read op message, parse op message, and fill removeMap
     *
     * @param removeMap Half message to be remove, key:halfOffset, value: opOffset.
     * @param opQueue Op message queue.
     * @param pullOffsetOfOp The begin offset of op message queue.
     * @param miniOffset The current minimum offset of half message queue.
     * @param doneOpOffset Stored op messages that have been processed.
     * @return Op message result.
     */
//
    private PullResult fillOpRemoveMap(HashMap<Long, Long> removeMap,
        MessageQueue opQueue, long pullOffsetOfOp, long miniOffset, List<Long> doneOpOffset) {
        PullResult pullResult = pullOpMsg(opQueue, pullOffsetOfOp, 32);
        if (null == pullResult) {
            return null;
        }
        if (pullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL
            || pullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
            log.warn("The miss op offset={} in queue={} is illegal, pullResult={}", pullOffsetOfOp, opQueue,
                pullResult);
            transactionalMessageBridge.updateConsumeOffset(opQueue, pullResult.getNextBeginOffset());
            return pullResult;
        } else if (pullResult.getPullStatus() == PullStatus.NO_NEW_MSG) {
            log.warn("The miss op offset={} in queue={} is NO_NEW_MSG, pullResult={}", pullOffsetOfOp, opQueue,
                pullResult);
            return pullResult;
        }
        List<MessageExt> opMsg = pullResult.getMsgFoundList();
        if (opMsg == null) {
            log.warn("The miss op offset={} in queue={} is empty, pullResult={}", pullOffsetOfOp, opQueue, pullResult);
            return pullResult;
        }
        for (MessageExt opMessageExt : opMsg) {
            Long queueOffset = getLong(new String(opMessageExt.getBody(), TransactionalMessageUtil.charset));
            log.info("Topic: {} tags: {}, OpOffset: {}, HalfOffset: {}", opMessageExt.getTopic(),
                opMessageExt.getTags(), opMessageExt.getQueueOffset(), queueOffset);
            if (TransactionalMessageUtil.REMOVETAG.equals(opMessageExt.getTags())) {
                if (queueOffset < miniOffset) {
                    doneOpOffset.add(opMessageExt.getQueueOffset());
                } else {
                    removeMap.put(queueOffset, opMessageExt.getQueueOffset());
                }
            } else {
                log.error("Found a illegal tag in opMessageExt= {} ", opMessageExt);
            }
        }
        log.debug("Remove map: {}", removeMap);
        log.debug("Done op list: {}", doneOpOffset);
        return pullResult;
    }