com.alibaba.rocketmq.client.consumer
类 DefaultMQPullConsumer

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

public class DefaultMQPullConsumer
extends ClientConfig
implements MQPullConsumer

消费者,主动拉取方式消费

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

字段摘要
protected  DefaultMQPullConsumerImpl defaultMQPullConsumerImpl
           
 
构造方法摘要
DefaultMQPullConsumer()
           
DefaultMQPullConsumer(String consumerGroup)
           
 
方法摘要
 void createTopic(String key, String newTopic, int queueNum)
          创建topic
 long earliestMsgStoreTime(MessageQueue mq)
          向服务器查询队列保存的最早消息对应的存储时间
 long fetchConsumeOffset(MessageQueue mq, boolean fromStore)
          获取消费进度,返回-1表示出错
 Set<MessageQueue> fetchMessageQueuesInBalance(String topic)
          根据topic获取MessageQueue,以均衡方式在组内多个成员之间分配
 Set<MessageQueue> fetchSubscribeMessageQueues(String topic)
          根据topic获取对应的MessageQueue,是可被订阅的队列
P.S 从Consumer Cache中拿数据,可以频繁调用。
 AllocateMessageQueueStrategy getAllocateMessageQueueStrategy()
           
 long getBrokerSuspendMaxTimeMillis()
           
 String getConsumerGroup()
           
 long getConsumerPullTimeoutMillis()
           
 long getConsumerTimeoutMillisWhenSuspend()
           
 DefaultMQPullConsumerImpl getDefaultMQPullConsumerImpl()
           
 MessageModel getMessageModel()
           
 MessageQueueListener getMessageQueueListener()
           
 OffsetStore getOffsetStore()
           
 Set<String> getRegisterTopics()
           
 boolean isUnitMode()
           
 long maxOffset(MessageQueue mq)
          向服务器查询队列最大Offset PS: 最大Offset无对应消息,减1有消息
 long minOffset(MessageQueue mq)
          向服务器查询队列最小Offset PS: 最小Offset有对应消息
 PullResult pull(MessageQueue mq, String subExpression, long offset, int maxNums)
          指定队列,主动拉取消息,即使没有消息,也立刻返回
 void pull(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback)
           
 PullResult pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums)
          指定队列,主动拉取消息,如果没有消息,则broker阻塞一段时间再返回(时间可配置)
broker阻塞期间,如果有消息,则立刻将消息返回
 void pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback)
           
 QueryResult queryMessage(String topic, String key, int maxNum, long begin, long end)
          根据消息Key查询消息
 void registerMessageQueueListener(String topic, MessageQueueListener listener)
          注册监听队列变化的listener对象
 long searchOffset(MessageQueue mq, long timestamp)
          根据时间查询对应的offset,精确到毫秒
P.S.
 void sendMessageBack(MessageExt msg, int delayLevel)
          Consumer消费失败的消息可以选择重新发回到服务器端,并延时消费
会首先尝试将消息发回到消息之前存储的主机,此时只传送消息Offset,消息体不传送,不会占用网络带宽
如果发送失败,会自动重试发往其他主机,此时消息体也会传送
重传回去的消息只会被当前Consumer Group消费。
 void setAllocateMessageQueueStrategy(AllocateMessageQueueStrategy allocateMessageQueueStrategy)
           
 void setBrokerSuspendMaxTimeMillis(long brokerSuspendMaxTimeMillis)
           
 void setConsumerGroup(String consumerGroup)
           
 void setConsumerPullTimeoutMillis(long consumerPullTimeoutMillis)
           
 void setConsumerTimeoutMillisWhenSuspend(long consumerTimeoutMillisWhenSuspend)
           
 void setMessageModel(MessageModel messageModel)
           
 void setMessageQueueListener(MessageQueueListener messageQueueListener)
           
 void setOffsetStore(OffsetStore offsetStore)
           
 void setRegisterTopics(Set<String> registerTopics)
           
 void setUnitMode(boolean isUnitMode)
           
 void shutdown()
          关闭服务
 void start()
          启动服务
 void updateConsumeOffset(MessageQueue mq, long offset)
          更新消费进度
只是更新Consumer缓存中的数据,如果是广播模式,则定时更新到本地存储
如果是集群模式,则定时更新到远端Broker

P.S.

 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
 

字段详细信息

defaultMQPullConsumerImpl

protected final transient DefaultMQPullConsumerImpl defaultMQPullConsumerImpl
构造方法详细信息

DefaultMQPullConsumer

public DefaultMQPullConsumer()

DefaultMQPullConsumer

public DefaultMQPullConsumer(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)

getBrokerSuspendMaxTimeMillis

public long getBrokerSuspendMaxTimeMillis()

setBrokerSuspendMaxTimeMillis

public void setBrokerSuspendMaxTimeMillis(long brokerSuspendMaxTimeMillis)

getConsumerGroup

public String getConsumerGroup()

setConsumerGroup

public void setConsumerGroup(String consumerGroup)

getConsumerPullTimeoutMillis

public long getConsumerPullTimeoutMillis()

setConsumerPullTimeoutMillis

public void setConsumerPullTimeoutMillis(long consumerPullTimeoutMillis)

getConsumerTimeoutMillisWhenSuspend

public long getConsumerTimeoutMillisWhenSuspend()

