Java Code Examples for org.apache.rocketmq.client.consumer.PullStatus#FOUND

The following examples show how to use org.apache.rocketmq.client.consumer.PullStatus#FOUND . 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: RmqAdminServiceImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private Message queryMessageByOffset(String nameServer, String topic, String brokerName, Integer qid, long offset) throws Exception {
    DefaultMQPullConsumer mqPullConsumer = getMqPullConsumer(nameServer);

    MessageQueue mq = new MessageQueue();
    mq.setTopic(topic);
    mq.setBrokerName(brokerName);
    mq.setQueueId(qid);

    PullResult pullResult = mqPullConsumer.pull(mq, "*", offset, 1, 5000);
    if (pullResult == null || pullResult.getPullStatus() != PullStatus.FOUND) {
        throw new MqException(String.format("[RMQ] message not exsit, nsrv:%s, topic:%s, brokerName:%s, qid:%s, offset:%d", nameServer, topic, brokerName, qid, offset));
    }

    MessageExt messageExt = pullResult.getMsgFoundList().get(0);
    if (messageExt.getBody().length == 0) {
        return null;
    }

    String msg;
    if (CodecsUtils.isUtf8(messageExt.getBody())) {
        msg = new String(messageExt.getBody(), "UTF-8");
    } else {
        msg = java.util.Base64.getEncoder().encodeToString(messageExt.getBody());
    }
    return new Message(brokerName + "_" + qid, offset, msg, messageExt.getTags(), messageExt.getKeys(), messageExt.getStoreSize(), messageExt.getBornTimestamp());
}
 
Example 2
Source File: ConsumeMessageCommandTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example 3
Source File: MQClientAPIImpl.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
private PullResult processPullResponse(final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 4
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private PullResult processPullResponse(final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 5
Source File: RmqAdminServiceImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private Message queryMessageByOffset(String nameServer, String topic, String brokerName, Integer qid, long offset) throws Exception {
    DefaultMQPullConsumer mqPullConsumer = getMqPullConsumer(nameServer);

    MessageQueue mq = new MessageQueue();
    mq.setTopic(topic);
    mq.setBrokerName(brokerName);
    mq.setQueueId(qid);

    PullResult pullResult = mqPullConsumer.pull(mq, "*", offset, 1, 5000);
    if (pullResult == null || pullResult.getPullStatus() != PullStatus.FOUND) {
        throw new MqException(String.format("[RMQ] message not exsit, nsrv:%s, topic:%s, brokerName:%s, qid:%s, offset:%d", nameServer, topic, brokerName, qid, offset));
    }

    MessageExt messageExt = pullResult.getMsgFoundList().get(0);
    if (messageExt.getBody().length == 0) {
        return null;
    }

    String msg;
    if (CodecsUtils.isUtf8(messageExt.getBody())) {
        msg = new String(messageExt.getBody(), "UTF-8");
    } else {
        msg = java.util.Base64.getEncoder().encodeToString(messageExt.getBody());
    }
    return new Message(brokerName + "_" + qid, offset, msg, messageExt.getTags(), messageExt.getKeys(), messageExt.getStoreSize(), messageExt.getBornTimestamp());
}
 
Example 6
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private PullResult processPullResponse(final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader)response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 7
Source File: ConsumeMessageCommandTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example 8
Source File: ConsumeMessageCommandTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example 9
Source File: MQClientAPIImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 10
Source File: MQClientAPIImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 11
Source File: ConsumerLagMonitor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private long getRmqConsumeDelay(RocketMQProduceOffsetFetcher fetcher, MessageQueue mq, long produceOffset, long consumeOffset, String group) {
    try {
        if (produceOffset == 0L || produceOffset <= consumeOffset) {
            // 未生产 or 消费完毕.
            return 0L;
        }

        PullResult consumePullResult = fetcher.queryMsgByOffset(mq, consumeOffset);

        if (consumePullResult != null && consumePullResult.getPullStatus() == PullStatus.FOUND) {
            return TimeUtils.getElapseTime(consumePullResult.getMsgFoundList().get(0).getStoreTimestamp());
        } else if (consumePullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);
            return 0; // 拿不到消息,认为没有延迟.
        } else if (consumePullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);

            PullResult pullResult = fetcher.queryMsgByOffset(mq, consumePullResult.getMinOffset());
            if (pullResult != null && pullResult.getPullStatus() == PullStatus.FOUND) {
                return TimeUtils.getElapseTime(pullResult.getMsgFoundList().get(0).getStoreTimestamp());
            }

            return MSG_NOT_FOUND;
        } else {
            return 0;
        }
    } catch (Exception e) {
        LOGGER.error("queryMsgByOffset failed,", e);
        return -1L;
    }
}
 
