package com.alibaba.ons.api.impl.rocketmq;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.ons.api.Action;
import com.alibaba.ons.api.ConsumeContext;
import com.alibaba.ons.api.Consumer;
import com.alibaba.ons.api.Message;
import com.alibaba.ons.api.MessageListener;
import com.alibaba.ons.api.PropertyKeyConst;
import com.alibaba.ons.api.exception.ONSClientException;
import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;
public class ConsumerImpl extends ONSClientAbstract implements Consumer {
private final DefaultMQPushConsumer defaultMQPushConsumer;
private final ConcurrentHashMap<String/* Topic */, MessageListener> subscribeTable =
new ConcurrentHashMap<String, MessageListener>();
public ConsumerImpl(final Properties properties) {
super(properties);
this.defaultMQPushConsumer = new DefaultMQPushConsumer();
String consumerGroup = properties.getProperty(PropertyKeyConst.ConsumerId);
if (null == consumerGroup) {
throw new ONSClientException("\'ConsumerGroup\' property is null");
}
this.defaultMQPushConsumer.setConsumerGroup(consumerGroup);
this.defaultMQPushConsumer.setInstanceName(this.buildIntanceName());
}
@Override
public Properties getProperties() {
return this.properties;
}
@Override
public void start() {
this.defaultMQPushConsumer.registerMessageListener(new MessageListenerImpl());
try {
this.defaultMQPushConsumer.start();
}
catch (Exception e) {
throw new ONSClientException("defaultMQPushConsumer start exception", e);
}
}
@Override
public void shutdown() {
this.defaultMQPushConsumer.shutdown();
}
class MessageListenerImpl implements MessageListenerConcurrently {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgsRMQList,
ConsumeConcurrentlyContext contextRMQ) {
MessageExt msgRMQ = msgsRMQList.get(0);
Message msg = ONSUtil.msgConvert(msgRMQ);
MessageListener listener = ConsumerImpl.this.subscribeTable.get(msg.getTopic());
if (null == listener) {
throw new ONSClientException("MessageListener is null");
}
final ConsumeContext context = new ConsumeContext();
Action action = listener.consume(msg, context);
if (action != null) {
switch (action) {
case CommitMessage:
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
case ReconsumeLater:
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
default:
break;
}
}
return null;
}
}
@Override
public void subscribe(String topic, String subExpression, MessageListener listener) {
if (null == topic) {
throw new ONSClientException("topic is null");
}
if (null == listener) {
throw new ONSClientException("listener is null");
}
try {
this.subscribeTable.put(topic, listener);
this.defaultMQPushConsumer.subscribe(topic, subExpression);
}
catch (MQClientException e) {
throw new ONSClientException("defaultMQPushConsumer subscribe exception", e);
}
}
}