setConsumerTimeoutMillisWhenSuspend

public void setConsumerTimeoutMillisWhenSuspend(long consumerTimeoutMillisWhenSuspend)

getMessageModel

public MessageModel getMessageModel()

setMessageModel

public void setMessageModel(MessageModel messageModel)

getMessageQueueListener

public MessageQueueListener getMessageQueueListener()

setMessageQueueListener

public void setMessageQueueListener(MessageQueueListener messageQueueListener)

getRegisterTopics

public Set<String> getRegisterTopics()

setRegisterTopics

public void setRegisterTopics(Set<String> registerTopics)

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
从接口 MQPullConsumer 复制的描述
启动服务

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

shutdown

public void shutdown()
从接口 MQPullConsumer 复制的描述
关闭服务

指定者:
接口 MQPullConsumer 中的 shutdown

registerMessageQueueListener

public void registerMessageQueueListener(String topic,
                                         MessageQueueListener listener)
从接口 MQPullConsumer 复制的描述
注册监听队列变化的listener对象

指定者:
接口 MQPullConsumer 中的 registerMessageQueueListener
listener - 一旦发生变化,客户端会主动回调listener对象

pull

public PullResult pull(MessageQueue mq,
                       String subExpression,
                       long offset,
                       int maxNums)
                throws MQClientException,
                       RemotingException,
                       MQBrokerException,
                       InterruptedException
从接口 MQPullConsumer 复制的描述
指定队列,主动拉取消息,即使没有消息,也立刻返回

指定者:
接口 MQPullConsumer 中的 pull
参数:
mq - 指定具体要拉取的队列
subExpression - 订阅过滤表达式字符串,broker依据此表达式进行过滤。目前只支持或运算
eg: "tag1 || tag2 || tag3"
如果subExpression等于null或者*,则表示全部订阅
offset - 从指定队列哪个位置开始拉取
maxNums - 一次最多拉取条数
返回:
参见PullResult
抛出:
MQClientException
RemotingException
MQBrokerException
InterruptedException

pull

public void pull(MessageQueue mq,
                 String subExpression,
                 long offset,
                 int maxNums,
                 PullCallback pullCallback)
          throws MQClientException,
                 RemotingException,
                 InterruptedException
指定者:
接口 MQPullConsumer 中的 pull
抛出:
MQClientException
RemotingException
InterruptedException

pullBlockIfNotFound

public PullResult pullBlockIfNotFound(MessageQueue mq,
                                      String subExpression,
                                      long offset,
                                      int maxNums)
                               throws MQClientException,
                                      RemotingException,
                                      MQBrokerException,
                                      InterruptedException
从接口 MQPullConsumer 复制的描述
指定队列,主动拉取消息,如果没有消息,则broker阻塞一段时间再返回(时间可配置)
broker阻塞期间,如果有消息,则立刻将消息返回

指定者:
接口 MQPullConsumer 中的 pullBlockIfNotFound
参数:
mq - 指定具体要拉取的队列
subExpression - 订阅过滤表达式字符串,broker依据此表达式进行过滤。目前只支持或运算
eg: "tag1 || tag2 || tag3"
如果subExpression等于null或者*,则表示全部订阅
offset - 从指定队列哪个位置开始拉取
maxNums - 一次最多拉取条数
返回:
参见PullResult
抛出:
MQClientException
RemotingException
MQBrokerException
InterruptedException

pullBlockIfNotFound

public void pullBlockIfNotFound(MessageQueue mq,
                                String subExpression,
                                long offset,
                                int maxNums,
                                PullCallback pullCallback)
                         throws MQClientException,
                                RemotingException,
                                InterruptedException
指定者:
接口 MQPullConsumer 中的 pullBlockIfNotFound
抛出:
MQClientException
RemotingException
InterruptedException

updateConsumeOffset

public void updateConsumeOffset(MessageQueue mq,
                                long offset)
                         throws MQClientException
从接口 MQPullConsumer 复制的描述
更新消费进度
只是更新Consumer缓存中的数据,如果是广播模式,则定时更新到本地存储
如果是集群模式,则定时更新到远端Broker

P.S. 可频繁调用,无性能开销

指定者:
接口 MQPullConsumer 中的 updateConsumeOffset
抛出:
MQClientException

fetchConsumeOffset

public long fetchConsumeOffset(MessageQueue mq,
                               boolean fromStore)
                        throws MQClientException
从接口 MQPullConsumer 复制的描述
获取消费进度,返回-1表示出错

指定者:
接口 MQPullConsumer 中的 fetchConsumeOffset
返回:
抛出:
MQClientException

fetchMessageQueuesInBalance

public Set<MessageQueue> fetchMessageQueuesInBalance(String topic)
                                              throws MQClientException
从接口 MQPullConsumer 复制的描述
根据topic获取MessageQueue,以均衡方式在组内多个成员之间分配

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

getOffsetStore

public OffsetStore getOffsetStore()

setOffsetStore

public void setOffsetStore(OffsetStore offsetStore)

getDefaultMQPullConsumerImpl

public DefaultMQPullConsumerImpl getDefaultMQPullConsumerImpl()

isUnitMode

public boolean isUnitMode()

setUnitMode

public void setUnitMode(boolean isUnitMode)


Copyright © 2012-2014. All Rights Reserved.