Example 12
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 13
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 14
Source File: ConsumerLagMonitor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private long getRmqConsumeDelay(RocketMQProduceOffsetFetcher fetcher, MessageQueue mq, long produceOffset, long consumeOffset, String group) {
    try {
        if (produceOffset == 0L || produceOffset <= consumeOffset) {
            // 未生产 or 消费完毕.
            return 0L;
        }

        PullResult consumePullResult = fetcher.queryMsgByOffset(mq, consumeOffset);

        if (consumePullResult != null && consumePullResult.getPullStatus() == PullStatus.FOUND) {
            return TimeUtils.getElapseTime(consumePullResult.getMsgFoundList().get(0).getStoreTimestamp());
        } else if (consumePullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);
            return 0; // 拿不到消息,认为没有延迟.
        } else if (consumePullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);

            PullResult pullResult = fetcher.queryMsgByOffset(mq, consumePullResult.getMinOffset());
            if (pullResult != null && pullResult.getPullStatus() == PullStatus.FOUND) {
                return TimeUtils.getElapseTime(pullResult.getMsgFoundList().get(0).getStoreTimestamp());
            }

            return MSG_NOT_FOUND;
        } else {
            return 0;
        }
    } catch (Exception e) {
        LOGGER.error("queryMsgByOffset failed,", e);
        return -1L;
    }
}
 
Example 15
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example 16
Source File: Utils.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
/**
 * 检查消费
 *
 * @param nameSvr
 * @param address
 * @throws MQClientException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InterruptedException
 * @throws RemotingException
 * @throws MQBrokerException
 */
public static long checkReceive(String cluster, String nameSvr, String address)
        throws MQClientException, NoSuchFieldException, SecurityException, IllegalArgumentException,
        IllegalAccessException, InterruptedException, RemotingException, MQBrokerException {

    DefaultMQPullConsumer consumer = getReceiveCheckConsumer(nameSvr, cluster, address);
    Field f1 = DefaultMQPullConsumerImpl.class.getDeclaredField("mQClientFactory");
    f1.setAccessible(true);

    MQClientInstance instance = (MQClientInstance) f1.get(consumer.getDefaultMQPullConsumerImpl());

    Field f = MQClientInstance.class.getDeclaredField("brokerAddrTable");
    f.setAccessible(true);

    Field f2 = MQClientInstance.class.getDeclaredField("scheduledExecutorService");
    f2.setAccessible(true);

    ScheduledExecutorService service = (ScheduledExecutorService) f2.get(instance);
    service.shutdown();
    service.awaitTermination(1000, TimeUnit.SECONDS);

    ConcurrentHashMap<String, HashMap<Long, String>> map = (ConcurrentHashMap<String, HashMap<Long, String>>) f.get(instance);
    HashMap<Long, String> addresses = new HashMap<>();
    addresses.put(0L, address);
    map.put("rmqmonitor_" + address, addresses);

    MessageQueue queue = new MessageQueue("SELF_TEST_TOPIC", "rmqmonitor_" + address, 0);

    boolean pullOk = false;
    long maxOffset = -1;
    for (int i = 0; i < 2; ++i) {
        try {
            maxOffset = consumer.getDefaultMQPullConsumerImpl().maxOffset(queue);
            PullResult result = consumer.pull(queue, "*", maxOffset > 100 ? maxOffset - 10 : 0, 1);
            if (result.getPullStatus() == PullStatus.FOUND) {
                pullOk = true;
                break;
            } else if(result.getPullStatus() == PullStatus.NO_NEW_MSG) {
                checkSend(cluster, nameSvr, address);
                continue;
            }

            logger.warn("pull result failed, PullResult={}, cluster={}, namesvr={}, address={}", result, cluster, nameSvr, address);
        } catch (Throwable e) {
            logger.error("pull exception, cluster={}, namesvr={}, address={}", cluster, nameSvr, address, e);
        }
        Thread.sleep(1000);
    }
    if (!pullOk) {
        logger.error(String.format("[AlarmPullErr] cluster=%s, broker=%s", cluster, address));
    } else {
        logger.info("AlarmPullCheck cluster={}, broker={}", cluster, address);
    }
    return maxOffset;
}
 
