rocketmq-client-nodejs 1.0.8 → 1.1.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.
Files changed (206) hide show
  1. package/README.md +61 -0
  2. package/dist/client/BaseClient.d.ts +44 -1
  3. package/dist/client/BaseClient.js +353 -85
  4. package/dist/client/Logger.js +9 -6
  5. package/dist/client/RpcClient.js +42 -134
  6. package/dist/client/RpcClientManager.d.ts +22 -17
  7. package/dist/client/RpcClientManager.js +61 -56
  8. package/dist/client/TelemetrySession.js +107 -17
  9. package/dist/consumer/ConsumeService.d.ts +2 -1
  10. package/dist/consumer/ConsumeService.js +12 -6
  11. package/dist/consumer/ConsumeTask.d.ts +6 -1
  12. package/dist/consumer/ConsumeTask.js +36 -5
  13. package/dist/consumer/Consumer.d.ts +13 -5
  14. package/dist/consumer/Consumer.js +94 -33
  15. package/dist/consumer/FifoConsumeService.d.ts +3 -1
  16. package/dist/consumer/FifoConsumeService.js +4 -4
  17. package/dist/consumer/LiteConsumerSupport.d.ts +25 -0
  18. package/dist/consumer/LiteConsumerSupport.js +47 -0
  19. package/dist/consumer/LiteFifoConsumeService.d.ts +3 -1
  20. package/dist/consumer/LiteFifoConsumeService.js +4 -4
  21. package/dist/consumer/LitePushConsumerImpl.d.ts +15 -4
  22. package/dist/consumer/LitePushConsumerImpl.js +26 -30
  23. package/dist/consumer/LiteSimpleConsumer.d.ts +177 -0
  24. package/dist/consumer/LiteSimpleConsumer.js +126 -0
  25. package/dist/consumer/LiteSimpleConsumerImpl.d.ts +133 -0
  26. package/dist/consumer/LiteSimpleConsumerImpl.js +179 -0
  27. package/dist/consumer/LiteStandardConsumeService.d.ts +3 -1
  28. package/dist/consumer/LiteStandardConsumeService.js +4 -4
  29. package/dist/consumer/LiteSubscriptionManager.d.ts +27 -6
  30. package/dist/consumer/LiteSubscriptionManager.js +36 -27
  31. package/dist/consumer/OffsetOption.d.ts +1 -0
  32. package/dist/consumer/OffsetOption.js +10 -4
  33. package/dist/consumer/ProcessQueue.d.ts +17 -1
  34. package/dist/consumer/ProcessQueue.js +134 -91
  35. package/dist/consumer/PushConsumer.d.ts +17 -0
  36. package/dist/consumer/PushConsumer.js +117 -26
  37. package/dist/consumer/PushConsumerGaugeObserver.d.ts +33 -0
  38. package/dist/consumer/PushConsumerGaugeObserver.js +60 -0
  39. package/dist/consumer/PushSubscriptionSettings.d.ts +8 -1
  40. package/dist/consumer/PushSubscriptionSettings.js +35 -8
  41. package/dist/consumer/SimpleConsumer.d.ts +5 -0
  42. package/dist/consumer/SimpleConsumer.js +33 -20
  43. package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
  44. package/dist/consumer/StandardConsumeService.d.ts +3 -1
  45. package/dist/consumer/StandardConsumeService.js +17 -8
  46. package/dist/consumer/SubscriptionLoadBalancer.js +3 -3
  47. package/dist/consumer/index.d.ts +1 -0
  48. package/dist/consumer/index.js +2 -1
  49. package/dist/exception/InternalErrorException.d.ts +1 -0
  50. package/dist/exception/InternalErrorException.js +3 -2
  51. package/dist/exception/PayloadEmptyException.d.ts +20 -0
  52. package/dist/exception/PayloadEmptyException.js +28 -0
  53. package/dist/exception/StatusChecker.js +11 -4
  54. package/dist/exception/index.d.ts +1 -0
  55. package/dist/exception/index.js +2 -1
  56. package/dist/exception/receiveStatus.d.ts +18 -0
  57. package/dist/exception/receiveStatus.js +28 -0
  58. package/dist/hook/Attribute.d.ts +26 -0
  59. package/dist/hook/Attribute.js +37 -0
  60. package/dist/hook/AttributeKey.d.ts +32 -0
  61. package/dist/hook/AttributeKey.js +43 -0
  62. package/dist/hook/CompositedMessageInterceptor.d.ts +32 -0
  63. package/dist/hook/CompositedMessageInterceptor.js +87 -0
  64. package/dist/hook/InflightRequestCountInterceptor.d.ts +28 -0
  65. package/dist/hook/InflightRequestCountInterceptor.js +44 -0
  66. package/dist/hook/MessageHookPoints.d.ts +54 -0
  67. package/dist/hook/MessageHookPoints.js +59 -0
  68. package/dist/hook/MessageHookPointsStatus.d.ts +24 -0
  69. package/dist/hook/MessageHookPointsStatus.js +29 -0
  70. package/dist/hook/MessageInterceptor.d.ts +34 -0
  71. package/dist/hook/MessageInterceptor.js +19 -0
  72. package/dist/hook/MessageInterceptorContext.d.ts +31 -0
  73. package/dist/hook/MessageInterceptorContext.js +19 -0
  74. package/dist/hook/MessageInterceptorContextImpl.d.ts +40 -0
  75. package/dist/hook/MessageInterceptorContextImpl.js +71 -0
  76. package/dist/hook/MessageMeterInterceptor.d.ts +39 -0
  77. package/dist/hook/MessageMeterInterceptor.js +149 -0
  78. package/dist/hook/index.d.ts +26 -0
  79. package/dist/hook/index.js +43 -0
  80. package/dist/index.d.ts +1 -0
  81. package/dist/index.js +2 -1
  82. package/dist/message/Message.js +26 -4
  83. package/dist/message/MessageView.js +15 -5
  84. package/dist/message/PublishingMessage.js +4 -4
  85. package/dist/metrics/ClientMeter.d.ts +42 -0
  86. package/dist/metrics/ClientMeter.js +75 -0
  87. package/dist/metrics/ClientMeterManager.d.ts +46 -0
  88. package/dist/metrics/ClientMeterManager.js +203 -0
  89. package/dist/metrics/EmptyGaugeObserver.d.ts +28 -0
  90. package/dist/metrics/EmptyGaugeObserver.js +34 -0
  91. package/dist/metrics/GaugeEnum.d.ts +32 -0
  92. package/dist/metrics/GaugeEnum.js +37 -0
  93. package/dist/metrics/GaugeObserver.d.ts +29 -0
  94. package/dist/metrics/GaugeObserver.js +19 -0
  95. package/dist/metrics/HistogramEnum.d.ts +54 -0
  96. package/dist/metrics/HistogramEnum.js +71 -0
  97. package/dist/metrics/InvocationStatus.d.ts +26 -0
  98. package/dist/metrics/InvocationStatus.js +33 -0
  99. package/dist/metrics/Metric.d.ts +30 -0
  100. package/dist/metrics/Metric.js +44 -0
  101. package/dist/metrics/MetricLabels.d.ts +27 -0
  102. package/dist/metrics/MetricLabels.js +34 -0
  103. package/dist/metrics/index.d.ts +25 -0
  104. package/dist/metrics/index.js +42 -0
  105. package/dist/producer/Producer.d.ts +8 -2
  106. package/dist/producer/Producer.js +83 -34
  107. package/dist/producer/PublishingLoadBalancer.js +11 -5
  108. package/dist/producer/PublishingSettings.js +1 -3
  109. package/dist/producer/SendReceipt.js +3 -2
  110. package/dist/producer/Transaction.d.ts +2 -1
  111. package/dist/producer/Transaction.js +9 -8
  112. package/dist/producer/index.d.ts +1 -0
  113. package/dist/producer/index.js +2 -2
  114. package/dist/retry/CustomizedBackoffRetryPolicy.d.ts +37 -0
  115. package/dist/retry/CustomizedBackoffRetryPolicy.js +72 -0
  116. package/dist/retry/ExponentialBackoffRetryPolicy.js +10 -6
  117. package/dist/retry/RetryPolicy.d.ts +1 -1
  118. package/dist/retry/index.d.ts +1 -0
  119. package/dist/retry/index.js +2 -1
  120. package/dist/route/Endpoints.d.ts +6 -0
  121. package/dist/route/Endpoints.js +69 -9
  122. package/dist/route/IpNameResolver.d.ts +28 -0
  123. package/dist/route/IpNameResolver.js +104 -0
  124. package/dist/route/MessageQueue.js +7 -2
  125. package/dist/route/TopicRouteData.d.ts +5 -0
  126. package/dist/route/TopicRouteData.js +22 -1
  127. package/dist/util/Semaphore.d.ts +47 -0
  128. package/dist/util/Semaphore.js +82 -0
  129. package/dist/util/index.d.ts +1 -1
  130. package/dist/util/index.js +9 -6
  131. package/dist/util/rpcContext.d.ts +20 -0
  132. package/dist/util/rpcContext.js +35 -0
  133. package/package.json +11 -4
  134. package/src/client/BaseClient.ts +392 -91
  135. package/src/client/Logger.ts +9 -5
  136. package/src/client/RpcClient.ts +62 -133
  137. package/src/client/RpcClientManager.ts +103 -60
  138. package/src/client/TelemetrySession.ts +112 -17
  139. package/src/consumer/ConsumeService.ts +16 -5
  140. package/src/consumer/ConsumeTask.ts +43 -3
  141. package/src/consumer/Consumer.ts +116 -35
  142. package/src/consumer/FifoConsumeService.ts +5 -3
  143. package/src/consumer/LiteConsumerSupport.ts +49 -0
  144. package/src/consumer/LiteFifoConsumeService.ts +5 -3
  145. package/src/consumer/LitePushConsumerImpl.ts +34 -23
  146. package/src/consumer/LiteSimpleConsumer.ts +251 -0
  147. package/src/consumer/LiteSimpleConsumerImpl.ts +214 -0
  148. package/src/consumer/LiteStandardConsumeService.ts +5 -3
  149. package/src/consumer/LiteSubscriptionManager.ts +66 -41
  150. package/src/consumer/OffsetOption.ts +10 -3
  151. package/src/consumer/ProcessQueue.ts +165 -107
  152. package/src/consumer/PushConsumer.ts +133 -24
  153. package/src/consumer/PushConsumerGaugeObserver.ts +60 -0
  154. package/src/consumer/PushSubscriptionSettings.ts +40 -9
  155. package/src/consumer/SimpleConsumer.ts +39 -23
  156. package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
  157. package/src/consumer/StandardConsumeService.ts +18 -7
  158. package/src/consumer/SubscriptionLoadBalancer.ts +2 -2
  159. package/src/consumer/index.ts +1 -0
  160. package/src/exception/InternalErrorException.ts +3 -1
  161. package/src/exception/PayloadEmptyException.ts +25 -0
  162. package/src/exception/StatusChecker.ts +10 -2
  163. package/src/exception/index.ts +1 -0
  164. package/src/exception/receiveStatus.ts +25 -0
  165. package/src/hook/Attribute.ts +36 -0
  166. package/src/hook/AttributeKey.ts +42 -0
  167. package/src/hook/CompositedMessageInterceptor.ts +96 -0
  168. package/src/hook/InflightRequestCountInterceptor.ts +46 -0
  169. package/src/hook/MessageHookPoints.ts +55 -0
  170. package/src/hook/MessageHookPointsStatus.ts +25 -0
  171. package/src/hook/MessageInterceptor.ts +35 -0
  172. package/src/hook/MessageInterceptorContext.ts +33 -0
  173. package/src/hook/MessageInterceptorContextImpl.ts +83 -0
  174. package/src/hook/MessageMeterInterceptor.ts +162 -0
  175. package/src/hook/index.ts +27 -0
  176. package/src/index.ts +1 -0
  177. package/src/message/Message.ts +25 -3
  178. package/src/message/MessageView.ts +13 -4
  179. package/src/message/PublishingMessage.ts +3 -3
  180. package/src/metrics/ClientMeter.ts +81 -0
  181. package/src/metrics/ClientMeterManager.ts +227 -0
  182. package/src/metrics/EmptyGaugeObserver.ts +36 -0
  183. package/src/metrics/GaugeEnum.ts +33 -0
  184. package/src/metrics/GaugeObserver.ts +32 -0
  185. package/src/metrics/HistogramEnum.ts +69 -0
  186. package/src/metrics/InvocationStatus.ts +28 -0
  187. package/src/metrics/Metric.ts +42 -0
  188. package/src/metrics/MetricLabels.ts +31 -0
  189. package/src/metrics/index.ts +26 -0
  190. package/src/producer/Producer.ts +94 -38
  191. package/src/producer/PublishingLoadBalancer.ts +10 -4
  192. package/src/producer/PublishingSettings.ts +0 -2
  193. package/src/producer/SendReceipt.ts +2 -1
  194. package/src/producer/Transaction.ts +12 -11
  195. package/src/producer/index.ts +1 -1
  196. package/src/retry/CustomizedBackoffRetryPolicy.ts +81 -0
  197. package/src/retry/ExponentialBackoffRetryPolicy.ts +11 -6
  198. package/src/retry/RetryPolicy.ts +1 -1
  199. package/src/retry/index.ts +1 -0
  200. package/src/route/Endpoints.ts +65 -8
  201. package/src/route/IpNameResolver.ts +119 -0
  202. package/src/route/MessageQueue.ts +6 -1
  203. package/src/route/TopicRouteData.ts +22 -1
  204. package/src/util/Semaphore.ts +82 -0
  205. package/src/util/index.ts +8 -5
  206. package/src/util/rpcContext.ts +34 -0
