Class: MQTransProducer

MQTransProducer

MQ的消息生产者,支持事务


new MQTransProducer(client, instanceId, topic, groupId)

构造函数

Parameters:
Name Type Description
client MQClient

MQ的客户端

instanceId string

实例ID

topic string

主题名字

groupId string

客户端GroupID

Source:
Returns:
Type
MQTransProducer

Methods


<async> commit(receiptHandle)

提交事务消息

Parameters:
Name Type Description
receiptHandle string

consumeHalfMessage返回的单条消息句柄或者是发送事务消息返回的句柄

Source:
Throws:

err 请求失败或者其它网络异常

{
 // MQ服务端返回的错误Code,如ReceiptHandleError,表示消息句柄非法,MessageNotExist如果超过了TransCheckImmunityTime(针对发送事务消息的句柄)或者超过NextCnosumeTime
 Code: ""
 // 请求ID
 RequestId: ""
}
Type
exception
Returns:
{
 // 请求成功
 code:204,
 // 请求ID
 requestId:""
}
Type
object

<async> consumeHalfMessage(numOfMessages, waitSeconds)

消费检查事务半消息,默认如果该条消息没有被 {commit} 或者 {rollback} 在NextConsumeTime时会再次消费到该条消息

Parameters:
Name Type Description
numOfMessages int

每次从服务端消费条消息

waitSeconds int

长轮询的等待时间(可空),如果服务端没有消息请求会在该时间之后返回等于请求阻塞在服务端,如果期间有消息立刻返回

Source:
Throws:

err MQ服务端返回的错误或者其它网络异常

 {
   // MQ服务端返回的错误Code,其中MessageNotExist是正常现象,表示没有可消费的消息
   Code: "",
   // 请求ID
   RequestId: ""
 }
```json
Type
exception
Returns:
{
 code: 200,
 requestId: "",
 body: [
   {
     // 消息ID
     MessageId: "",
     // 消息体MD5
     MessageBodyMD5: "",
     // 发送消息的时间戳,毫秒
     PublishTime: {long},
     // 下次重试消费的时间,前提是这次不调用{commit} 或者 {rollback},毫秒
     NextConsumeTime: {long},
     // 第一次消费的时间,毫秒,对于顺序消费无意义
     FirstConsumeTime: {long},
     // 消费的次数
     ConsumedTimes: {long},
     // 消息句柄,调用 {commit} 或者 {rollback} 需要将消息句柄传入,用于提交或者回滚该条事务消息
     ReceiptHandle: "",
     // 消息内容
     MessageBody: "",
     // 消息标签
     MessageTag: ""
   }
 ]
}

Type
object

<async> publishMessage(body, tag, msgProps)

向主题发送一条消息

Parameters:
Name Type Description
body string

发送的内容

tag string

发送消息的标签

msgProps MessageProperties

发送消息的属性

Source:
Throws:

err MQ服务端返回的错误或者其它网络异常

{
 // MQ服务端返回的错误Code,like: TopicNotExist 
 Code:"",
 // 请求ID
 RequestId:""
}
Type
exception
Returns:
{
 // http请求状态码,发送成功就是201,如果发送失败则抛异常
 code: 201,
 // 请求ID
 requestId: "xxxxxxxxxxxxxx",
 // 发送消息的响应内容
 body: {
   // 消息ID
   MessageId: "",
   // 消息体内容的MD5值
   MessageBodyMD5: ""
   // 消息句柄,仅事务消息存在
   ReceiptHandle: ""
 }
}
Type
object

<async> rollback(receiptHandle)

回滚事务消息

Parameters:
Name Type Description
receiptHandle string

consumeHalfMessage返回的单条消息句柄或者是发送事务消息返回的句柄

Source:
Throws:

err 请求失败或者其它网络异常

{
 // MQ服务端返回的错误Code,如ReceiptHandleError,表示消息句柄非法,MessageNotExist如果超过了TransCheckImmunityTime(针对发送事务消息的句柄)或者超过NextCnosumeTime
 Code: ""
 // 请求ID
 RequestId: ""
}
Type
exception
Returns:
{
 // 请求成功
 code:204,
 // 请求ID
 requestId:""
}
Type
object