• 说明: 本wiki是关于microservice-msq工具包的使用说明。 microservice-msq工具包主要封装对阿里云的消息服务的api。阿里云的消息服务的参考链接:https://help.aliyun.com/document_detail/27431.html


  • 接口说明


  • CloudTopicProducerBuilder:该类是microservice-msq工具包中各个api:"TopicProducer"、"CloudPullTopicConsumer"、"P2PProducer"、"Consumer"的构建者

构建方法:

  • [ 1] public P2PProducer buildP2PProducer(String queue) :创建queue的P2PProducer

  • [ 2] public TopicProducer buildTopic(String topic) :创建topic的TopicProducer

  • [ 3] public CloudPullTopicConsumer buildTopicPullConsumer(String topic,String queue):根据topic->queue创建topic对应queue的消费者(topicPullConsumer,内部操作会检查queue是否存在,不存在就会创建)

  • [ 4] public CloudPullTopicConsumer buildTopicPullConsumer(String topicName, String queue, boolean initiate):参数initiate 是否需要立刻init,如果为false,那么需要手动调用{@link CloudPullTopicConsumer#init()}方法,主要供测试用

  • [ 5] public CloudPullTopicConsumer buildTopicPullConsumer(String topic,TopicMeta topicMeta,QueueMeta queueMeta,String queue):重载方法,用于设定topicMeta和queueMeta

  • [ 6] public Consumer buildConsumer(String queue):构建queue的consumer

  • [ 7] public CloudPullTopicProducer buildCloudTopic(String topic,boolean createQueue,String... queueNameList):createQueue为true的时候,会在阿里云消息服务中创建topci对应queueNameList. CloudPullTopicProducer与TopicProducer的区别在于:TopicProducer只创建topic的引用,而不会创建queue。

  • [ 8] public CloudPullTopicProducer buildCloudTopic(String topic,boolean createQueue,QueueMeta queueMetaTemplate,String... queueNameList):重载可以指定创建queue时需要的模板元数据


  1. TopicProducer:封装阿里云消息服务的主题(topic)模型的消息发布者。用户在实例化TopicProducer之后,可以直接调用TopicProducer的send方法publish消息到阿里云消息服务的topic中。

消息发送方法:

  • [ 1] public TopicMessage send(String msgBody):向主题中publish一条消息,消息body的类型为String,返回TopicMessage为阿里云消息服务的原生对象

  • [ 2] public TopicMessage send(String msgBody,String id,String tag) :向主题中publish一条消息,并指定消息id和tag。

  • [ 3] public TopicMessage send(Object object):向主题中publish一条消息,消息body的类型为Object,microservice-msq会将Object对象转换为Json。

  • [ 4] public TopicMessage send(Object object,String tag,String id):向主题中publish一条消息,消息body的类型为Object,并指定id和tag


  1. CloudPullTopicConsumer:分装了阿里云消息服务的主题(topic)模型的消费者。用户在实例化CloudPullTopicConsumer之后,就可以消费消息。

消费方法说明:

  • [ 1] public Message popMessage():从主题对应的队列中pop一条消息,pop消息同时会删除该条消息,返回值Message是阿里云消息服务的原生对象。
  • [ 2] public MessageObject popMessageObject(): 从主题对应的队列中pop一条消息,pop消息同时会删除该条消息,返回值MessageObject 是microservice-msq的封装的消息对象,MessageObject 的作用是可以将消息的内容序列化和反序列化为json和java对象

  • [ 3] public Message peekMessage():从主题对应的队列中peek一条消息,peek消息不会删除该条消息,返回值Message是阿里云消息服务的原生对象

  • [ 4] public MessageObject peekMessageObject(),从主题对应的队列中peek一条消息,peek消息不会删除该条消息,返回值MessageObject 是microservice-msq的封装的消息对象。

  • [ 5] public void deleteAlreadyGetMessage(Message result):手动删除消息 说明:因为peekMessage是不会自动删除消息的,需要手动删除,但是经过测试deleteAlreadyGetMessage方法并不能删除peekMessage的消息,反而会抛出ServiceException.class,经过调查发现peekMessage返回的Message中ReceiptHandle为空,所以才会删除失败,抛出异常。


  1. CloudPullTopicProducer:广播拉取消息模型,请参考(https://help.aliyun.com/document_detail/34483.html?spm=5176.doc27431.6.709.F6Rrjt)

  1. P2PProducer:封装阿里云消息服务的队列(queue)模型的消息生产者。用户在实例化P2PProducer之后,可以直接调用P2PProducer的send方法put消息到阿里云消息服务的queue中。
  • [ 1] public Message send(String msgBody):向队列中put一条消息,消息body的类型为String,返回Message为阿里云消息服务的原生对象

  • [ 2] public Message send(String msgBody,String id) :向主题中put一条消息,并指定消息id和tag。

  • [ 3] public Message send(Object object):向主题中put一条消息,消息body的类型为Object,microservice-msq会将Object对象转换为Json。

  • [ 4] public Message send(Object object,String id):向主题中put一条消息,消息body的类型为Object,并指定id和tag

    注意:TopicProducer和P2PProducer的send方法的区别在于:1.返回值不同、2:P2PProducer无法指定tag


  1. Consumer:封装阿里云消息服务的队列(queue)模型的消息消费者。用户在实例化Consumer之后,就可以消费队列中的消息。 消费方法:参考CloudPullTopicConsumer。

  • 使用方法

step1: 与apalca-config-zookeeper集成:为了集成apalca-config-zookeeper,microservice-msq定义了一个AliMNSConfigBean:

image
image

step2:注入:

image