com.alibaba.rocketmq.client.consumer
类 DefaultMQPushConsumer

java.lang.Object
  继承者 com.alibaba.rocketmq.client.ClientConfig
      继承者 com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer
所有已实现的接口:
MQConsumer, MQPushConsumer, MQAdmin

public class DefaultMQPushConsumer
extends ClientConfig
implements MQPushConsumer

类似于Broker Push消息到Consumer方式,但实际仍然是Consumer内部后台从Broker Pull消息
采用长轮询方式拉消息,实时性同push方式一致,且不会无谓的拉消息导致Broker、Consumer压力增大

从以下版本开始:
2013-7-24
作者:
shijia.wxr

字段摘要
protected  DefaultMQPushConsumerImpl defaultMQPushConsumerImpl
           
 
构造方法摘要
DefaultMQPushConsumer()
           
DefaultMQPushConsumer(String consumerGroup)
           
 
方法摘要
 void createTopic(String key, String newTopic, int queueNum)
          创建topic
 long earliestMsgStoreTime(MessageQueue mq)
          向服务器查询队列保存的最早消息对应的存储时间
 Set<MessageQueue> fetchSubscribeMessageQueues(String topic)
          根据topic获取对应的MessageQueue,是可被订阅的队列
P.S 从Consumer Cache中拿数据,可以频繁调用。
 AllocateMessageQueueStrategy getAllocateMessageQueueStrategy()
           
 int getConsumeConcurrentlyMaxSpan()
           
 ConsumeFromWhere getConsumeFromWhere()
           
 int getConsumeMessageBatchMaxSize()
           
 String getConsumerGroup()
           
 int getConsumeThreadMax()
           
 int getConsumeThreadMin()
           
 String getConsumeTimestamp()
           
 DefaultMQPushConsumerImpl getDefaultMQPushConsumerImpl()
           
 MessageListener getMessageListener()
           
 MessageModel getMessageModel()
           
 OffsetStore getOffsetStore()
           
 int getPullBatchSize()
           
 long getPullInterval()
           
 int getPullThresholdForQueue()
           
 Map<String,String> getSubscription()
           
 boolean isPostSubscriptionWhenPull()
           
 boolean isUnitMode()
           
 long maxOffset(MessageQueue mq)
          向服务器查询队列最大Offset PS: 最大Offset无对应消息,减1有消息
 long minOffset(MessageQueue mq)
          向服务器查询队列最小Offset PS: 最小Offset有对应消息
 QueryResult queryMessage(String topic, String key, int maxNum, long begin, long end)
          根据消息Key查询消息
 void registerMessageListener(MessageListener messageListener)
          注册消息监听器,一个Consumer只能有一个监听器
 void resume()
          消费线程恢复,继续消费
 long searchOffset(MessageQueue mq, long timestamp)
          根据时间查询对应的offset,精确到毫秒
P.S.
 void sendMessageBack(MessageExt msg, int delayLevel)
          Consumer消费失败的消息可以选择重新发回到服务器端,并延时消费
会首先尝试将消息发回到消息之前存储的主机,此时只传送消息Offset,消息体不传送,不会占用网络带宽
如果发送失败,会自动重试发往其他主机,此时消息体也会传送
重传回去的消息只会被当前Consumer Group消费。
 void setAllocateMessageQueueStrategy(AllocateMessageQueueStrategy allocateMessageQueueStrategy)
           
 void setConsumeConcurrentlyMaxSpan(int consumeConcurrentlyMaxSpan)
           
 void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere)
           
 void setConsumeMessageBatchMaxSize(int consumeMessageBatchMaxSize)
           
 void setConsumerGroup(String consumerGroup)
           
 void setConsumeThreadMax(int consumeThreadMax)
           
 void setConsumeThreadMin(int consumeThreadMin)
           
 void setConsumeTimestamp(String consumeTimestamp)
           
 void setMessageListener(MessageListener messageListener)
           
 void setMessageModel(MessageModel messageModel)
           
 void setOffsetStore(OffsetStore offsetStore)
           
 void setPostSubscriptionWhenPull(boolean postSubscriptionWhenPull)
           
 void setPullBatchSize(int pullBatchSize)
           
 void setPullInterval(long pullInterval)
           
 void setPullThresholdForQueue(int pullThresholdForQueue)
           
 void setSubscription(Map<String,String> subscription)
           
 void setUnitMode(boolean isUnitMode)
           
 void shutdown()
          关闭服务,一旦关闭,此对象将不可用
 void start()
          启动服务,调用之前确保registerMessageListener与subscribe都已经调用
或者已经通过Spring注入了相关配置
 void subscribe(String topic, String subExpression)
          订阅消息,方法可以调用多次来订阅不同的Topic,也可覆盖之前Topic的订阅过滤表达式
 void suspend()
          消费线程挂起,暂停消费
 void unsubscribe(String topic)
          取消订阅,从当前订阅组内注销,消息会被订阅组内其他订阅者订阅
 void updateCorePoolSize(int corePoolSize)
          动态调整消费线程池线程数量
 MessageExt viewMessage(String msgId)
          根据消息ID,从服务器获取完整的消息
 
