Github user hustfxj commented on a diff in the pull request: https://github.com/apache/storm/pull/2024#discussion_r111294519 --- Diff: external/storm-rocketmq/src/main/java/org/apache/storm/rocketmq/spout/RocketMQSpout.java --- @@ -0,0 +1,177 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.storm.rocketmq.spout; + +import org.apache.commons.lang.Validate; +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; +import org.apache.rocketmq.client.consumer.MQPushConsumer; +import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; +import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; +import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext; +import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; +import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; +import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.storm.Config; +import org.apache.storm.rocketmq.DefaultMessageRetryManager; +import org.apache.storm.rocketmq.MessageRetryManager; +import org.apache.storm.rocketmq.MessageSet; +import org.apache.storm.rocketmq.RocketMQConfig; +import org.apache.storm.rocketmq.SpoutConfig; +import org.apache.storm.spout.SpoutOutputCollector; +import org.apache.storm.task.TopologyContext; +import org.apache.storm.topology.IRichSpout; +import org.apache.storm.topology.OutputFieldsDeclarer; +import org.apache.storm.tuple.Fields; +import org.apache.storm.tuple.Values; + +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import static org.apache.storm.rocketmq.RocketMQUtils.getBoolean; +import static org.apache.storm.rocketmq.RocketMQUtils.getInteger; + +/** + * RocketMQSpout uses MQPushConsumer as the default implementation. + * PushConsumer is a high level consumer API, wrapping the pulling details + * Looks like broker push messages to consumer + */ +public class RocketMQSpout implements IRichSpout { + // TODO add metrics + + private MQPushConsumer consumer; + private SpoutOutputCollector collector; + private BlockingQueue<MessageSet> queue; + + private Properties properties; + private MessageRetryManager messageRetryManager; + + public RocketMQSpout(Properties properties) { + this.properties = properties; + } + + @Override + public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { + Validate.notEmpty(properties, "Consumer properties can not be empty"); + boolean ordered = getBoolean(properties, RocketMQConfig.CONSUMER_MESSAGES_ORDERLY, false); + + int queueSize = getInteger(properties, SpoutConfig.QUEUE_SIZE, Integer.parseInt(conf.get(Config.TOPOLOGY_MAX_SPOUT_PENDING).toString())); + queue = new LinkedBlockingQueue<>(queueSize); + + consumer = new DefaultMQPushConsumer(); + RocketMQConfig.buildConsumerConfigs(properties, (DefaultMQPushConsumer)consumer, context); + + if (ordered) { + consumer.registerMessageListener(new MessageListenerOrderly() { + @Override + public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, + ConsumeOrderlyContext context) { + if (process(msgs)) { + return ConsumeOrderlyStatus.SUCCESS; + } else { + return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; + } + } + }); + } else { + consumer.registerMessageListener(new MessageListenerConcurrently() { + @Override + public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, + ConsumeConcurrentlyContext context) { + if (process(msgs)) { + return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; + } else { + return ConsumeConcurrentlyStatus.RECONSUME_LATER; --- End diff -- The messages maybe lost because the consumer is based the automatic commit mode of RocketMq here. It had better not commit the message until the storm handle the message successfully. Of course, this is only a consumption strategy.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---