forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.ts
200 lines (182 loc) · 7.37 KB
/
receiver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as log from "./log";
import { ConnectionContext } from './connectionContext';
import { ReceiveOptions } from './eventHubClient';
import { OnMessage, OnError, LastEnqueuedInfo } from "./eventHubReceiver";
import { ReceivedEventData } from "./eventData";
import { MessagingError, Constants } from "@azure/amqp-common";
import { StreamingReceiver, ReceiveHandler } from "./streamingReceiver";
import { BatchingReceiver } from "./batchingReceiver";
import { Aborter } from './aborter';
/**
* The Receiver class can be used to receive messages in a batch or by registering handlers.
* Use the `createReceiver` function on the QueueClient or SubscriptionClient to instantiate a Receiver.
* The Receiver class is an abstraction over the underlying AMQP receiver link.
* @class Receiver
*/
export class Receiver {
/**
* @property Describes the amqp connection context for the QueueClient.
*/
private _context: ConnectionContext;
/**
* @property {boolean} [_isClosed] Denotes if close() was called on this receiver
*/
private _isClosed: boolean = false;
private _partitionId: string;
private _receiveOptions: ReceiveOptions;
/**
* @property Returns `true` if the receiver is closed. This can happen either because the receiver
* itself has been closed or the client that created it has been closed.
* @readonly
*/
public get isClosed(): boolean {
return this._isClosed;
}
/**
* @property Returns `true` if the receiver is closed. This can happen either because the receiver
* itself has been closed or the client that created it has been closed.
* @readonly
*/
public get partitionId(): string {
return this._partitionId;
}
/**
* @property {string} [consumerGroup] The consumer group from which the handler is receiving
* events from.
* @readonly
*/
get consumerGroup(): string | undefined {
return this._receiveOptions && this._receiveOptions.consumerGroup
}
/**
* @property {number} [epoch] The epoch value of the underlying receiver, if present.
* @readonly
*/
get epoch(): number | undefined {
return this._receiveOptions && this._receiveOptions.epoch
}
/**
* @property {ReceiverRuntimeInfo} [runtimeInfo] The receiver runtime info. This property will only
* be enabled when `enableReceiverRuntimeMetric` option is set to true in the
* `client.receive()` method.
* @readonly
*/
get lastEnqueuedInfo(): LastEnqueuedInfo | undefined {
return undefined;
}
/**
* @internal
*/
constructor(context: ConnectionContext, partitionId: string, options?: ReceiveOptions) {
this._context = context;
this._partitionId = partitionId;
this._receiveOptions = options || {};
}
/**
* Starts the receiver by establishing an AMQP session and an AMQP receiver link on the session. Messages will be passed to
* the provided onMessage handler and error will be passed to the provided onError handler.
*
* @param {string|number} partitionId Partition ID from which to receive.
* @param {OnMessage} onMessage The message handler to receive event data objects.
* @param {OnError} onError The error handler to receive an error that occurs
* while receiving messages.
* @param {ReceiveOptions} [options] Options for how you'd like to receive messages.
*
* @returns {ReceiveHandler} ReceiveHandler - An object that provides a mechanism to stop receiving more messages.
*/
receive(partitionId: string, onMessage: OnMessage, onError: OnError, cancellationToken?: Aborter): ReceiveHandler {
if (typeof partitionId !== "string" && typeof partitionId !== "number") {
throw new Error("'partitionId' is a required parameter and must be of type: 'string' | 'number'.");
}
const sReceiver = StreamingReceiver.create(this._context, partitionId, this._receiveOptions);
sReceiver.prefetchCount = Constants.defaultPrefetchCount;
this._context.receivers[sReceiver.name] = sReceiver;
return sReceiver.receive(onMessage, onError);
}
/**
* Receives a batch of EventData objects from an EventHub partition for a given count and a given max wait time in seconds, whichever
* happens first. This method can be used directly after creating the receiver object and **MUST NOT** be used along with the `start()` method.
*
* @private
* @param {string|number} partitionId Partition ID from which to receive.
* @param {number} maxMessageCount The maximum message count. Must be a value greater than 0.
* @param {number} [maxWaitTimeInSeconds] The maximum wait time in seconds for which the Receiver should wait
* to receiver the said amount of messages. If not provided, it defaults to 60 seconds.
* @param {ReceiveOptions} [options] Options for how you'd like to receive messages.
*
* @returns {Promise<Array<EventData>>} Promise<Array<EventData>>.
*/
private async receiveBatch(
partitionId: string,
maxMessageCount: number,
maxWaitTimeInSeconds: number,
cancellationToken?: Aborter
): Promise<ReceivedEventData[]> {
if (typeof partitionId !== "string" && typeof partitionId !== "number") {
throw new Error("'partitionId' is a required parameter and must be of type: 'string' | 'number'.");
}
const bReceiver = BatchingReceiver.create(this._context, partitionId, this._receiveOptions);
this._context.receivers[bReceiver.name] = bReceiver;
let error: MessagingError | undefined;
let result: ReceivedEventData[] = [];
try {
result = await bReceiver.receive(maxMessageCount, maxWaitTimeInSeconds);
} catch (err) {
error = err;
log.error(
"[%s] Receiver '%s', an error occurred while receiving %d messages for %d max time:\n %O",
this._context.connectionId,
bReceiver.name,
maxMessageCount,
maxWaitTimeInSeconds,
err
);
}
try {
await bReceiver.close();
} catch (err) {
// do nothing about it.
}
if (error) {
throw error;
}
return result;
}
/**
* Gets an async iterator over events from the receiver.
*/
async *getEventIterator(
partitionId: string,
batchSize: number,
maxWaitTimeInSeconds: number,
cancellationToken?: Aborter
): AsyncIterableIterator<ReceivedEventData[]> {
// TODO: Create batching receiver using the options here
// Update `receiveBatch` call to use the above receiver
// Don't export `receiveBatch` from user
while (true) {
const currentBatch = await this.receiveBatch(partitionId, batchSize, maxWaitTimeInSeconds, cancellationToken);
yield currentBatch;
}
}
/**
* Closes the underlying AMQP receiver link.
* Once closed, the receiver cannot be used for any further operations.
* Use the `createReceiver` function on the QueueClient or SubscriptionClient to instantiate
* a new Receiver
*
* @returns {Promise<void>}
*/
async close(): Promise<void> {
this._isClosed = true;
}
/**
* Indicates whether the receiver is currently receiving messages or not.
* When this returns true, new `registerMessageHandler()` or `receiveMessages()` calls cannot be made.
*/
isReceivingMessages(): boolean {
return false;
}
}