从类 com.alibaba.rocketmq.client.ClientConfig 继承的方法
buildMQClientId, cloneClientConfig, getClientCallbackExecutorThreads, getClientIP, getHeartbeatBrokerInterval, getInstanceName, getNamesrvAddr, getPersistConsumerOffsetInterval, getPollNameServerInteval, resetClientConfig, setClientCallbackExecutorThreads, setClientIP, setHeartbeatBrokerInterval, setInstanceName, setNamesrvAddr, setPersistConsumerOffsetInterval, setPollNameServerInteval, toString
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

defaultMQPushConsumerImpl

protected final transient DefaultMQPushConsumerImpl defaultMQPushConsumerImpl
构造方法详细信息

DefaultMQPushConsumer

public DefaultMQPushConsumer()

DefaultMQPushConsumer

public DefaultMQPushConsumer(String consumerGroup)
方法详细信息

createTopic

public void createTopic(String key,
                        String newTopic,
                        int queueNum)
                 throws MQClientException
从接口 MQAdmin 复制的描述
创建topic

指定者:
接口 MQAdmin 中的 createTopic
参数:
key - 请向运维人员申请
newTopic - 要创建的新topic
queueNum - 新topic队列数
抛出:
MQClientException

searchOffset

public long searchOffset(MessageQueue mq,
                         long timestamp)
                  throws MQClientException
从接口 MQAdmin 复制的描述
根据时间查询对应的offset,精确到毫秒
P.S. 当前接口有较多IO开销,请勿频繁调用

指定者:
接口 MQAdmin 中的 searchOffset
参数:
mq - 队列
timestamp - 毫秒形式时间戳
返回:
指定时间对应的offset
抛出:
MQClientException

maxOffset

public long maxOffset(MessageQueue mq)
               throws MQClientException
从接口 MQAdmin 复制的描述
向服务器查询队列最大Offset PS: 最大Offset无对应消息,减1有消息

指定者:
接口 MQAdmin 中的 maxOffset
参数:
mq - 队列
返回:
队列的最大Offset
抛出:
MQClientException

minOffset

public long minOffset(MessageQueue mq)
               throws MQClientException
从接口 MQAdmin 复制的描述
向服务器查询队列最小Offset PS: 最小Offset有对应消息

指定者:
接口 MQAdmin 中的 minOffset
参数:
mq - 队列
返回:
队列的最小Offset
抛出:
MQClientException

earliestMsgStoreTime

public long earliestMsgStoreTime(MessageQueue mq)
                          throws MQClientException
从接口 MQAdmin 复制的描述
向服务器查询队列保存的最早消息对应的存储时间

指定者:
接口 MQAdmin 中的 earliestMsgStoreTime
参数:
mq - 队列
返回:
最早消息对应的存储时间,精确到毫秒
抛出:
MQClientException

viewMessage

public MessageExt viewMessage(String msgId)
                       throws RemotingException,
                              MQBrokerException,
                              InterruptedException,
                              MQClientException
从接口 MQAdmin 复制的描述
根据消息ID,从服务器获取完整的消息

指定者:
接口 MQAdmin 中的 viewMessage
返回:
完整消息
抛出:
RemotingException
MQBrokerException
InterruptedException
MQClientException

queryMessage

public QueryResult queryMessage(String topic,
                                String key,
                                int maxNum,
                                long begin,
                                long end)
                         throws MQClientException,
                                InterruptedException
从接口 MQAdmin 复制的描述
根据消息Key查询消息

指定者:
接口 MQAdmin 中的 queryMessage
参数:
topic - 消息主题
key - 消息关键词
maxNum - 查询最大条数
begin - 起始时间戳
end - 结束时间戳
返回:
查询结果
抛出:
MQClientException
InterruptedException

getAllocateMessageQueueStrategy

public AllocateMessageQueueStrategy getAllocateMessageQueueStrategy()

setAllocateMessageQueueStrategy

public void setAllocateMessageQueueStrategy(AllocateMessageQueueStrategy allocateMessageQueueStrategy)

getConsumeConcurrentlyMaxSpan

public int getConsumeConcurrentlyMaxSpan()

setConsumeConcurrentlyMaxSpan

public void setConsumeConcurrentlyMaxSpan(int consumeConcurrentlyMaxSpan)

getConsumeFromWhere

public ConsumeFromWhere getConsumeFromWhere()

setConsumeFromWhere

public void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere)

getConsumeMessageBatchMaxSize

public int getConsumeMessageBatchMaxSize()

setConsumeMessageBatchMaxSize

public void setConsumeMessageBatchMaxSize(int consumeMessageBatchMaxSize)

getConsumerGroup

public String getConsumerGroup()

setConsumerGroup

public void setConsumerGroup(String consumerGroup)

getConsumeThreadMax

public int getConsumeThreadMax()

setConsumeThreadMax

public void setConsumeThreadMax(int consumeThreadMax)

getConsumeThreadMin

public int getConsumeThreadMin()

