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.
- package/README.md +61 -0
- package/dist/client/BaseClient.d.ts +44 -1
- package/dist/client/BaseClient.js +353 -85
- package/dist/client/Logger.js +9 -6
- package/dist/client/RpcClient.js +42 -134
- package/dist/client/RpcClientManager.d.ts +22 -17
- package/dist/client/RpcClientManager.js +61 -56
- package/dist/client/TelemetrySession.js +107 -17
- package/dist/consumer/ConsumeService.d.ts +2 -1
- package/dist/consumer/ConsumeService.js +12 -6
- package/dist/consumer/ConsumeTask.d.ts +6 -1
- package/dist/consumer/ConsumeTask.js +36 -5
- package/dist/consumer/Consumer.d.ts +13 -5
- package/dist/consumer/Consumer.js +94 -33
- package/dist/consumer/FifoConsumeService.d.ts +3 -1
- package/dist/consumer/FifoConsumeService.js +4 -4
- package/dist/consumer/LiteConsumerSupport.d.ts +25 -0
- package/dist/consumer/LiteConsumerSupport.js +47 -0
- package/dist/consumer/LiteFifoConsumeService.d.ts +3 -1
- package/dist/consumer/LiteFifoConsumeService.js +4 -4
- package/dist/consumer/LitePushConsumerImpl.d.ts +15 -4
- package/dist/consumer/LitePushConsumerImpl.js +26 -30
- package/dist/consumer/LiteSimpleConsumer.d.ts +177 -0
- package/dist/consumer/LiteSimpleConsumer.js +126 -0
- package/dist/consumer/LiteSimpleConsumerImpl.d.ts +133 -0
- package/dist/consumer/LiteSimpleConsumerImpl.js +179 -0
- package/dist/consumer/LiteStandardConsumeService.d.ts +3 -1
- package/dist/consumer/LiteStandardConsumeService.js +4 -4
- package/dist/consumer/LiteSubscriptionManager.d.ts +27 -6
- package/dist/consumer/LiteSubscriptionManager.js +36 -27
- package/dist/consumer/OffsetOption.d.ts +1 -0
- package/dist/consumer/OffsetOption.js +10 -4
- package/dist/consumer/ProcessQueue.d.ts +17 -1
- package/dist/consumer/ProcessQueue.js +134 -91
- package/dist/consumer/PushConsumer.d.ts +17 -0
- package/dist/consumer/PushConsumer.js +117 -26
- package/dist/consumer/PushConsumerGaugeObserver.d.ts +33 -0
- package/dist/consumer/PushConsumerGaugeObserver.js +60 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +8 -1
- package/dist/consumer/PushSubscriptionSettings.js +35 -8
- package/dist/consumer/SimpleConsumer.d.ts +5 -0
- package/dist/consumer/SimpleConsumer.js +33 -20
- package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
- package/dist/consumer/StandardConsumeService.d.ts +3 -1
- package/dist/consumer/StandardConsumeService.js +17 -8
- package/dist/consumer/SubscriptionLoadBalancer.js +3 -3
- package/dist/consumer/index.d.ts +1 -0
- package/dist/consumer/index.js +2 -1
- package/dist/exception/InternalErrorException.d.ts +1 -0
- package/dist/exception/InternalErrorException.js +3 -2
- package/dist/exception/PayloadEmptyException.d.ts +20 -0
- package/dist/exception/PayloadEmptyException.js +28 -0
- package/dist/exception/StatusChecker.js +11 -4
- package/dist/exception/index.d.ts +1 -0
- package/dist/exception/index.js +2 -1
- package/dist/exception/receiveStatus.d.ts +18 -0
- package/dist/exception/receiveStatus.js +28 -0
- package/dist/hook/Attribute.d.ts +26 -0
- package/dist/hook/Attribute.js +37 -0
- package/dist/hook/AttributeKey.d.ts +32 -0
- package/dist/hook/AttributeKey.js +43 -0
- package/dist/hook/CompositedMessageInterceptor.d.ts +32 -0
- package/dist/hook/CompositedMessageInterceptor.js +87 -0
- package/dist/hook/InflightRequestCountInterceptor.d.ts +28 -0
- package/dist/hook/InflightRequestCountInterceptor.js +44 -0
- package/dist/hook/MessageHookPoints.d.ts +54 -0
- package/dist/hook/MessageHookPoints.js +59 -0
- package/dist/hook/MessageHookPointsStatus.d.ts +24 -0
- package/dist/hook/MessageHookPointsStatus.js +29 -0
- package/dist/hook/MessageInterceptor.d.ts +34 -0
- package/dist/hook/MessageInterceptor.js +19 -0
- package/dist/hook/MessageInterceptorContext.d.ts +31 -0
- package/dist/hook/MessageInterceptorContext.js +19 -0
- package/dist/hook/MessageInterceptorContextImpl.d.ts +40 -0
- package/dist/hook/MessageInterceptorContextImpl.js +71 -0
- package/dist/hook/MessageMeterInterceptor.d.ts +39 -0
- package/dist/hook/MessageMeterInterceptor.js +149 -0
- package/dist/hook/index.d.ts +26 -0
- package/dist/hook/index.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/message/Message.js +26 -4
- package/dist/message/MessageView.js +15 -5
- package/dist/message/PublishingMessage.js +4 -4
- package/dist/metrics/ClientMeter.d.ts +42 -0
- package/dist/metrics/ClientMeter.js +75 -0
- package/dist/metrics/ClientMeterManager.d.ts +46 -0
- package/dist/metrics/ClientMeterManager.js +203 -0
- package/dist/metrics/EmptyGaugeObserver.d.ts +28 -0
- package/dist/metrics/EmptyGaugeObserver.js +34 -0
- package/dist/metrics/GaugeEnum.d.ts +32 -0
- package/dist/metrics/GaugeEnum.js +37 -0
- package/dist/metrics/GaugeObserver.d.ts +29 -0
- package/dist/metrics/GaugeObserver.js +19 -0
- package/dist/metrics/HistogramEnum.d.ts +54 -0
- package/dist/metrics/HistogramEnum.js +71 -0
- package/dist/metrics/InvocationStatus.d.ts +26 -0
- package/dist/metrics/InvocationStatus.js +33 -0
- package/dist/metrics/Metric.d.ts +30 -0
- package/dist/metrics/Metric.js +44 -0
- package/dist/metrics/MetricLabels.d.ts +27 -0
- package/dist/metrics/MetricLabels.js +34 -0
- package/dist/metrics/index.d.ts +25 -0
- package/dist/metrics/index.js +42 -0
- package/dist/producer/Producer.d.ts +8 -2
- package/dist/producer/Producer.js +83 -34
- package/dist/producer/PublishingLoadBalancer.js +11 -5
- package/dist/producer/PublishingSettings.js +1 -3
- package/dist/producer/SendReceipt.js +3 -2
- package/dist/producer/Transaction.d.ts +2 -1
- package/dist/producer/Transaction.js +9 -8
- package/dist/producer/index.d.ts +1 -0
- package/dist/producer/index.js +2 -2
- package/dist/retry/CustomizedBackoffRetryPolicy.d.ts +37 -0
- package/dist/retry/CustomizedBackoffRetryPolicy.js +72 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +10 -6
- package/dist/retry/RetryPolicy.d.ts +1 -1
- package/dist/retry/index.d.ts +1 -0
- package/dist/retry/index.js +2 -1
- package/dist/route/Endpoints.d.ts +6 -0
- package/dist/route/Endpoints.js +69 -9
- package/dist/route/IpNameResolver.d.ts +28 -0
- package/dist/route/IpNameResolver.js +104 -0
- package/dist/route/MessageQueue.js +7 -2
- package/dist/route/TopicRouteData.d.ts +5 -0
- package/dist/route/TopicRouteData.js +22 -1
- package/dist/util/Semaphore.d.ts +47 -0
- package/dist/util/Semaphore.js +82 -0
- package/dist/util/index.d.ts +1 -1
- package/dist/util/index.js +9 -6
- package/dist/util/rpcContext.d.ts +20 -0
- package/dist/util/rpcContext.js +35 -0
- package/package.json +11 -4
- package/src/client/BaseClient.ts +392 -91
- package/src/client/Logger.ts +9 -5
- package/src/client/RpcClient.ts +62 -133
- package/src/client/RpcClientManager.ts +103 -60
- package/src/client/TelemetrySession.ts +112 -17
- package/src/consumer/ConsumeService.ts +16 -5
- package/src/consumer/ConsumeTask.ts +43 -3
- package/src/consumer/Consumer.ts +116 -35
- package/src/consumer/FifoConsumeService.ts +5 -3
- package/src/consumer/LiteConsumerSupport.ts +49 -0
- package/src/consumer/LiteFifoConsumeService.ts +5 -3
- package/src/consumer/LitePushConsumerImpl.ts +34 -23
- package/src/consumer/LiteSimpleConsumer.ts +251 -0
- package/src/consumer/LiteSimpleConsumerImpl.ts +214 -0
- package/src/consumer/LiteStandardConsumeService.ts +5 -3
- package/src/consumer/LiteSubscriptionManager.ts +66 -41
- package/src/consumer/OffsetOption.ts +10 -3
- package/src/consumer/ProcessQueue.ts +165 -107
- package/src/consumer/PushConsumer.ts +133 -24
- package/src/consumer/PushConsumerGaugeObserver.ts +60 -0
- package/src/consumer/PushSubscriptionSettings.ts +40 -9
- package/src/consumer/SimpleConsumer.ts +39 -23
- package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
- package/src/consumer/StandardConsumeService.ts +18 -7
- package/src/consumer/SubscriptionLoadBalancer.ts +2 -2
- package/src/consumer/index.ts +1 -0
- package/src/exception/InternalErrorException.ts +3 -1
- package/src/exception/PayloadEmptyException.ts +25 -0
- package/src/exception/StatusChecker.ts +10 -2
- package/src/exception/index.ts +1 -0
- package/src/exception/receiveStatus.ts +25 -0
- package/src/hook/Attribute.ts +36 -0
- package/src/hook/AttributeKey.ts +42 -0
- package/src/hook/CompositedMessageInterceptor.ts +96 -0
- package/src/hook/InflightRequestCountInterceptor.ts +46 -0
- package/src/hook/MessageHookPoints.ts +55 -0
- package/src/hook/MessageHookPointsStatus.ts +25 -0
- package/src/hook/MessageInterceptor.ts +35 -0
- package/src/hook/MessageInterceptorContext.ts +33 -0
- package/src/hook/MessageInterceptorContextImpl.ts +83 -0
- package/src/hook/MessageMeterInterceptor.ts +162 -0
- package/src/hook/index.ts +27 -0
- package/src/index.ts +1 -0
- package/src/message/Message.ts +25 -3
- package/src/message/MessageView.ts +13 -4
- package/src/message/PublishingMessage.ts +3 -3
- package/src/metrics/ClientMeter.ts +81 -0
- package/src/metrics/ClientMeterManager.ts +227 -0
- package/src/metrics/EmptyGaugeObserver.ts +36 -0
- package/src/metrics/GaugeEnum.ts +33 -0
- package/src/metrics/GaugeObserver.ts +32 -0
- package/src/metrics/HistogramEnum.ts +69 -0
- package/src/metrics/InvocationStatus.ts +28 -0
- package/src/metrics/Metric.ts +42 -0
- package/src/metrics/MetricLabels.ts +31 -0
- package/src/metrics/index.ts +26 -0
- package/src/producer/Producer.ts +94 -38
- package/src/producer/PublishingLoadBalancer.ts +10 -4
- package/src/producer/PublishingSettings.ts +0 -2
- package/src/producer/SendReceipt.ts +2 -1
- package/src/producer/Transaction.ts +12 -11
- package/src/producer/index.ts +1 -1
- package/src/retry/CustomizedBackoffRetryPolicy.ts +81 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +11 -6
- package/src/retry/RetryPolicy.ts +1 -1
- package/src/retry/index.ts +1 -0
- package/src/route/Endpoints.ts +65 -8
- package/src/route/IpNameResolver.ts +119 -0
- package/src/route/MessageQueue.ts +6 -1
- package/src/route/TopicRouteData.ts +22 -1
- package/src/util/Semaphore.ts +82 -0
- package/src/util/index.ts +8 -5
- package/src/util/rpcContext.ts +34 -0
|
@@ -16,17 +16,20 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { MessageView } from '../message';
|
|
19
|
+
import { MessageInterceptor } from '../hook';
|
|
19
20
|
import { ConsumeService } from './ConsumeService';
|
|
21
|
+
import { ConsumeResult } from './ConsumeResult';
|
|
20
22
|
import { MessageListener } from './MessageListener';
|
|
21
23
|
import type { ProcessQueue } from './ProcessQueue';
|
|
22
|
-
import { getDefaultLogger } from '../client';
|
|
24
|
+
import { ILogger, getDefaultLogger } from '../client/Logger';
|
|
23
25
|
|
|
24
26
|
export class StandardConsumeService extends ConsumeService {
|
|
25
|
-
private readonly logger
|
|
27
|
+
private readonly logger: ILogger;
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
super(clientId, messageListener);
|
|
29
|
+
constructor(clientId: string, messageListener: MessageListener,
|
|
30
|
+
messageInterceptor?: MessageInterceptor, consumerGroup?: string, logger?: ILogger) {
|
|
31
|
+
super(clientId, messageListener, messageInterceptor, consumerGroup);
|
|
32
|
+
this.logger = logger ?? getDefaultLogger();
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
consume(pq: ProcessQueue, messageViews: MessageView[]): void {
|
|
@@ -48,9 +51,17 @@ export class StandardConsumeService extends ConsumeService {
|
|
|
48
51
|
pq.eraseMessage(messageView, result);
|
|
49
52
|
})
|
|
50
53
|
.catch(err => {
|
|
51
|
-
|
|
54
|
+
// consumeMessage never rejects, so this is a settlement that threw synchronously.
|
|
55
|
+
// Nack the message so it still leaves the cache and releases its permit; otherwise
|
|
56
|
+
// it would hold the permit until shutdown times out waiting for it.
|
|
57
|
+
this.logger.error('Failed to settle the consumed message, nack it instead, messageId=%s, error=%s',
|
|
52
58
|
messageView.messageId, err);
|
|
53
|
-
|
|
59
|
+
try {
|
|
60
|
+
pq.eraseMessage(messageView, ConsumeResult.FAILURE);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
this.logger.error('Failed to nack the message after the settlement failure, messageId=%s, error=%s',
|
|
63
|
+
messageView.messageId, e);
|
|
64
|
+
}
|
|
54
65
|
});
|
|
55
66
|
}
|
|
56
67
|
}
|
|
@@ -26,12 +26,12 @@ export class SubscriptionLoadBalancer {
|
|
|
26
26
|
|
|
27
27
|
constructor(topicRouteData: TopicRouteData, index?: number) {
|
|
28
28
|
this.#messageQueues = topicRouteData.messageQueues.filter(mq => {
|
|
29
|
-
return mq.
|
|
29
|
+
return mq.broker.id === MASTER_BROKER_ID && (mq.permission === Permission.READ || mq.permission === Permission.READ_WRITE);
|
|
30
30
|
});
|
|
31
|
-
this.#index = index === undefined ? randomInt(this.#messageQueues.length) : index;
|
|
32
31
|
if (this.#messageQueues.length === 0) {
|
|
33
32
|
throw new Error(`No readable message queue found, topicRouteData=${JSON.stringify(topicRouteData)}`);
|
|
34
33
|
}
|
|
34
|
+
this.#index = index === undefined ? randomInt(this.#messageQueues.length) : index;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
update(topicRouteData: TopicRouteData) {
|
package/src/consumer/index.ts
CHANGED
|
@@ -34,6 +34,7 @@ export * from './LiteStandardConsumeService';
|
|
|
34
34
|
export * from './ProcessQueue';
|
|
35
35
|
export * from './PushConsumer';
|
|
36
36
|
export * from './LitePushConsumer';
|
|
37
|
+
export * from './LiteSimpleConsumer';
|
|
37
38
|
export * from './OffsetOption';
|
|
38
39
|
export { LitePushConsumerImpl } from './LitePushConsumerImpl';
|
|
39
40
|
export { LiteSubscriptionManager } from './LiteSubscriptionManager';
|
|
@@ -18,11 +18,13 @@
|
|
|
18
18
|
import { ClientException } from './ClientException';
|
|
19
19
|
|
|
20
20
|
export class InternalErrorException extends ClientException {
|
|
21
|
+
readonly retryable = true;
|
|
22
|
+
|
|
21
23
|
constructor(code: number, message: string, requestId?: string) {
|
|
22
24
|
super(code, message, requestId);
|
|
23
25
|
this.name = 'InternalErrorException';
|
|
24
26
|
// Mark this as a potentially retryable error (temporary server-side issue)
|
|
25
|
-
|
|
27
|
+
// The class field explicitly declares retryable.
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
/**
|
|
@@ -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 PayloadEmptyException extends ClientException {
|
|
21
|
+
constructor(code: number, message: string, requestId?: string) {
|
|
22
|
+
super(code, message, requestId);
|
|
23
|
+
this.name = 'PayloadEmptyException';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -19,8 +19,10 @@ import { Status, Code } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
|
19
19
|
import { BadRequestException } from './BadRequestException';
|
|
20
20
|
import { ForbiddenException } from './ForbiddenException';
|
|
21
21
|
import { InternalErrorException } from './InternalErrorException';
|
|
22
|
+
import { LiteSubscriptionQuotaExceededException } from './LiteSubscriptionQuotaExceededException';
|
|
22
23
|
import { LiteTopicQuotaExceededException } from './LiteTopicQuotaExceededException';
|
|
23
24
|
import { NotFoundException } from './NotFoundException';
|
|
25
|
+
import { PayloadEmptyException } from './PayloadEmptyException';
|
|
24
26
|
import { PayloadTooLargeException } from './PayloadTooLargeException';
|
|
25
27
|
import { PaymentRequiredException } from './PaymentRequiredException';
|
|
26
28
|
import { ProxyTimeoutException } from './ProxyTimeoutException';
|
|
@@ -31,7 +33,9 @@ import { UnsupportedException } from './UnsupportedException';
|
|
|
31
33
|
|
|
32
34
|
export class StatusChecker {
|
|
33
35
|
static check(status?: Status.AsObject, requestId?: string) {
|
|
34
|
-
if (!status)
|
|
36
|
+
if (!status) {
|
|
37
|
+
throw new InternalErrorException(Code.INTERNAL_SERVER_ERROR, 'status was not set by server', requestId);
|
|
38
|
+
}
|
|
35
39
|
switch (status.code) {
|
|
36
40
|
case Code.OK:
|
|
37
41
|
case Code.MULTIPLE_RESULTS:
|
|
@@ -44,6 +48,7 @@ export class StatusChecker {
|
|
|
44
48
|
case Code.ILLEGAL_MESSAGE_KEY:
|
|
45
49
|
case Code.ILLEGAL_MESSAGE_GROUP:
|
|
46
50
|
case Code.ILLEGAL_MESSAGE_PROPERTY_KEY:
|
|
51
|
+
case Code.ILLEGAL_LITE_TOPIC:
|
|
47
52
|
case Code.INVALID_TRANSACTION_ID:
|
|
48
53
|
case Code.ILLEGAL_MESSAGE_ID:
|
|
49
54
|
case Code.ILLEGAL_FILTER_EXPRESSION:
|
|
@@ -63,7 +68,6 @@ export class StatusChecker {
|
|
|
63
68
|
case Code.FORBIDDEN:
|
|
64
69
|
throw new ForbiddenException(status.code, status.message, requestId);
|
|
65
70
|
case Code.MESSAGE_NOT_FOUND:
|
|
66
|
-
return;
|
|
67
71
|
case Code.NOT_FOUND:
|
|
68
72
|
case Code.TOPIC_NOT_FOUND:
|
|
69
73
|
case Code.CONSUMER_GROUP_NOT_FOUND:
|
|
@@ -71,6 +75,8 @@ export class StatusChecker {
|
|
|
71
75
|
case Code.PAYLOAD_TOO_LARGE:
|
|
72
76
|
case Code.MESSAGE_BODY_TOO_LARGE:
|
|
73
77
|
throw new PayloadTooLargeException(status.code, status.message, requestId);
|
|
78
|
+
case Code.MESSAGE_BODY_EMPTY:
|
|
79
|
+
throw new PayloadEmptyException(status.code, status.message, requestId);
|
|
74
80
|
case Code.TOO_MANY_REQUESTS:
|
|
75
81
|
throw new TooManyRequestsException(status.code, status.message, requestId);
|
|
76
82
|
case Code.REQUEST_HEADER_FIELDS_TOO_LARGE:
|
|
@@ -90,6 +96,8 @@ export class StatusChecker {
|
|
|
90
96
|
throw new UnsupportedException(status.code, status.message, requestId);
|
|
91
97
|
case Code.LITE_TOPIC_QUOTA_EXCEEDED:
|
|
92
98
|
throw new LiteTopicQuotaExceededException(status.code, status.message || '', requestId);
|
|
99
|
+
case Code.LITE_SUBSCRIPTION_QUOTA_EXCEEDED:
|
|
100
|
+
throw new LiteSubscriptionQuotaExceededException(status.code, requestId ?? null, status.message || '');
|
|
93
101
|
default:
|
|
94
102
|
throw new UnsupportedException(status.code, status.message, requestId);
|
|
95
103
|
}
|
package/src/exception/index.ts
CHANGED
|
@@ -22,6 +22,7 @@ export * from './InternalErrorException';
|
|
|
22
22
|
export * from './LiteSubscriptionQuotaExceededException';
|
|
23
23
|
export * from './LiteTopicQuotaExceededException';
|
|
24
24
|
export * from './NotFoundException';
|
|
25
|
+
export * from './PayloadEmptyException';
|
|
25
26
|
export * from './PayloadTooLargeException';
|
|
26
27
|
export * from './PaymentRequiredException';
|
|
27
28
|
export * from './ProxyTimeoutException';
|
|
@@ -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 { Code, Status } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { StatusChecker } from './StatusChecker';
|
|
20
|
+
|
|
21
|
+
// Allow empty results only for receive operations without changing the public StatusChecker.check signature.
|
|
22
|
+
export function checkReceiveStatus(status?: Status.AsObject, requestId?: string) {
|
|
23
|
+
if (status?.code === Code.MESSAGE_NOT_FOUND) return;
|
|
24
|
+
StatusChecker.check(status, requestId);
|
|
25
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* Immutable value holder shared through a {@link MessageInterceptorContext},
|
|
20
|
+
* mirroring org.apache.rocketmq.client.java.hook.Attribute.
|
|
21
|
+
*/
|
|
22
|
+
export class Attribute<T> {
|
|
23
|
+
readonly #value: T;
|
|
24
|
+
|
|
25
|
+
private constructor(value: T) {
|
|
26
|
+
this.#value = value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static create<T>(value: T): Attribute<T> {
|
|
30
|
+
return new Attribute(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get(): T {
|
|
34
|
+
return this.#value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -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
|
+
/**
|
|
19
|
+
* Typed key of a context attribute, mirroring
|
|
20
|
+
* org.apache.rocketmq.client.java.hook.AttributeKey.
|
|
21
|
+
*
|
|
22
|
+
* The type parameter is only used for compile-time typing of the associated value.
|
|
23
|
+
*/
|
|
24
|
+
export class AttributeKey<T> {
|
|
25
|
+
readonly name: string;
|
|
26
|
+
/**
|
|
27
|
+
* Never assigned at runtime; it only carries the value type for compile-time typing.
|
|
28
|
+
*/
|
|
29
|
+
readonly typeHint?: T;
|
|
30
|
+
|
|
31
|
+
constructor(name: string) {
|
|
32
|
+
this.name = name;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static create<T>(name: string): AttributeKey<T> {
|
|
36
|
+
return new AttributeKey<T>(name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toString() {
|
|
40
|
+
return `AttributeKey(${this.name})`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
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 type { ILogger } from '../client/Logger';
|
|
19
|
+
import { Attribute } from './Attribute';
|
|
20
|
+
import { AttributeKey } from './AttributeKey';
|
|
21
|
+
import { MessageHookPointsStatus } from './MessageHookPointsStatus';
|
|
22
|
+
import { MessageInterceptorContext } from './MessageInterceptorContext';
|
|
23
|
+
import { MessageInterceptorContextImpl } from './MessageInterceptorContextImpl';
|
|
24
|
+
import { GeneralMessage, MessageInterceptor } from './MessageInterceptor';
|
|
25
|
+
|
|
26
|
+
// Key under which per-interceptor attributes are stashed between doBefore and doAfter.
|
|
27
|
+
const INTERCEPTOR_ATTRIBUTES_KEY = AttributeKey
|
|
28
|
+
.create<Map<number, Map<AttributeKey<any>, Attribute<any>>>>('composited_interceptor_attributes');
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Composite interceptor which delegates to a chain of interceptors: doBefore runs
|
|
32
|
+
* in registration order while doAfter runs in reverse order, mirroring
|
|
33
|
+
* org.apache.rocketmq.client.java.hook.CompositedMessageInterceptor.
|
|
34
|
+
*/
|
|
35
|
+
export class CompositedMessageInterceptor implements MessageInterceptor {
|
|
36
|
+
readonly #interceptors: MessageInterceptor[] = [];
|
|
37
|
+
readonly #logger?: ILogger;
|
|
38
|
+
|
|
39
|
+
constructor(interceptors: MessageInterceptor[] = [], logger?: ILogger) {
|
|
40
|
+
this.#interceptors.push(...interceptors);
|
|
41
|
+
this.#logger = logger;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
addInterceptor(interceptor: MessageInterceptor) {
|
|
45
|
+
this.#interceptors.push(interceptor);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get size() {
|
|
49
|
+
return this.#interceptors.length;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
doBefore(context0: MessageInterceptorContext, messages: GeneralMessage[]) {
|
|
53
|
+
const attributeMap = new Map<number, Map<AttributeKey<any>, Attribute<any>>>();
|
|
54
|
+
// A single cumulative context is shared by the whole chain, mirroring the Java
|
|
55
|
+
// implementation, so that later interceptors observe what earlier ones recorded.
|
|
56
|
+
const context = new MessageInterceptorContextImpl(context0.getMessageHookPoints(), context0.getStatus());
|
|
57
|
+
for (let index = 0; index < this.#interceptors.length; index++) {
|
|
58
|
+
for (const [ key, value ] of context0.getAttributes()) {
|
|
59
|
+
context.putAttribute(key, value);
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
this.#interceptors[index].doBefore(context, messages);
|
|
63
|
+
} catch (t) {
|
|
64
|
+
// Interceptors must never break the message pipeline. Standalone instances without
|
|
65
|
+
// a client logger fall back to the console.
|
|
66
|
+
// eslint-disable-next-line no-console
|
|
67
|
+
(this.#logger ?? console).error(
|
|
68
|
+
'[CompositedMessageInterceptor] Exception raised while handling messages before hook point=%s, error=%s',
|
|
69
|
+
context0.getMessageHookPoints(), t);
|
|
70
|
+
}
|
|
71
|
+
attributeMap.set(index, context.getAttributes());
|
|
72
|
+
}
|
|
73
|
+
context0.putAttribute(INTERCEPTOR_ATTRIBUTES_KEY, Attribute.create(attributeMap));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
doAfter(context0: MessageInterceptorContext, messages: GeneralMessage[]) {
|
|
77
|
+
const attributeMap = context0.getAttribute(INTERCEPTOR_ATTRIBUTES_KEY)?.get()
|
|
78
|
+
?? new Map<number, Map<AttributeKey<any>, Attribute<any>>>();
|
|
79
|
+
for (let index = this.#interceptors.length - 1; index >= 0; index--) {
|
|
80
|
+
const attributes = attributeMap.get(index);
|
|
81
|
+
const context = new MessageInterceptorContextImpl(
|
|
82
|
+
context0.getMessageHookPoints(), context0.getStatus() ?? MessageHookPointsStatus.OK, attributes);
|
|
83
|
+
for (const [ key, value ] of context0.getAttributes()) {
|
|
84
|
+
context.putAttribute(key, value);
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
this.#interceptors[index].doAfter(context, messages);
|
|
88
|
+
} catch (t) {
|
|
89
|
+
// eslint-disable-next-line no-console
|
|
90
|
+
(this.#logger ?? console).error(
|
|
91
|
+
'[CompositedMessageInterceptor] Exception raised while handling messages after hook point=%s, error=%s',
|
|
92
|
+
context0.getMessageHookPoints(), t);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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 { MessageHookPoints } from './MessageHookPoints';
|
|
19
|
+
import { MessageInterceptorContext } from './MessageInterceptorContext';
|
|
20
|
+
import { GeneralMessage, MessageInterceptor } from './MessageInterceptor';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Interceptor which counts the in-flight receive requests, mirroring
|
|
24
|
+
* org.apache.rocketmq.client.java.hook.InflightRequestCountInterceptor.
|
|
25
|
+
*/
|
|
26
|
+
export class InflightRequestCountInterceptor implements MessageInterceptor {
|
|
27
|
+
#inflightReceiveRequestCount = 0;
|
|
28
|
+
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
+
doBefore(context: MessageInterceptorContext, _messages: GeneralMessage[]) {
|
|
31
|
+
if (context.getMessageHookPoints() === MessageHookPoints.RECEIVE) {
|
|
32
|
+
this.#inflightReceiveRequestCount++;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
37
|
+
doAfter(context: MessageInterceptorContext, _messages: GeneralMessage[]) {
|
|
38
|
+
if (context.getMessageHookPoints() === MessageHookPoints.RECEIVE) {
|
|
39
|
+
this.#inflightReceiveRequestCount--;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getInflightReceiveRequestCount(): number {
|
|
44
|
+
return this.#inflightReceiveRequestCount;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* Hook points at which {@link MessageInterceptor}s are invoked, mirroring
|
|
20
|
+
* org.apache.rocketmq.client.java.hook.MessageHookPoints.
|
|
21
|
+
*/
|
|
22
|
+
export enum MessageHookPoints {
|
|
23
|
+
/**
|
|
24
|
+
* The hook point of message publishing.
|
|
25
|
+
*/
|
|
26
|
+
SEND = 'SEND',
|
|
27
|
+
/**
|
|
28
|
+
* The hook point of message reception.
|
|
29
|
+
*/
|
|
30
|
+
RECEIVE = 'RECEIVE',
|
|
31
|
+
/**
|
|
32
|
+
* The hook point of message consumption.
|
|
33
|
+
*/
|
|
34
|
+
CONSUME = 'CONSUME',
|
|
35
|
+
/**
|
|
36
|
+
* The hook point of message ack acknowledgement.
|
|
37
|
+
*/
|
|
38
|
+
ACK = 'ACK',
|
|
39
|
+
/**
|
|
40
|
+
* The hook point of message changing invisible duration.
|
|
41
|
+
*/
|
|
42
|
+
CHANGE_INVISIBLE_DURATION = 'CHANGE_INVISIBLE_DURATION',
|
|
43
|
+
/**
|
|
44
|
+
* The hook point of message transaction commit.
|
|
45
|
+
*/
|
|
46
|
+
COMMIT_TRANSACTION = 'COMMIT_TRANSACTION',
|
|
47
|
+
/**
|
|
48
|
+
* The hook point of message transaction rollback.
|
|
49
|
+
*/
|
|
50
|
+
ROLLBACK_TRANSACTION = 'ROLLBACK_TRANSACTION',
|
|
51
|
+
/**
|
|
52
|
+
* The hook point of forwarding message to DLQ.
|
|
53
|
+
*/
|
|
54
|
+
FORWARD_TO_DLQ = 'FORWARD_TO_DLQ',
|
|
55
|
+
}
|
|
@@ -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
|
+
/**
|
|
19
|
+
* Status of a hook point invocation, mirroring
|
|
20
|
+
* org.apache.rocketmq.client.java.hook.MessageHookPointsStatus.
|
|
21
|
+
*/
|
|
22
|
+
export enum MessageHookPointsStatus {
|
|
23
|
+
OK = 'OK',
|
|
24
|
+
ERROR = 'ERROR',
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { MessageView } from '../message/MessageView';
|
|
19
|
+
import { MessageInterceptorContext } from './MessageInterceptorContext';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Message passed to interceptors: either an outgoing message or a received one,
|
|
23
|
+
* mirroring org.apache.rocketmq.client.java.message.GeneralMessage.
|
|
24
|
+
*/
|
|
25
|
+
export type GeneralMessage = MessageView | { topic: string };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Interface for intercepting ingoing/outgoing message before/after they are
|
|
29
|
+
* dispatched by client, mirroring
|
|
30
|
+
* org.apache.rocketmq.client.java.hook.MessageInterceptor.
|
|
31
|
+
*/
|
|
32
|
+
export interface MessageInterceptor {
|
|
33
|
+
doBefore(context: MessageInterceptorContext, messages: GeneralMessage[]): void;
|
|
34
|
+
doAfter(context: MessageInterceptorContext, messages: GeneralMessage[]): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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 { Attribute } from './Attribute';
|
|
19
|
+
import { AttributeKey } from './AttributeKey';
|
|
20
|
+
import { MessageHookPoints } from './MessageHookPoints';
|
|
21
|
+
import { MessageHookPointsStatus } from './MessageHookPointsStatus';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Context carried through the message interceptor chain, mirroring
|
|
25
|
+
* org.apache.rocketmq.client.java.hook.MessageInterceptorContext.
|
|
26
|
+
*/
|
|
27
|
+
export interface MessageInterceptorContext {
|
|
28
|
+
getMessageHookPoints(): MessageHookPoints;
|
|
29
|
+
getStatus(): MessageHookPointsStatus | undefined;
|
|
30
|
+
putAttribute<T>(key: AttributeKey<T>, value: Attribute<T>): void;
|
|
31
|
+
getAttribute<T>(key: AttributeKey<T>): Attribute<T> | undefined;
|
|
32
|
+
getAttributes(): Map<AttributeKey<any>, Attribute<any>>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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 { Attribute } from './Attribute';
|
|
19
|
+
import { AttributeKey } from './AttributeKey';
|
|
20
|
+
import { MessageHookPoints } from './MessageHookPoints';
|
|
21
|
+
import { MessageHookPointsStatus } from './MessageHookPointsStatus';
|
|
22
|
+
import { MessageInterceptorContext } from './MessageInterceptorContext';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Default {@link MessageInterceptorContext} implementation, mirroring
|
|
26
|
+
* org.apache.rocketmq.client.java.hook.MessageInterceptorContextImpl.
|
|
27
|
+
*/
|
|
28
|
+
export class MessageInterceptorContextImpl implements MessageInterceptorContext {
|
|
29
|
+
readonly #messageHookPoints: MessageHookPoints;
|
|
30
|
+
readonly #status: MessageHookPointsStatus | undefined;
|
|
31
|
+
readonly #attributes: Map<AttributeKey<any>, Attribute<any>>;
|
|
32
|
+
|
|
33
|
+
constructor(messageHookPoints: MessageHookPoints,
|
|
34
|
+
status?: MessageHookPointsStatus,
|
|
35
|
+
attributes?: Map<AttributeKey<any>, Attribute<any>>) {
|
|
36
|
+
this.#messageHookPoints = messageHookPoints;
|
|
37
|
+
this.#status = status;
|
|
38
|
+
this.#attributes = new Map();
|
|
39
|
+
for (const [ key, value ] of attributes ?? []) {
|
|
40
|
+
this.putAttribute(key, value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Derive a new context from an existing one with the given status, e.g. once the
|
|
46
|
+
* outcome of the intercepted operation is known.
|
|
47
|
+
*/
|
|
48
|
+
static withStatus(context: MessageInterceptorContext, status: MessageHookPointsStatus) {
|
|
49
|
+
return new MessageInterceptorContextImpl(context.getMessageHookPoints(), status, context.getAttributes());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getMessageHookPoints(): MessageHookPoints {
|
|
53
|
+
return this.#messageHookPoints;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getStatus(): MessageHookPointsStatus | undefined {
|
|
57
|
+
return this.#status;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
putAttribute<T>(key: AttributeKey<T>, value: Attribute<T>) {
|
|
61
|
+
// Java compares AttributeKeys by name; preserve the public Map<AttributeKey, Attribute> representation.
|
|
62
|
+
const existing = this.findKey(key);
|
|
63
|
+
this.#attributes.set(existing ?? key, value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getAttribute<T>(key: AttributeKey<T>): Attribute<T> | undefined {
|
|
67
|
+
const existing = this.findKey(key);
|
|
68
|
+
return existing ? this.#attributes.get(existing) as Attribute<T> | undefined : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private findKey<T>(key: AttributeKey<T>): AttributeKey<T> | undefined {
|
|
72
|
+
for (const candidate of this.#attributes.keys()) {
|
|
73
|
+
if (candidate.name === key.name) {
|
|
74
|
+
return candidate;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getAttributes(): Map<AttributeKey<any>, Attribute<any>> {
|
|
81
|
+
return this.#attributes;
|
|
82
|
+
}
|
|
83
|
+
}
|