| /* |
| * 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.rocketmq.ons.api.impl.rocketmq; |
| |
| import java.util.List; |
| import java.util.Properties; |
| import java.util.concurrent.ConcurrentHashMap; |
| |
| import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext; |
| import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; |
| import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly; |
| import org.apache.rocketmq.common.UtilAll; |
| import org.apache.rocketmq.common.message.MessageExt; |
| |
| import org.apache.rocketmq.ons.api.Message; |
| import org.apache.rocketmq.ons.api.MessageSelector; |
| import org.apache.rocketmq.ons.api.PropertyKeyConst; |
| import org.apache.rocketmq.ons.api.exception.ONSClientException; |
| import org.apache.rocketmq.ons.api.order.ConsumeOrderContext; |
| import org.apache.rocketmq.ons.api.order.MessageOrderListener; |
| import org.apache.rocketmq.ons.api.order.OrderAction; |
| import org.apache.rocketmq.ons.api.order.OrderConsumer; |
| |
| public class OrderConsumerImpl extends ONSConsumerAbstract implements OrderConsumer { |
| private final ConcurrentHashMap<String, MessageOrderListener> subscribeTable = new ConcurrentHashMap<String, MessageOrderListener>(); |
| |
| public OrderConsumerImpl(final Properties properties) { |
| super(properties); |
| String suspendTimeMillis = properties.getProperty(PropertyKeyConst.SuspendTimeMillis); |
| if (!UtilAll.isBlank(suspendTimeMillis)) { |
| try { |
| this.defaultMQPushConsumer.setSuspendCurrentQueueTimeMillis(Long.parseLong(suspendTimeMillis)); |
| } catch (NumberFormatException ignored) { |
| } |
| } |
| } |
| |
| @Override |
| public void start() { |
| this.defaultMQPushConsumer.registerMessageListener(new MessageListenerOrderlyImpl()); |
| super.start(); |
| } |
| |
| @Override |
| public void subscribe(String topic, String subExpression, MessageOrderListener listener) { |
| if (null == topic) { |
| throw new ONSClientException("topic is null"); |
| } |
| |
| if (null == listener) { |
| throw new ONSClientException("listener is null"); |
| } |
| this.subscribeTable.put(topic, listener); |
| super.subscribe(topic, subExpression); |
| } |
| |
| @Override |
| public void subscribe(final String topic, final MessageSelector selector, final MessageOrderListener listener) { |
| if (null == topic) { |
| throw new ONSClientException("topic is null"); |
| } |
| |
| if (null == listener) { |
| throw new ONSClientException("listener is null"); |
| } |
| this.subscribeTable.put(topic, listener); |
| super.subscribe(topic, selector); |
| } |
| |
| class MessageListenerOrderlyImpl implements MessageListenerOrderly { |
| |
| @Override |
| public ConsumeOrderlyStatus consumeMessage(List<MessageExt> arg0, ConsumeOrderlyContext arg1) { |
| MessageExt msgRMQ = arg0.get(0); |
| Message msg = ONSUtil.msgConvert(msgRMQ); |
| msg.setMsgID(msgRMQ.getMsgId()); |
| |
| MessageOrderListener listener = OrderConsumerImpl.this.subscribeTable.get(msg.getTopic()); |
| if (null == listener) { |
| throw new ONSClientException("MessageOrderListener is null"); |
| } |
| |
| final ConsumeOrderContext context = new ConsumeOrderContext(); |
| OrderAction action = listener.consume(msg, context); |
| if (action != null) { |
| switch (action) { |
| case Success: |
| return ConsumeOrderlyStatus.SUCCESS; |
| case Suspend: |
| return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; |
| default: |
| break; |
| } |
| } |
| |
| return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; |
| } |
| } |
| } |