@@ -15,14 +15,14 @@
15
15
  * limitations under the License.
16
16
  */
17
17
 
18
- import { ClientType } from '../../proto/apache/rocketmq/v2/definition_pb';
18
+ import { ClientType, Settings as SettingsPB } from '../../proto/apache/rocketmq/v2/definition_pb';
19
19
  import { NotifyUnsubscribeLiteCommand } from '../../proto/apache/rocketmq/v2/service_pb';
20
20
  import { Resource } from '../route/Resource';
21
21
  import { Endpoints, TopicRouteData } from '../route';
22
22
  import { PushConsumer, PushConsumerOptions } from './PushConsumer';
23
23
  import { LitePushConsumer } from './LitePushConsumer';
24
24
  import { OffsetOption } from './OffsetOption';
25
- import { LiteSubscriptionManager } from './LiteSubscriptionManager';
25
+ import { LiteConsumerSupport } from './LiteConsumerSupport';
26
26
  import { FilterExpression } from './FilterExpression';
27
27
  import { LiteFifoConsumeService } from './LiteFifoConsumeService';
28
28
  import { LiteStandardConsumeService } from './LiteStandardConsumeService';
@@ -52,7 +52,7 @@ export interface LitePushConsumerOptions extends PushConsumerOptions {
52
52
  * because it uses the assignment-based message delivery mechanism managed by the server.</p>
53
53
  */
54
54
  export class LitePushConsumerImpl extends PushConsumer implements LitePushConsumer {
55
- private readonly liteSubscriptionManager: LiteSubscriptionManager;
55
+ private readonly liteSubscriptionManager: LiteConsumerSupport;
56
56
  private readonly bindTopic: Resource;
57
57
  readonly #messageListener: MessageListener;
58
58
  readonly #enableFifoConsumeAccelerator: boolean;
@@ -73,7 +73,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
73
73
  this.bindTopic = new Resource(options.namespace, options.bindTopic);
74
74
 
75
75
  const groupResource = new Resource(options.namespace, options.consumerGroup);
76
- this.liteSubscriptionManager = new LiteSubscriptionManager(
76
+ this.liteSubscriptionManager = new LiteConsumerSupport(
77
77
  this,
78
78
  this.bindTopic,
79
79
  groupResource,
@@ -95,10 +95,11 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
95
95
  'Create lite FIFO consume service, consumerGroup=%s, clientId=%s, enableFifoConsumeAccelerator=%s',
96
96
  this.consumerGroup, this.clientId, this.#enableFifoConsumeAccelerator,
97
97
  );
98
- return new LiteFifoConsumeService(this.clientId, this.#messageListener, this.#enableFifoConsumeAccelerator);
98
+ return new LiteFifoConsumeService(this.clientId, this.#messageListener, this.#enableFifoConsumeAccelerator,
99
+ this, this.consumerGroup, this.logger);
99
100
  }
100
101
  this.logger.info('Create lite standard consume service, consumerGroup=%s, clientId=%s', this.consumerGroup, this.clientId);
101
- return new LiteStandardConsumeService(this.clientId, this.#messageListener);
102
+ return new LiteStandardConsumeService(this.clientId, this.#messageListener, this, this.consumerGroup, this.logger);
102
103
  }
103
104
 
104
105
  /**
@@ -111,13 +112,11 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
111
112
  }
112
113
 
113
114
  /**
114
- * Override to disable standard assignment scanning for lite consumers.
115
- * LitePushConsumer uses Lite Subscription mechanism instead of assignments.
115
+ * Route updates do not create queues; Lite Push uses the parent's assignment scans to maintain ProcessQueues.
116
116
  */
117
117
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
118
118
  protected onTopicRouteDataUpdate(_topic: string, _topicRouteData: TopicRouteData) {
119
- // Lite consumers do not use standard assignment mechanism
120
- // They rely on LiteSubscriptionManager for message delivery
119
+ // LiteSubscriptionManager maintains subscriptions; server assignments determine receive queues.
121
120
  }
122
121
 
123
122
  /**
@@ -127,8 +126,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
127
126
  * It must be called before the consumer can receive messages.</p>
128
127
  */
129
128
  async startup() {
130
- await super.startup();
131
- this.liteSubscriptionManager.startUp();
129
+ await this.liteSubscriptionManager.startConsumer(() => super.startup());
132
130
  }
133
131
 
134
132
  /**
@@ -138,8 +136,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
138
136
  * and stopping the lite subscription manager.</p>
139
137
  */
140
138
  async shutdown() {
141
- this.liteSubscriptionManager.shutdown();
142
- await super.shutdown();
139
+ await this.liteSubscriptionManager.shutdownConsumer(() => super.shutdown());
143
140
  }
144
141
 
145
142
  /**
@@ -167,10 +164,7 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
167
164
  async subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
168
165
 
169
166
  async subscribeLite(liteTopic: string, offsetOption?: OffsetOption): Promise<void> {
170
- if (!liteTopic || liteTopic.trim().length === 0) {
171
- throw new Error('liteTopic should not be blank');
172
- }
173
- await this.liteSubscriptionManager.subscribeLite(liteTopic, offsetOption ?? null);
167
+ await this.liteSubscriptionManager.subscribeLite(liteTopic, offsetOption);
174
168
  }
175
169
 
176
170
  /**
@@ -182,9 +176,6 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
182
176
  * @param liteTopic - The name of the lite topic to unsubscribe from
183
177
  */
184
178
  async unsubscribeLite(liteTopic: string): Promise<void> {
185
- if (!liteTopic || liteTopic.trim().length === 0) {
186
- throw new Error('liteTopic should not be blank');
187
- }
188
179
  await this.liteSubscriptionManager.unsubscribeLite(liteTopic);
189
180
  }
190
181
 
@@ -218,9 +209,12 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
218
209
  * <p>This method is called when the server sends a notification to unsubscribe
219
210
  * from a lite topic, typically due to quota violations or administrative actions.</p>
220
211
  *
212
+ * @param endpoints - The remote endpoints which sent the command
221
213
  * @param command - The unsubscribe command from the server
222
214
  */
223
- onNotifyUnsubscribeLiteCommand(command: NotifyUnsubscribeLiteCommand) {
215
+ onNotifyUnsubscribeLiteCommand(endpoints: Endpoints, command: NotifyUnsubscribeLiteCommand) {
216
+ this.logger.info('Received notify unsubscribe lite command, liteTopic=%s, endpoints=%s, clientId=%s',
217
+ command.getLiteTopic(), endpoints.facade, this.clientId);
224
218
  this.liteSubscriptionManager.onNotifyUnsubscribeLiteCommand(command);
225
219
  }
226
220
 
@@ -233,7 +227,10 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
233
227
  * @param endpoints - The server endpoints
234
228
  * @param settings - The settings configuration
235
229
  */
236
- onSettingsCommand(endpoints: any, settings: any) {
230
+ onSettingsCommand(endpoints: Endpoints, settings: SettingsPB): void;
231
+ // Preserve the public broad overload; implement handling with the same protocol types as the parent.
232
+ onSettingsCommand(endpoints: any, settings: any): void;
233
+ onSettingsCommand(endpoints: Endpoints, settings: SettingsPB): void {
237
234
  super.onSettingsCommand(endpoints, settings);
238
235
  this.liteSubscriptionManager.sync(settings);
239
236
  }
@@ -278,4 +275,18 @@ export class LitePushConsumerImpl extends PushConsumer implements LitePushConsum
278
275
  getRequestTimeout(): number {
279
276
  return this.requestTimeout;
280
277
  }
278
+
279
+ /**
280
+ * Endpoints the lite subscription manager should sync to.
281
+ *
282
+ * Lite subscriptions must reach every proxy serving the topic route, not only
283
+ * the endpoint the client was configured with, so the route cache is used and
284
+ * the configured endpoints act as the fallback before any route is known.
285
+ *
286
+ * @internal
287
+ */
288
+ getSyncEndpoints(): Endpoints[] {
289
+ const endpointsList = this.getTotalRouteEndpoints();
290
+ return endpointsList.length > 0 ? endpointsList : [ this.endpoints ];
291
+ }
281
292
  }
@@ -0,0 +1,251 @@
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 { BaseClientOptions } from '../client';
19
+ import { MessageView } from '../message';
20
+ import { OffsetOption } from './OffsetOption';
21
+ import { LiteSimpleConsumerImpl } from './LiteSimpleConsumerImpl';
22
+
23
+ /**
24
+ * LiteSimpleConsumer interface for consuming messages from lite topics with
25
+ * explicit receive/ack control.
26
+ *
27
+ * <p>Similar to SimpleConsumer, but bound to a single parent topic and able to
28
+ * (un)subscribe lite topics dynamically through the lite subscription sync
29
+ * protocol.</p>
30
+ */
31
+ export interface LiteSimpleConsumer {
32
+ /**
33
+ * Get the load balancing group for the lite simple consumer.
34
+ *
35
+ * @return Consumer load balancing group
36
+ */
37
+ getConsumerGroup(): string;
38
+
39
+ /**
40
+ * Subscribe to a lite topic.
41
+ *
42
+ * <p>The subscribeLite() method initiates network requests and performs quota
43
+ * verification, so it may fail. It's important to check the result of this
44
+ * call to ensure that the subscription was successfully added. Possible
45
+ * failure scenarios include:</p>
46
+ * <ul>
47
+ * <li>Network request errors, which can be retried.</li>
48
+ * <li>Quota verification failures, indicated by LiteSubscriptionQuotaExceededException.
49
+ * In this case, evaluate whether the quota is insufficient and promptly
50
+ * unsubscribe from unused subscriptions using unsubscribeLite() to free
51
+ * up resources.</li>
52
+ * </ul>
53
+ *
54
+ * @param liteTopic - The name of the lite topic to subscribe
55
+ * @throws ClientException if an error occurs during subscription
56
+ */
57
+ subscribeLite(liteTopic: string): Promise<void>;
58
+
59
+ /**
60
+ * Subscribe to a lite topic with an offset option to specify the consume from
61
+ * offset.
62
+ *
63
+ * @param liteTopic - The name of the lite topic to subscribe
64
+ * @param offsetOption - The consume from offset option
65
+ * @throws ClientException if an error occurs during subscription
66
+ */
67
+ subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
68
+
69
+ /**
70
+ * Unsubscribe from a lite topic.
71
+ *
72
+ * @param liteTopic - The name of the lite topic to unsubscribe from
73
+ * @throws ClientException if an error occurs during unsubscription
74
+ */
75
+ unsubscribeLite(liteTopic: string): Promise<void>;
76
+
77
+ /**
78
+ * Get the lite topic immutable set.
79
+ *
80
+ * @return Lite topic immutable set
81
+ */
82
+ getLiteTopicSet(): Set<string>;
83
+
84
+ /**
85
+ * Fetch messages from the server synchronously.
86
+ *
87
+ * <p>This method returns immediately if there are messages available.
88
+ * Otherwise, it will await the passed timeout. If the timeout expires, an
89
+ * empty list will be returned.</p>
90
+ *
91
+ * @param maxMessageNum - Max message num of server returned
92
+ * @param invisibleDuration - Set the invisibleDuration of messages to return
93
+ * @return List of message views
94
+ */
95
+ receive(maxMessageNum?: number, invisibleDuration?: number): Promise<MessageView[]>;
96
+
97
+ /**
98
+ * Ack the consumption of the message which is returned by receive().
99
+ *
100
+ * @param messageView - Message view with the receipt handle to ack
101
+ * @throws ClientException if an error occurs during ack
102
+ */
103
+ ack(messageView: MessageView): Promise<void>;
104
+
105
+ /**
106
+ * Change the invisible duration of a received message.
107
+ *
108
+ * @param messageView - Message view to change invisible duration
109
+ * @param invisibleDuration - New invisible duration
110
+ * @throws ClientException if an error occurs
111
+ */
112
+ changeInvisibleDuration(messageView: MessageView, invisibleDuration: number): Promise<void>;
113
+
114
+ /**
115
+ * Close the consumer and release all related resources.
116
+ *
117
+ * <p>Once the consumer is closed, <strong>it could not be started once
118
+ * again.</strong></p>
119
+ */
120
+ close(): Promise<void>;
121
+ }
122
+
123
+ export interface LiteSimpleConsumerOptions extends BaseClientOptions {
124
+ consumerGroup: string;
125
+ /**
126
+ * The parent topic the lite consumer binds to. All lite topics subscribed by
127
+ * this consumer must belong to this parent topic.
128
+ */
129
+ bindTopic: string;
130
+ /**
131
+ * set await duration for long-polling, default is 30000ms
132
+ */
133
+ awaitDuration?: number;
134
+ /**
135
+ * max retry attempts for temporary errors (e.g., internal server error), default is 3
136
+ */
137
+ maxRetryAttempts?: number;
138
+ }
139
+
140
+ const CONSUMER_GROUP_PATTERN = /^[a-zA-Z0-9_-]+$/;
141
+
142
+ /**
143
+ * LiteSimpleConsumer builder class.
144
+ *
145
+ * <p>This class provides a fluent API for configuring and creating lite simple
146
+ * consumers with explicit receive/ack control over lite topics.</p>
147
+ */
148
+ export class LiteSimpleConsumerBuilder {
149
+ private options: Partial<LiteSimpleConsumerOptions> = {};
150
+
151
+ /**
152
+ * Set the bind topic for the lite simple consumer.
153
+ *
154
+ * @param bindTopic - The parent topic to bind
155
+ * @return This builder instance
156
+ * @throws Error if bindTopic is blank
157
+ */
158
+ bindTopic(bindTopic: string): LiteSimpleConsumerBuilder {
159
+ if (!bindTopic || bindTopic.trim().length === 0) {
160
+ throw new Error('bindTopic should not be blank');
161
+ }
162
+ this.options.bindTopic = bindTopic;
163
+ return this;
164
+ }
165
+
166
+ /**
167
+ * Set the client configuration.
168
+ *
169
+ * @param options - Client configuration options
170
+ * @return This builder instance
171
+ * @throws Error if options is null/undefined
172
+ */
173
+ setClientConfiguration(options: BaseClientOptions): LiteSimpleConsumerBuilder {
174
+ if (!options) {
175
+ throw new Error('clientConfiguration should not be null');
176
+ }
177
+ Object.assign(this.options, options);
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * Set the consumer group.
183
+ *
184
+ * @param consumerGroup - Consumer group name
185
+ * @return This builder instance
186
+ * @throws Error if consumerGroup is null or doesn't match the pattern
187
+ */
188
+ setConsumerGroup(consumerGroup: string): LiteSimpleConsumerBuilder {
189
+ if (!consumerGroup) {
190
+ throw new Error('consumerGroup should not be null');
191
+ }
192
+ if (!CONSUMER_GROUP_PATTERN.test(consumerGroup)) {
193
+ throw new Error(`consumerGroup does not match the pattern ${CONSUMER_GROUP_PATTERN.source}`);
194
+ }
195
+ this.options.consumerGroup = consumerGroup;
196
+ return this;
197
+ }
198
+
199
+ /**
200
+ * Set the await duration for long-polling receive requests.
201
+ *
202
+ * @param awaitDuration - Maximum time to block when no message is available
203
+ * @return This builder instance
204
+ * @throws Error if awaitDuration is not positive
205
+ */
206
+ setAwaitDuration(awaitDuration: number): LiteSimpleConsumerBuilder {
207
+ if (awaitDuration <= 0) {
208
+ throw new Error('awaitDuration should be positive');
209
+ }
210
+ this.options.awaitDuration = awaitDuration;
211
+ return this;
212
+ }
213
+
214
+ /**
215
+ * Finalize the build of LiteSimpleConsumer and start.
216
+ *
217
+ * <p>This method will block until the lite simple consumer starts
218
+ * successfully.</p>
219
+ *
220
+ * @return Promise resolving to started LiteSimpleConsumer instance
221
+ * @throws Error if required parameters are not set
222
+ */
223
+ async build(): Promise<LiteSimpleConsumer> {
224
+ if (!this.options.endpoints) {
225
+ throw new Error('clientConfiguration has not been set yet');
226
+ }
227
+ if (!this.options.consumerGroup) {
228
+ throw new Error('consumerGroup has not been set yet');
229
+ }
230
+ if (!this.options.bindTopic) {
231
+ throw new Error('bindTopic has not been set yet');
232
+ }
233
+
234
+ const options: LiteSimpleConsumerOptions = {
235
+ endpoints: this.options.endpoints,
236
+ namespace: this.options.namespace ?? '',
237
+ consumerGroup: this.options.consumerGroup!,
238
+ bindTopic: this.options.bindTopic,
239
+ awaitDuration: this.options.awaitDuration,
240
+ maxRetryAttempts: this.options.maxRetryAttempts,
241
+ sslEnabled: this.options.sslEnabled,
242
+ sessionCredentials: this.options.sessionCredentials,
243
+ requestTimeout: this.options.requestTimeout,
244
+ logger: this.options.logger,
245
+ };
246
+
247
+ const liteSimpleConsumer = new LiteSimpleConsumerImpl(options);
248
+ await liteSimpleConsumer.startup();
249
+ return liteSimpleConsumer;
250
+ }
251
+ }
@@ -0,0 +1,214 @@
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, Permission, Settings as SettingsPB } from '../../proto/apache/rocketmq/v2/definition_pb';
19
+ import { NotifyUnsubscribeLiteCommand } from '../../proto/apache/rocketmq/v2/service_pb';
20
+ import { Endpoints, TopicRouteData } from '../route';
21
+ import { Resource } from '../route/Resource';
22
+ import { MASTER_BROKER_ID } from '../util';
23
+ import { ILogger } from '../client/Logger';
24
+ import { RpcClientManager } from '../client/RpcClientManager';
25
+ import { SimpleConsumer, SimpleConsumerOptions } from './SimpleConsumer';
26
+ import { FilterExpression } from './FilterExpression';
27
+ import { LiteSubscriptionHost } from './LiteSubscriptionManager';
28
+ import { LiteConsumerSupport } from './LiteConsumerSupport';
29
+ import { OffsetOption } from './OffsetOption';
30
+ import { LiteSimpleConsumer, LiteSimpleConsumerOptions } from './LiteSimpleConsumer';
31
+
32
+ /**
33
+ * Keep only the first readable master queue of the route. Lite consumers talk
34
+ * to brokers only through the bound parent topic, so a single queue is enough
35
+ * and it avoids useless route bookkeeping for the other queues.
36
+ */
37
+ export function firstReadableMasterQueue(topicRouteData: TopicRouteData): TopicRouteData {
38
+ const readable = topicRouteData.messageQueues.find(
39
+ mq => mq.broker.id === MASTER_BROKER_ID &&
40
+ (mq.permission === Permission.READ || mq.permission === Permission.READ_WRITE),
41
+ );
42
+ return new TopicRouteData(readable ? [ readable.toProtobuf() ] : []);
43
+ }
44
+
45
+ /**
46
+ * Implementation of LiteSimpleConsumer.
47
+ *
48
+ * <p>LiteSimpleConsumer extends SimpleConsumer to provide explicit receive/ack
49
+ * control over lite topics. It binds to a single parent topic and manages lite
50
+ * topic subscriptions dynamically via the lite subscription sync protocol.</p>
51
+ */
52
+ export class LiteSimpleConsumerImpl extends SimpleConsumer
53
+ implements LiteSimpleConsumer, LiteSubscriptionHost {
54
+ readonly #bindTopic: Resource;
55
+ readonly #liteSubscriptionManager: LiteConsumerSupport;
56
+
57
+ constructor(options: LiteSimpleConsumerOptions) {
58
+ if (!options.bindTopic || options.bindTopic.trim().length === 0) {
59
+ throw new TypeError('bindTopic should not be blank');
60
+ }
61
+ // Default subscription: (bindTopic, *) for code reuse.
62
+ const subscriptions = new Map<string, FilterExpression | string>()
63
+ .set(options.bindTopic, FilterExpression.SUB_ALL);
64
+
65
+ super({
66
+ ...options,
67
+ subscriptions,
68
+ } as SimpleConsumerOptions);
69
+
70
+ this.#bindTopic = new Resource(options.namespace, options.bindTopic);
71
+ const groupResource = new Resource(options.namespace, options.consumerGroup);
72
+ this.#liteSubscriptionManager = new LiteConsumerSupport(this, this.#bindTopic, groupResource);
73
+ }
74
+
75
+ /**
76
+ * Get the client type.
77
+ *
78
+ * @return The client type identifier for lite simple consumer
79
+ */
80
+ protected getClientType(): ClientType {
81
+ return ClientType.LITE_SIMPLE_CONSUMER;
82
+ }
83
+
84
+ protected onTopicRouteDataUpdate(topic: string, topicRouteData: TopicRouteData) {
85
+ super.onTopicRouteDataUpdate(topic, firstReadableMasterQueue(topicRouteData));
86
+ }
87
+
88
+ /**
89
+ * Start up the consumer.
90
+ *
91
+ * <p>This method initializes the consumer and starts the lite subscription
92
+ * manager. It must be called before the consumer can receive messages.</p>
93
+ */
94
+ async startup() {
95
+ await this.#liteSubscriptionManager.startConsumer(() => super.startup());
96
+ }
97
+
98
+ /**
99
+ * Shutdown the consumer.
100
+ *
101
+ * <p>This method gracefully shuts down the consumer, releasing all resources
102
+ * and stopping the lite subscription manager.</p>
103
+ */
104
+ async shutdown() {
105
+ await this.#liteSubscriptionManager.shutdownConsumer(() => super.shutdown());
106
+ }
107
+
108
+ async subscribeLite(liteTopic: string): Promise<void>;
109
+
110
+ /**
111
+ * Subscribe to a lite topic with an offset option to specify the consume
112
+ * from offset.
113
+ *
114
+ * @param liteTopic - The name of the lite topic to subscribe
115
+ * @param offsetOption - The consume from offset option
116
+ */
117
+ async subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
118
+
119
+ async subscribeLite(liteTopic: string, offsetOption?: OffsetOption): Promise<void> {
120
+ await this.#liteSubscriptionManager.subscribeLite(liteTopic, offsetOption);
121
+ }
122
+
123
+ /**
124
+ * Unsubscribe from a lite topic.
125
+ *
126
+ * @param liteTopic - The name of the lite topic to unsubscribe from
127
+ */
128
+ async unsubscribeLite(liteTopic: string): Promise<void> {
129
+ await this.#liteSubscriptionManager.unsubscribeLite(liteTopic);
130
+ }
131
+
132
+ /**
133
+ * Get the lite topic set.
134
+ *
135
+ * @return Set of currently subscribed lite topic names
136
+ */
137
+ getLiteTopicSet(): Set<string> {
138
+ return this.#liteSubscriptionManager.getLiteTopicSet();
139
+ }
140
+
141
+ /**
142
+ * Get the load balancing group for the consumer.
143
+ *
144
+ * @return Consumer group name
145
+ */
146
+ getConsumerGroup(): string {
147
+ return this.#liteSubscriptionManager.getConsumerGroupName();
148
+ }
149
+
150
+ /**
151
+ * Handle notify unsubscribe lite command from server.
152
+ *
153
+ * @param _endpoints - The server endpoints
154
+ * @param command - The unsubscribe command from the server
155
+ */
156
+ onNotifyUnsubscribeLiteCommand(_endpoints: Endpoints, command: NotifyUnsubscribeLiteCommand) {
157
+ this.#liteSubscriptionManager.onNotifyUnsubscribeLiteCommand(command);
158
+ }
159
+
160
+ /**
161
+ * Handle settings command from server.
162
+ *
163
+ * @param endpoints - The server endpoints
164
+ * @param settings - The settings configuration
165
+ */
166
+ onSettingsCommand(endpoints: Endpoints, settings: SettingsPB) {
167
+ super.onSettingsCommand(endpoints, settings);
168
+ this.#liteSubscriptionManager.sync(settings);
169
+ }
170
+
171
+ /**
172
+ * Close the consumer.
173
+ */
174
+ async close(): Promise<void> {
175
+ await this.shutdown();
176
+ }
177
+
178
+ /**
179
+ * Get the logger.
180
+ *
181
+ * @internal
182
+ */
183
+ getLogger(): ILogger {
184
+ return this.logger;
185
+ }
186
+
187
+ /**
188
+ * Get the RPC client manager (protected access for internal use).
189
+ *
190
+ * @internal
191
+ */
192
+ getRpcClientManager(): RpcClientManager {
193
+ return this.rpcClientManager;
194
+ }
195
+
196
+ /**
197
+ * Get the request timeout (protected access for internal use).
198
+ *
199
+ * @internal
200
+ */
201
+ getRequestTimeout(): number {
202
+ return this.requestTimeout;
203
+ }
204
+
205
+ /**
206
+ * Endpoints the lite subscription manager should sync to: every endpoint
207
+ * present in the consumed routes.
208
+ *
209
+ * @internal
210
+ */
211
+ getSyncEndpoints(): Endpoints[] {
212
+ return this.getTotalRouteEndpoints();
213
+ }
214
+ }
@@ -16,6 +16,7 @@
16
16
  */
17
17
 
18
18
  import { MessageView } from '../message';
19
+ import { MessageInterceptor } from '../hook';
19
20
  import { ConsumeService } from './ConsumeService';
20
21
  import { MessageListener } from './MessageListener';
21
22
  import type { ProcessQueue } from './ProcessQueue';
@@ -31,9 +32,10 @@ import { ILogger, getDefaultLogger } from '../client/Logger';
31
32
  export class LiteStandardConsumeService extends ConsumeService {
32
33
  readonly #logger: ILogger;
33
34
 
34
- constructor(clientId: string, messageListener: MessageListener) {
35
- super(clientId, messageListener);
36
- this.#logger = getDefaultLogger();
35
+ constructor(clientId: string, messageListener: MessageListener,
36
+ messageInterceptor?: MessageInterceptor, consumerGroup?: string, logger?: ILogger) {
37
+ super(clientId, messageListener, messageInterceptor, consumerGroup);
38
+ this.#logger = logger ?? getDefaultLogger();
37
39
  }
38
40
 
39
41
  consume(pq: ProcessQueue, messageViews: MessageView[]): void {