rocketmq-client-nodejs 0.0.0 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +69 -1
- package/dist/client/BaseClient.d.ts +92 -0
- package/dist/client/BaseClient.js +306 -0
- package/dist/client/ClientId.d.ts +24 -0
- package/dist/client/ClientId.js +33 -0
- package/dist/client/Logger.d.ts +24 -0
- package/dist/client/Logger.js +34 -0
- package/dist/client/RpcClient.d.ts +47 -0
- package/dist/client/RpcClient.js +208 -0
- package/dist/client/RpcClientManager.d.ts +41 -0
- package/dist/client/RpcClientManager.js +139 -0
- package/dist/client/SessionCredentials.d.ts +21 -0
- package/dist/client/SessionCredentials.js +19 -0
- package/dist/client/Settings.d.ts +30 -0
- package/dist/client/Settings.js +38 -0
- package/dist/client/TelemetrySession.d.ts +27 -0
- package/dist/client/TelemetrySession.js +101 -0
- package/dist/client/UserAgent.d.ts +25 -0
- package/dist/client/UserAgent.js +47 -0
- package/dist/client/index.d.ts +26 -0
- package/dist/client/index.js +43 -0
- package/dist/consumer/Consumer.d.ts +32 -0
- package/dist/consumer/Consumer.js +96 -0
- package/dist/consumer/FilterExpression.d.ts +25 -0
- package/dist/consumer/FilterExpression.js +40 -0
- package/dist/consumer/SimpleConsumer.d.ts +47 -8
- package/dist/consumer/SimpleConsumer.js +105 -27
- package/dist/consumer/SimpleSubscriptionSettings.d.ts +28 -0
- package/dist/consumer/SimpleSubscriptionSettings.js +57 -0
- package/dist/consumer/SubscriptionLoadBalancer.d.ts +23 -0
- package/dist/consumer/SubscriptionLoadBalancer.js +46 -0
- package/dist/consumer/index.d.ts +21 -0
- package/dist/consumer/index.js +38 -0
- package/dist/exception/BadRequestException.d.ts +20 -0
- package/dist/exception/BadRequestException.js +28 -0
- package/dist/exception/ClientException.d.ts +20 -0
- package/dist/exception/ClientException.js +31 -0
- package/dist/exception/ForbiddenException.d.ts +20 -0
- package/dist/exception/ForbiddenException.js +28 -0
- package/dist/exception/InternalErrorException.d.ts +20 -0
- package/dist/exception/InternalErrorException.js +28 -0
- package/dist/exception/NotFoundException.d.ts +20 -0
- package/dist/exception/NotFoundException.js +28 -0
- package/dist/exception/PayloadTooLargeException.d.ts +20 -0
- package/dist/exception/PayloadTooLargeException.js +28 -0
- package/dist/exception/PaymentRequiredException.d.ts +20 -0
- package/dist/exception/PaymentRequiredException.js +28 -0
- package/dist/exception/ProxyTimeoutException.d.ts +20 -0
- package/dist/exception/ProxyTimeoutException.js +28 -0
- package/dist/exception/RequestHeaderFieldsTooLargeException.d.ts +20 -0
- package/dist/exception/RequestHeaderFieldsTooLargeException.js +28 -0
- package/dist/exception/StatusChecker.d.ts +20 -0
- package/dist/exception/StatusChecker.js +98 -0
- package/dist/exception/TooManyRequestsException.d.ts +20 -0
- package/dist/exception/TooManyRequestsException.js +28 -0
- package/dist/exception/UnauthorizedException.d.ts +20 -0
- package/dist/exception/UnauthorizedException.js +28 -0
- package/dist/exception/UnsupportedException.d.ts +20 -0
- package/dist/exception/UnsupportedException.js +28 -0
- package/dist/exception/index.d.ts +29 -0
- package/dist/exception/index.js +46 -0
- package/dist/index.d.ts +22 -3
- package/dist/index.js +23 -4
- package/dist/message/Message.d.ts +37 -0
- package/dist/message/Message.js +43 -0
- package/dist/message/MessageId.d.ts +59 -0
- package/dist/message/MessageId.js +123 -0
- package/dist/message/MessageView.d.ts +39 -0
- package/dist/message/MessageView.js +90 -0
- package/dist/message/PublishingMessage.d.ts +30 -0
- package/dist/message/PublishingMessage.js +98 -0
- package/dist/message/index.d.ts +20 -0
- package/dist/message/index.js +37 -0
- package/dist/producer/Producer.d.ts +42 -0
- package/dist/producer/Producer.js +240 -1
- package/dist/producer/PublishingLoadBalancer.d.ts +24 -0
- package/dist/producer/PublishingLoadBalancer.js +82 -0
- package/dist/producer/PublishingSettings.d.ts +28 -0
- package/dist/producer/PublishingSettings.js +68 -0
- package/dist/producer/SendReceipt.d.ts +28 -0
- package/dist/producer/SendReceipt.js +57 -0
- package/dist/producer/Transaction.d.ts +28 -0
- package/dist/producer/Transaction.js +64 -0
- package/dist/producer/TransactionChecker.d.ts +21 -0
- package/dist/producer/TransactionChecker.js +19 -0
- package/dist/producer/index.d.ts +21 -0
- package/dist/producer/index.js +39 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.d.ts +27 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +67 -0
- package/dist/retry/RetryPolicy.d.ts +46 -0
- package/dist/retry/RetryPolicy.js +19 -0
- package/dist/retry/index.d.ts +18 -0
- package/dist/retry/index.js +35 -0
- package/dist/route/Broker.d.ts +25 -0
- package/dist/route/Broker.js +40 -0
- package/dist/route/Endpoints.d.ts +30 -0
- package/dist/route/Endpoints.js +71 -0
- package/dist/route/MessageQueue.d.ts +27 -0
- package/dist/route/MessageQueue.js +47 -0
- package/dist/route/TopicRouteData.d.ts +24 -0
- package/dist/route/TopicRouteData.js +37 -0
- package/dist/route/index.d.ts +20 -0
- package/dist/route/index.js +37 -0
- package/dist/util/index.d.ts +36 -0
- package/dist/util/index.js +88 -0
- package/package.json +12 -5
- package/src/client/BaseClient.ts +382 -0
- package/src/client/ClientId.ts +31 -0
- package/src/client/Logger.ts +35 -0
- package/src/client/RpcClient.ts +245 -0
- package/src/client/RpcClientManager.ts +173 -0
- package/src/client/SessionCredentials.ts +22 -0
- package/src/client/Settings.ts +45 -0
- package/src/client/TelemetrySession.ts +118 -0
- package/src/client/UserAgent.ts +45 -0
- package/src/client/index.ts +27 -0
- package/src/consumer/Consumer.ts +111 -0
- package/src/consumer/FilterExpression.ts +42 -0
- package/src/consumer/SimpleConsumer.ts +133 -20
- package/src/consumer/SimpleSubscriptionSettings.ts +65 -0
- package/src/consumer/SubscriptionLoadBalancer.ts +47 -0
- package/src/consumer/index.ts +22 -0
- package/src/exception/BadRequestException.ts +25 -0
- package/src/exception/ClientException.ts +29 -0
- package/src/exception/ForbiddenException.ts +25 -0
- package/src/exception/InternalErrorException.ts +25 -0
- package/src/exception/NotFoundException.ts +25 -0
- package/src/exception/PayloadTooLargeException.ts +25 -0
- package/src/exception/PaymentRequiredException.ts +25 -0
- package/src/exception/ProxyTimeoutException.ts +25 -0
- package/src/exception/RequestHeaderFieldsTooLargeException.ts +25 -0
- package/src/exception/StatusChecker.ts +94 -0
- package/src/exception/TooManyRequestsException.ts +25 -0
- package/src/exception/UnauthorizedException.ts +25 -0
- package/src/exception/UnsupportedException.ts +25 -0
- package/src/exception/index.ts +30 -0
- package/src/index.ts +23 -3
- package/src/message/Message.ts +51 -0
- package/src/message/MessageId.ts +123 -0
- package/src/message/MessageView.ts +94 -0
- package/src/message/PublishingMessage.ts +102 -0
- package/src/message/index.ts +21 -0
- package/src/producer/Producer.ts +289 -0
- package/src/producer/PublishingLoadBalancer.ts +85 -0
- package/src/producer/PublishingSettings.ts +77 -0
- package/src/producer/SendReceipt.ts +60 -0
- package/src/producer/Transaction.ts +70 -0
- package/src/producer/TransactionChecker.ts +23 -0
- package/src/producer/index.ts +23 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +76 -0
- package/src/retry/RetryPolicy.ts +51 -0
- package/src/retry/index.ts +19 -0
- package/src/route/Broker.ts +39 -0
- package/src/route/Endpoints.ts +70 -0
- package/src/route/MessageQueue.ts +49 -0
- package/src/route/TopicRouteData.ts +38 -0
- package/src/route/index.ts +21 -0
- package/src/util/index.ts +83 -0
- package/dist/grpc/MessagingServiceClient.d.ts +0 -9
- package/dist/grpc/MessagingServiceClient.js +0 -13
- package/dist/model/SessionCredentials.d.ts +0 -5
- package/dist/model/SessionCredentials.js +0 -3
- package/src/grpc/MessagingServiceClient.ts +0 -17
- package/src/model/SessionCredentials.ts +0 -5
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { readFileSync } from 'node:fs';
|
|
21
|
+
import { UA, Language } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
22
|
+
|
|
23
|
+
const VERSION: string = JSON.parse(readFileSync(path.join(__dirname, '../../package.json'), 'utf-8')).version;
|
|
24
|
+
|
|
25
|
+
export class UserAgent {
|
|
26
|
+
static readonly INSTANCE = new UserAgent(VERSION, os.platform(), os.hostname());
|
|
27
|
+
|
|
28
|
+
readonly version: string;
|
|
29
|
+
readonly platform: string;
|
|
30
|
+
readonly hostname: string;
|
|
31
|
+
|
|
32
|
+
constructor(version: string, platform: string, hostname: string) {
|
|
33
|
+
this.version = version;
|
|
34
|
+
this.platform = platform;
|
|
35
|
+
this.hostname = hostname;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
toProtobuf() {
|
|
39
|
+
return new UA()
|
|
40
|
+
.setLanguage(Language.NODE_JS)
|
|
41
|
+
.setVersion(this.version)
|
|
42
|
+
.setPlatform(this.platform)
|
|
43
|
+
.setHostname(this.hostname);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export * from './BaseClient';
|
|
19
|
+
export * from './ClientId';
|
|
20
|
+
export * from './Logger';
|
|
21
|
+
export * from './UserAgent';
|
|
22
|
+
export * from './RpcClient';
|
|
23
|
+
export * from './RpcClientManager';
|
|
24
|
+
export * from './SessionCredentials';
|
|
25
|
+
export * from './Settings';
|
|
26
|
+
export * from './TelemetrySession';
|
|
27
|
+
export * from './UserAgent';
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { Message, Status } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import {
|
|
20
|
+
AckMessageRequest,
|
|
21
|
+
ChangeInvisibleDurationRequest,
|
|
22
|
+
ReceiveMessageRequest, ReceiveMessageResponse,
|
|
23
|
+
} from '../../proto/apache/rocketmq/v2/service_pb';
|
|
24
|
+
import { MessageView } from '../message';
|
|
25
|
+
import { MessageQueue } from '../route';
|
|
26
|
+
import { StatusChecker } from '../exception';
|
|
27
|
+
import { BaseClient, BaseClientOptions } from '../client';
|
|
28
|
+
import { createDuration, createResource } from '../util';
|
|
29
|
+
import { FilterExpression } from './FilterExpression';
|
|
30
|
+
|
|
31
|
+
export interface ConsumerOptions extends BaseClientOptions {
|
|
32
|
+
consumerGroup: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export abstract class Consumer extends BaseClient {
|
|
36
|
+
protected readonly consumerGroup: string;
|
|
37
|
+
|
|
38
|
+
constructor(options: ConsumerOptions) {
|
|
39
|
+
super(options);
|
|
40
|
+
this.consumerGroup = options.consumerGroup;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected wrapReceiveMessageRequest(batchSize: number, mq: MessageQueue,
|
|
44
|
+
filterExpression: FilterExpression, invisibleDuration: number, longPollingTimeout: number) {
|
|
45
|
+
return new ReceiveMessageRequest()
|
|
46
|
+
.setGroup(createResource(this.consumerGroup))
|
|
47
|
+
.setMessageQueue(mq.toProtobuf())
|
|
48
|
+
.setFilterExpression(filterExpression.toProtobuf())
|
|
49
|
+
.setLongPollingTimeout(createDuration(longPollingTimeout))
|
|
50
|
+
.setBatchSize(batchSize)
|
|
51
|
+
.setAutoRenew(false)
|
|
52
|
+
.setInvisibleDuration(createDuration(invisibleDuration));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async receiveMessage(request: ReceiveMessageRequest, mq: MessageQueue, awaitDuration: number) {
|
|
56
|
+
const endpoints = mq.broker.endpoints;
|
|
57
|
+
const timeout = this.requestTimeout + awaitDuration;
|
|
58
|
+
let status: Status.AsObject | undefined;
|
|
59
|
+
const responses = await this.rpcClientManager.receiveMessage(endpoints, request, timeout);
|
|
60
|
+
const messageList: Message[] = [];
|
|
61
|
+
let transportDeliveryTimestamp: Date | undefined;
|
|
62
|
+
for (const response of responses) {
|
|
63
|
+
switch (response.getContentCase()) {
|
|
64
|
+
case ReceiveMessageResponse.ContentCase.STATUS:
|
|
65
|
+
status = response.getStatus()?.toObject();
|
|
66
|
+
break;
|
|
67
|
+
case ReceiveMessageResponse.ContentCase.MESSAGE:
|
|
68
|
+
messageList.push(response.getMessage()!);
|
|
69
|
+
break;
|
|
70
|
+
case ReceiveMessageResponse.ContentCase.DELIVERY_TIMESTAMP:
|
|
71
|
+
transportDeliveryTimestamp = response.getDeliveryTimestamp()?.toDate();
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
// this.logger.warn("[Bug] Not recognized content for receive message response, mq={}, " +
|
|
75
|
+
// "clientId={}, response={}", mq, clientId, response);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
StatusChecker.check(status);
|
|
79
|
+
const messages = messageList.map(message => new MessageView(message, mq, transportDeliveryTimestamp));
|
|
80
|
+
return messages;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
protected async ackMessage(messageView: MessageView) {
|
|
84
|
+
const endpoints = messageView.endpoints;
|
|
85
|
+
const request = new AckMessageRequest()
|
|
86
|
+
.setGroup(createResource(this.consumerGroup))
|
|
87
|
+
.setTopic(createResource(messageView.topic));
|
|
88
|
+
request.addEntries()
|
|
89
|
+
.setMessageId(messageView.messageId)
|
|
90
|
+
.setReceiptHandle(messageView.receiptHandle);
|
|
91
|
+
const res = await this.rpcClientManager.ackMessage(endpoints, request, this.requestTimeout);
|
|
92
|
+
// FIXME: handle fail ack
|
|
93
|
+
const response = res.toObject();
|
|
94
|
+
StatusChecker.check(response.status);
|
|
95
|
+
return response.entriesList;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected async invisibleDuration(messageView: MessageView, invisibleDuration: number) {
|
|
99
|
+
const request = new ChangeInvisibleDurationRequest()
|
|
100
|
+
.setGroup(createResource(this.consumerGroup))
|
|
101
|
+
.setTopic(createResource(messageView.topic))
|
|
102
|
+
.setReceiptHandle(messageView.receiptHandle)
|
|
103
|
+
.setInvisibleDuration(createDuration(invisibleDuration))
|
|
104
|
+
.setMessageId(messageView.messageId);
|
|
105
|
+
|
|
106
|
+
const res = await this.rpcClientManager.changeInvisibleDuration(messageView.endpoints, request, this.requestTimeout);
|
|
107
|
+
const response = res.toObject();
|
|
108
|
+
StatusChecker.check(response.status);
|
|
109
|
+
return response.receiptHandle;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { FilterType, FilterExpression as FilterExpressionPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
|
|
20
|
+
const TAG_EXPRESSION_SUB_ALL = '*';
|
|
21
|
+
|
|
22
|
+
export class FilterExpression {
|
|
23
|
+
static readonly SUB_ALL = new FilterExpression(TAG_EXPRESSION_SUB_ALL);
|
|
24
|
+
|
|
25
|
+
readonly expression: string;
|
|
26
|
+
readonly filterType: FilterType;
|
|
27
|
+
|
|
28
|
+
constructor(expression: string, filterType = FilterType.TAG) {
|
|
29
|
+
this.expression = expression;
|
|
30
|
+
this.filterType = filterType;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
toProtobuf() {
|
|
34
|
+
return new FilterExpressionPB()
|
|
35
|
+
.setType(this.filterType)
|
|
36
|
+
.setExpression(this.expression);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toString() {
|
|
40
|
+
return `FilterExpression(${this.filterType},${this.expression})`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,27 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { ClientType } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { HeartbeatRequest, NotifyClientTerminationRequest } from '../../proto/apache/rocketmq/v2/service_pb';
|
|
20
|
+
import { MessageView } from '../message';
|
|
21
|
+
import { TopicRouteData } from '../route';
|
|
22
|
+
import { createResource } from '../util';
|
|
23
|
+
import { FilterExpression } from './FilterExpression';
|
|
24
|
+
import { SimpleSubscriptionSettings } from './SimpleSubscriptionSettings';
|
|
25
|
+
import { SubscriptionLoadBalancer } from './SubscriptionLoadBalancer';
|
|
26
|
+
import { Consumer, ConsumerOptions } from './Consumer';
|
|
27
|
+
|
|
28
|
+
export interface SimpleConsumerOptions extends ConsumerOptions {
|
|
29
|
+
/**
|
|
30
|
+
* support tag string as filter, e.g.:
|
|
31
|
+
* ```ts
|
|
32
|
+
* new Map()
|
|
33
|
+
* .set('TestTopic1', 'TestTag1')
|
|
34
|
+
* .set('TestTopic2', 'TestTag2')
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
subscriptions: Map<string/* topic */, FilterExpression | string>;
|
|
38
|
+
/**
|
|
39
|
+
* set await duration for long-polling, default is 30000ms
|
|
40
|
+
*/
|
|
41
|
+
awaitDuration?: number;
|
|
10
42
|
}
|
|
11
43
|
|
|
12
|
-
export class Consumer {
|
|
13
|
-
readonly #
|
|
14
|
-
readonly #
|
|
15
|
-
readonly #
|
|
44
|
+
export class SimpleConsumer extends Consumer {
|
|
45
|
+
readonly #simpleSubscriptionSettings: SimpleSubscriptionSettings;
|
|
46
|
+
readonly #subscriptionExpressions = new Map<string, FilterExpression>();
|
|
47
|
+
readonly #subscriptionRouteDataCache = new Map<string, SubscriptionLoadBalancer>();
|
|
48
|
+
readonly #awaitDuration: number;
|
|
49
|
+
#topicIndex = 0;
|
|
50
|
+
|
|
51
|
+
constructor(options: SimpleConsumerOptions) {
|
|
52
|
+
options.topics = Array.from(options.subscriptions.keys());
|
|
53
|
+
super(options);
|
|
54
|
+
for (const [ topic, filter ] of options.subscriptions.entries()) {
|
|
55
|
+
if (typeof filter === 'string') {
|
|
56
|
+
// filter is tag string
|
|
57
|
+
this.#subscriptionExpressions.set(topic, new FilterExpression(filter));
|
|
58
|
+
} else {
|
|
59
|
+
this.#subscriptionExpressions.set(topic, filter);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
this.#awaitDuration = options.awaitDuration ?? 30000;
|
|
63
|
+
this.#simpleSubscriptionSettings = new SimpleSubscriptionSettings(this.clientId, this.endpoints,
|
|
64
|
+
this.consumerGroup, this.requestTimeout, this.#awaitDuration, this.#subscriptionExpressions);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected getSettings() {
|
|
68
|
+
return this.#simpleSubscriptionSettings;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected wrapHeartbeatRequest() {
|
|
72
|
+
return new HeartbeatRequest()
|
|
73
|
+
.setClientType(ClientType.SIMPLE_CONSUMER)
|
|
74
|
+
.setGroup(createResource(this.consumerGroup));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected wrapNotifyClientTerminationRequest() {
|
|
78
|
+
return new NotifyClientTerminationRequest()
|
|
79
|
+
.setGroup(createResource(this.consumerGroup));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected onTopicRouteDataUpdate(topic: string, topicRouteData: TopicRouteData) {
|
|
83
|
+
this.#updateSubscriptionLoadBalancer(topic, topicRouteData);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#updateSubscriptionLoadBalancer(topic: string, topicRouteData: TopicRouteData) {
|
|
87
|
+
let subscriptionLoadBalancer = this.#subscriptionRouteDataCache.get(topic);
|
|
88
|
+
if (!subscriptionLoadBalancer) {
|
|
89
|
+
subscriptionLoadBalancer = new SubscriptionLoadBalancer(topicRouteData);
|
|
90
|
+
} else {
|
|
91
|
+
subscriptionLoadBalancer = subscriptionLoadBalancer.update(topicRouteData);
|
|
92
|
+
}
|
|
93
|
+
this.#subscriptionRouteDataCache.set(topic, subscriptionLoadBalancer);
|
|
94
|
+
return subscriptionLoadBalancer;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async #getSubscriptionLoadBalancer(topic: string) {
|
|
98
|
+
let loadBalancer = this.#subscriptionRouteDataCache.get(topic);
|
|
99
|
+
if (!loadBalancer) {
|
|
100
|
+
const topicRouteData = await this.getRouteData(topic);
|
|
101
|
+
loadBalancer = this.#updateSubscriptionLoadBalancer(topic, topicRouteData);
|
|
102
|
+
}
|
|
103
|
+
return loadBalancer;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async subscribe(topic: string, filterExpression: FilterExpression) {
|
|
107
|
+
await this.getRouteData(topic);
|
|
108
|
+
this.#subscriptionExpressions.set(topic, filterExpression);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
unsubscribe(topic: string) {
|
|
112
|
+
this.#subscriptionExpressions.delete(topic);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async receive(maxMessageNum = 10, invisibleDuration = 15000) {
|
|
116
|
+
const topic = this.#nextTopic();
|
|
117
|
+
const filterExpression = this.#subscriptionExpressions.get(topic)!;
|
|
118
|
+
const loadBalancer = await this.#getSubscriptionLoadBalancer(topic);
|
|
119
|
+
const mq = loadBalancer.takeMessageQueue();
|
|
120
|
+
const request = this.wrapReceiveMessageRequest(maxMessageNum, mq, filterExpression,
|
|
121
|
+
invisibleDuration, this.#awaitDuration);
|
|
122
|
+
return await this.receiveMessage(request, mq, this.#awaitDuration);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async ack(message: MessageView) {
|
|
126
|
+
await this.ackMessage(message);
|
|
127
|
+
}
|
|
16
128
|
|
|
17
|
-
|
|
18
|
-
this
|
|
19
|
-
this.#topic = options.topic;
|
|
20
|
-
this.#consumerGroup = options.consumerGroup;
|
|
129
|
+
async changeInvisibleDuration0(message: MessageView, invisibleDuration: number) {
|
|
130
|
+
await this.changeInvisibleDuration0(message, invisibleDuration);
|
|
21
131
|
}
|
|
22
132
|
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
133
|
+
#nextTopic() {
|
|
134
|
+
const topics = Array.from(this.#subscriptionExpressions.keys());
|
|
135
|
+
if (this.#topicIndex >= topics.length) {
|
|
136
|
+
this.#topicIndex = 0;
|
|
137
|
+
}
|
|
138
|
+
return topics[this.#topicIndex++];
|
|
26
139
|
}
|
|
27
140
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
Settings as SettingsPB,
|
|
20
|
+
ClientType,
|
|
21
|
+
Subscription,
|
|
22
|
+
} from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
23
|
+
import { Endpoints } from '../route';
|
|
24
|
+
import { Settings, UserAgent } from '../client';
|
|
25
|
+
import { createDuration, createResource } from '../util';
|
|
26
|
+
import { FilterExpression } from './FilterExpression';
|
|
27
|
+
|
|
28
|
+
export class SimpleSubscriptionSettings extends Settings {
|
|
29
|
+
readonly longPollingTimeout: number;
|
|
30
|
+
readonly group: string;
|
|
31
|
+
readonly subscriptionExpressions: Map<string, FilterExpression>;
|
|
32
|
+
|
|
33
|
+
constructor(clientId: string, accessPoint: Endpoints, group: string,
|
|
34
|
+
requestTimeout: number, longPollingTimeout: number, subscriptionExpressions: Map<string, FilterExpression>) {
|
|
35
|
+
super(clientId, ClientType.SIMPLE_CONSUMER, accessPoint, requestTimeout);
|
|
36
|
+
this.longPollingTimeout = longPollingTimeout;
|
|
37
|
+
this.group = group;
|
|
38
|
+
this.subscriptionExpressions = subscriptionExpressions;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
toProtobuf(): SettingsPB {
|
|
42
|
+
const subscription = new Subscription()
|
|
43
|
+
.setGroup(createResource(this.group))
|
|
44
|
+
.setLongPollingTimeout(createDuration(this.longPollingTimeout));
|
|
45
|
+
|
|
46
|
+
for (const [ topic, filterExpression ] of this.subscriptionExpressions.entries()) {
|
|
47
|
+
subscription.addSubscriptions()
|
|
48
|
+
.setTopic(createResource(topic))
|
|
49
|
+
.setExpression(filterExpression.toProtobuf());
|
|
50
|
+
}
|
|
51
|
+
return new SettingsPB()
|
|
52
|
+
.setClientType(this.clientType)
|
|
53
|
+
.setAccessPoint(this.accessPoint.toProtobuf())
|
|
54
|
+
.setRequestTimeout(createDuration(this.requestTimeout))
|
|
55
|
+
.setSubscription(subscription)
|
|
56
|
+
.setUserAgent(UserAgent.INSTANCE.toProtobuf());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
sync(settings: SettingsPB): void {
|
|
60
|
+
if (settings.getPubSubCase() !== SettingsPB.PubSubCase.SUBSCRIPTION) {
|
|
61
|
+
// log.error("[Bug] Issued settings not match with the client type, clientId={}, pubSubCase={}, "
|
|
62
|
+
// + "clientType={}", clientId, pubSubCase, clientType);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { randomInt } from 'node:crypto';
|
|
19
|
+
import { Permission } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
20
|
+
import { MessageQueue, TopicRouteData } from '../route';
|
|
21
|
+
import { MASTER_BROKER_ID } from '../util';
|
|
22
|
+
|
|
23
|
+
export class SubscriptionLoadBalancer {
|
|
24
|
+
#index: number;
|
|
25
|
+
#messageQueues: MessageQueue[];
|
|
26
|
+
|
|
27
|
+
constructor(topicRouteData: TopicRouteData, index?: number) {
|
|
28
|
+
this.#messageQueues = topicRouteData.messageQueues.filter(mq => {
|
|
29
|
+
return mq.queueId === MASTER_BROKER_ID && (mq.permission === Permission.READ || mq.permission === Permission.READ_WRITE);
|
|
30
|
+
});
|
|
31
|
+
this.#index = index === undefined ? randomInt(this.#messageQueues.length) : index;
|
|
32
|
+
if (this.#messageQueues.length === 0) {
|
|
33
|
+
throw new Error(`No readable message queue found, topicRouteData=${JSON.stringify(topicRouteData)}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
update(topicRouteData: TopicRouteData) {
|
|
38
|
+
return new SubscriptionLoadBalancer(topicRouteData, this.#index);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
takeMessageQueue() {
|
|
42
|
+
if (this.#index >= this.#messageQueues.length) {
|
|
43
|
+
this.#index = 0;
|
|
44
|
+
}
|
|
45
|
+
return this.#messageQueues[this.#index++];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export * from './Consumer';
|
|
19
|
+
export * from './FilterExpression';
|
|
20
|
+
export * from './SimpleConsumer';
|
|
21
|
+
export * from './SimpleSubscriptionSettings';
|
|
22
|
+
export * from './SubscriptionLoadBalancer';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { ClientException } from './ClientException';
|
|
19
|
+
|
|
20
|
+
export class BadRequestException extends ClientException {
|
|
21
|
+
constructor(code: number, message: string, requestId?: string) {
|
|
22
|
+
super(code, message, requestId);
|
|
23
|
+
this.name = 'BadRequestException';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const REQUEST_ID_KEY = 'request-id';
|
|
19
|
+
const RESPONSE_CODE_KEY = 'response-code';
|
|
20
|
+
|
|
21
|
+
export class ClientException extends Error {
|
|
22
|
+
code: number;
|
|
23
|
+
|
|
24
|
+
constructor(code: number, message: string, requestId?: string) {
|
|
25
|
+
super(`[${REQUEST_ID_KEY}=${requestId}, ${RESPONSE_CODE_KEY}=${code}] ${message}`);
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.name = 'ClientException';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { ClientException } from './ClientException';
|
|
19
|
+
|
|
20
|
+
export class ForbiddenException extends ClientException {
|
|
21
|
+
constructor(code: number, message: string, requestId?: string) {
|
|
22
|
+
super(code, message, requestId);
|
|
23
|
+
this.name = 'ForbiddenException';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { ClientException } from './ClientException';
|
|
19
|
+
|
|
20
|
+
export class InternalErrorException extends ClientException {
|
|
21
|
+
constructor(code: number, message: string, requestId?: string) {
|
|
22
|
+
super(code, message, requestId);
|
|
23
|
+
this.name = 'InternalErrorException';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { ClientException } from './ClientException';
|
|
19
|
+
|
|
20
|
+
export class NotFoundException extends ClientException {
|
|
21
|
+
constructor(code: number, message: string, requestId?: string) {
|
|
22
|
+
super(code, message, requestId);
|
|
23
|
+
this.name = 'NotFoundException';
|
|
24
|
+
}
|
|
25
|
+
}
|