setConsumeThreadMin

public void setConsumeThreadMin(int consumeThreadMin)

getDefaultMQPushConsumerImpl

public DefaultMQPushConsumerImpl getDefaultMQPushConsumerImpl()

getMessageListener

public MessageListener getMessageListener()

setMessageListener

public void setMessageListener(MessageListener messageListener)

getMessageModel

public MessageModel getMessageModel()

setMessageModel

public void setMessageModel(MessageModel messageModel)

getPullBatchSize

public int getPullBatchSize()

setPullBatchSize

public void setPullBatchSize(int pullBatchSize)

getPullInterval

public long getPullInterval()

setPullInterval

public void setPullInterval(long pullInterval)

getPullThresholdForQueue

public int getPullThresholdForQueue()

setPullThresholdForQueue

public void setPullThresholdForQueue(int pullThresholdForQueue)

getSubscription

public Map<String,String> getSubscription()

setSubscription

public void setSubscription(Map<String,String> subscription)

sendMessageBack

public void sendMessageBack(MessageExt msg,
                            int delayLevel)
                     throws RemotingException,
                            MQBrokerException,
                            InterruptedException,
                            MQClientException
从接口 MQConsumer 复制的描述
Consumer消费失败的消息可以选择重新发回到服务器端,并延时消费
会首先尝试将消息发回到消息之前存储的主机,此时只传送消息Offset,消息体不传送,不会占用网络带宽
如果发送失败,会自动重试发往其他主机,此时消息体也会传送
重传回去的消息只会被当前Consumer Group消费。

指定者:
接口 MQConsumer 中的 sendMessageBack
抛出:
RemotingException
MQBrokerException
InterruptedException
MQClientException

fetchSubscribeMessageQueues

public Set<MessageQueue> fetchSubscribeMessageQueues(String topic)
                                              throws MQClientException
从接口 MQConsumer 复制的描述
根据topic获取对应的MessageQueue,是可被订阅的队列
P.S 从Consumer Cache中拿数据,可以频繁调用。Cache中数据大约30秒更新一次

指定者:
接口 MQConsumer 中的 fetchSubscribeMessageQueues
参数:
topic - 消息Topic
返回:
返回队列集合
抛出:
MQClientException

start

public void start()
           throws MQClientException
从接口 MQPushConsumer 复制的描述
启动服务,调用之前确保registerMessageListener与subscribe都已经调用
或者已经通过Spring注入了相关配置

指定者:
接口 MQPushConsumer 中的 start
抛出:
MQClientException

shutdown

public void shutdown()
从接口 MQPushConsumer 复制的描述
关闭服务,一旦关闭,此对象将不可用

指定者:
接口 MQPushConsumer 中的 shutdown

registerMessageListener

public void registerMessageListener(MessageListener messageListener)
从接口 MQPushConsumer 复制的描述
注册消息监听器,一个Consumer只能有一个监听器

指定者:
接口 MQPushConsumer 中的 registerMessageListener

subscribe

public void subscribe(String topic,
                      String subExpression)
               throws MQClientException
从接口 MQPushConsumer 复制的描述
订阅消息,方法可以调用多次来订阅不同的Topic,也可覆盖之前Topic的订阅过滤表达式

指定者:
接口 MQPushConsumer 中的 subscribe
参数:
topic - 消息主题
subExpression - 订阅过滤表达式字符串,broker依据此表达式进行过滤。目前只支持或运算
eg: "tag1 || tag2 || tag3"
如果subExpression等于null或者*,则表示全部订阅
抛出:
MQClientException

unsubscribe

public void unsubscribe(String topic)
从接口 MQPushConsumer 复制的描述
取消订阅,从当前订阅组内注销,消息会被订阅组内其他订阅者订阅

指定者:
接口 MQPushConsumer 中的 unsubscribe
参数:
topic - 消息主题

updateCorePoolSize

public void updateCorePoolSize(int corePoolSize)
从接口 MQPushConsumer 复制的描述
动态调整消费线程池线程数量

指定者:
接口 MQPushConsumer 中的 updateCorePoolSize

suspend

public void suspend()
从接口 MQPushConsumer 复制的描述
消费线程挂起,暂停消费

指定者:
接口 MQPushConsumer 中的 suspend

resume

public void resume()
从接口 MQPushConsumer 复制的描述
消费线程恢复,继续消费

指定者:
接口 MQPushConsumer 中的 resume

getOffsetStore

public OffsetStore getOffsetStore()

setOffsetStore

public void setOffsetStore(OffsetStore offsetStore)

getConsumeTimestamp

public String getConsumeTimestamp()

setConsumeTimestamp

public void setConsumeTimestamp(String consumeTimestamp)

isPostSubscriptionWhenPull

public boolean isPostSubscriptionWhenPull()

setPostSubscriptionWhenPull

public void setPostSubscriptionWhenPull(boolean postSubscriptionWhenPull)

isUnitMode

public boolean isUnitMode()

setUnitMode

public void setUnitMode(boolean isUnitMode)


Copyright © 2012-2014. All Rights Reserved.