com.aliyun.openservices.ons.client.rocketmq.impl.PushConsumerImpl Maven / Gradle / Ivy
package com.aliyun.openservices.ons.client.rocketmq.impl;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.MessageSelector;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import com.aliyun.openservices.ons.client.rocketmq.PushConsumer;
import com.aliyun.openservices.ons.client.utils.UtilAll;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.aliyun.openservices.ons.shaded.commons.lang3.StringUtils;
import com.aliyun.openservices.ons.shaded.org.apache.rocketmq.client.consumer.ConsumeStatus;
import com.aliyun.openservices.ons.shaded.org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.aliyun.openservices.ons.shaded.org.apache.rocketmq.client.message.MessageExt;
import com.aliyun.openservices.ons.shaded.org.slf4j.Logger;
import com.aliyun.openservices.ons.shaded.org.slf4j.LoggerFactory;
public class PushConsumerImpl extends PushConsumer implements Consumer {
private static final Logger log = LoggerFactory.getLogger(PushConsumerImpl.class);
private final ConcurrentMap subscribeTable;
public PushConsumerImpl(final Properties properties) {
super(properties);
this.subscribeTable = new ConcurrentHashMap();
}
@Override
public void start() {
this.defaultMQPushConsumer.registerMessageListener(new MessageListenerImpl());
log.info("Register message listener successfully.");
super.start();
}
@Override
public void subscribe(String topic, String subExpression, MessageListener listener) {
MessageSelector selector = MessageSelector.byTag(subExpression);
this.subscribe(topic, selector, listener);
}
@Override
public void subscribe(String topic, MessageSelector selector, MessageListener listener) {
if (StringUtils.isBlank(topic)) {
throw new ONSClientException("Topic is blank unexpectedly, please set it.");
}
if (null == listener) {
throw new ONSClientException("Listener is null, please set it.");
}
subscribeTable.put(topic, listener);
super.subscribe(topic, selector);
}
@Override
public void rateLimit(String topic, double permitsPerSecond) {
defaultMQPushConsumer.rateLimit(topic, permitsPerSecond);
}
@Override
public void unsubscribe(String topic) {
if (StringUtils.isBlank(topic)) {
return;
}
this.subscribeTable.remove(topic);
super.unsubscribe(topic);
}
class MessageListenerImpl extends MessageListenerConcurrently {
@Override
public ConsumeStatus consume(List msgList,
com.aliyun.openservices.ons.shaded.org.apache.rocketmq.client.consumer.ConsumeContext consumeContext) {
final MessageExt rmqMessageExt = msgList.get(0);
final Message message = UtilAll.msgConvert(rmqMessageExt);
final String topic = message.getTopic();
final MessageListener messageListener = PushConsumerImpl.this.subscribeTable.get(topic);
if (null == messageListener) {
log.error("MessageListener is null, subscribeTable topics={}, messageExt={}",
PushConsumerImpl.this.subscribeTable.keySet(), rmqMessageExt);
throw new ONSClientException("MessageListener is null.");
}
final ConsumeContext context = new ConsumeContext();
final Action action = messageListener.consume(message, context);
// rocketmq would handle this case.
if (null == action) {
return null;
}
switch (action) {
case CommitMessage:
return ConsumeStatus.OK;
case ReconsumeLater:
default:
return ConsumeStatus.ERROR;
}
}
}
}