Example 17
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 18
Source File: Utils.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
/**
 * 检查消费
 *
 * @param nameSvr
 * @param address
 * @throws MQClientException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InterruptedException
 * @throws RemotingException
 * @throws MQBrokerException
 */
public static long checkReceive(String cluster, String nameSvr, String address)
        throws MQClientException, NoSuchFieldException, SecurityException, IllegalArgumentException,
        IllegalAccessException, InterruptedException, RemotingException, MQBrokerException {

    DefaultMQPullConsumer consumer = getReceiveCheckConsumer(nameSvr, cluster, address);
    Field f1 = DefaultMQPullConsumerImpl.class.getDeclaredField("mQClientFactory");
    f1.setAccessible(true);

    MQClientInstance instance = (MQClientInstance) f1.get(consumer.getDefaultMQPullConsumerImpl());

    Field f = MQClientInstance.class.getDeclaredField("brokerAddrTable");
    f.setAccessible(true);

    Field f2 = MQClientInstance.class.getDeclaredField("scheduledExecutorService");
    f2.setAccessible(true);

    ScheduledExecutorService service = (ScheduledExecutorService) f2.get(instance);
    service.shutdown();
    service.awaitTermination(1000, TimeUnit.SECONDS);

    ConcurrentHashMap<String, HashMap<Long, String>> map = (ConcurrentHashMap<String, HashMap<Long, String>>) f.get(instance);
    HashMap<Long, String> addresses = new HashMap<>();
    addresses.put(0L, address);
    map.put("rmqmonitor_" + address, addresses);

    MessageQueue queue = new MessageQueue("SELF_TEST_TOPIC", "rmqmonitor_" + address, 0);

    boolean pullOk = false;
    long maxOffset = -1;
    for (int i = 0; i < 2; ++i) {
        try {
            maxOffset = consumer.getDefaultMQPullConsumerImpl().maxOffset(queue);
            PullResult result = consumer.pull(queue, "*", maxOffset > 100 ? maxOffset - 10 : 0, 1);
            if (result.getPullStatus() == PullStatus.FOUND) {
                pullOk = true;
                break;
            } else if(result.getPullStatus() == PullStatus.NO_NEW_MSG) {
                checkSend(cluster, nameSvr, address);
                continue;
            }

            logger.warn("pull result failed, PullResult={}, cluster={}, namesvr={}, address={}", result, cluster, nameSvr, address);
        } catch (Throwable e) {
            logger.error("pull exception, cluster={}, namesvr={}, address={}", cluster, nameSvr, address, e);
        }
        Thread.sleep(1000);
    }
    if (!pullOk) {
        logger.error(String.format("[AlarmPullErr] cluster=%s, broker=%s", cluster, address));
    } else {
        logger.info("AlarmPullCheck cluster={}, broker={}", cluster, address);
    }
    return maxOffset;
}