Skip to content

Commit

Permalink
Create producer directly without helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jun 13, 2020
1 parent 1c3c3a1 commit 6b5100f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 41 deletions.
46 changes: 9 additions & 37 deletions sdk/eventhub/event-hubs/src/eventHubProducerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { EventData } from "./eventData";
import { EventDataBatch, isEventDataBatch } from "./eventDataBatch";
import { createConnectionContext } from "./impl/eventHubClient";
import { EventHubProperties, PartitionProperties } from "./managementClient";
import { EventHubProducerOptions } from "./models/private";
import {
CreateBatchOptions,
EventHubClientOptions,
Expand Down Expand Up @@ -154,14 +153,18 @@ export class EventHubProducerClient {
* @throws AbortError if the operation is cancelled via the abortSignal in the options.
*/
async createBatch(options?: CreateBatchOptions): Promise<EventDataBatch> {
throwErrorIfConnectionClosed(this._context);

if (options && options.partitionId && options.partitionKey) {
throw new Error("partitionId and partitionKey cannot both be set when creating a batch");
}

let producer = this._producersMap.get("");

if (!producer) {
producer = this._createProducer();
producer = new EventHubProducer(this._context, {
retryOptions: this._clientOptions.retryOptions
});
this._producersMap.set("", producer);
}

Expand Down Expand Up @@ -202,6 +205,8 @@ export class EventHubProducerClient {
batch: EventDataBatch | EventData[],
options: SendBatchOptions | OperationOptions = {}
): Promise<void> {
throwErrorIfConnectionClosed(this._context);

let partitionId: string | undefined;
let partitionKey: string | undefined;
if (isEventDataBatch(batch)) {
Expand Down Expand Up @@ -238,7 +243,8 @@ export class EventHubProducerClient {

let producer = this._producersMap.get(partitionId);
if (!producer) {
producer = this._createProducer({
producer = new EventHubProducer(this._context, {
retryOptions: this._clientOptions.retryOptions,
partitionId: partitionId === "" ? undefined : partitionId
});
this._producersMap.set(partitionId, producer);
Expand Down Expand Up @@ -310,38 +316,4 @@ export class EventHubProducerClient {
retryOptions: this._clientOptions.retryOptions
});
}

/**
* Creates an Event Hub producer that can send events to the Event Hub.
* If `partitionId` is specified in the `options`, all event data sent using the producer
* will be sent to the specified partition.
* Otherwise, they are automatically routed to an available partition by the Event Hubs service.
*
* Automatic routing of partitions is recommended because:
* - The sending of events will be highly available.
* - The event data will be evenly distributed among all available partitions.
*
* @param options The set of options to apply when creating the producer.
* - `partitionId` : The identifier of the partition that the producer can be bound to.
* - `retryOptions` : The retry options used to govern retry attempts when an issue is encountered while sending events.
* A simple usage can be `{ "maxRetries": 4 }`.
*
* @throws Error if the underlying connection has been closed, create a new EventHubClient.
* @returns EventHubProducer
*/
private _createProducer(options?: EventHubProducerOptions): EventHubProducer {
if (!options) {
options = {};
}
if (!options.retryOptions) {
options.retryOptions = this._clientOptions.retryOptions;
}
throwErrorIfConnectionClosed(this._context);
return new EventHubProducer(
this.eventHubName,
this.fullyQualifiedNamespace,
this._context,
options
);
}
}
6 changes: 2 additions & 4 deletions sdk/eventhub/event-hubs/src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ export class EventHubProducer {
* @ignore
*/
constructor(
eventHubName: string,
fullyQualifiedNamespace: string,
context: ConnectionContext,
options?: EventHubProducerOptions
) {
Expand All @@ -80,8 +78,8 @@ export class EventHubProducer {
? String(this._senderOptions.partitionId)
: undefined;
this._eventHubSender = EventHubSender.create(this._context, partitionId);
this._eventHubName = eventHubName;
this._fullyQualifiedNamespace = fullyQualifiedNamespace;
this._eventHubName = this._context.config.entityPath;
this._fullyQualifiedNamespace = this._context.config.host;
}

/**
Expand Down

0 comments on commit 6b5100f

Please sign in to comment.