From 6b5100f8ebcd1cdb63f1997c47925d0e4407af50 Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Fri, 12 Jun 2020 23:36:02 -0700 Subject: [PATCH] Create producer directly without helper method --- .../event-hubs/src/eventHubProducerClient.ts | 46 ++++--------------- sdk/eventhub/event-hubs/src/sender.ts | 6 +-- 2 files changed, 11 insertions(+), 41 deletions(-) diff --git a/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts b/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts index 64a337364d29..aa0765e74487 100644 --- a/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts +++ b/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts @@ -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, @@ -154,6 +153,8 @@ export class EventHubProducerClient { * @throws AbortError if the operation is cancelled via the abortSignal in the options. */ async createBatch(options?: CreateBatchOptions): Promise { + throwErrorIfConnectionClosed(this._context); + if (options && options.partitionId && options.partitionKey) { throw new Error("partitionId and partitionKey cannot both be set when creating a batch"); } @@ -161,7 +162,9 @@ export class EventHubProducerClient { let producer = this._producersMap.get(""); if (!producer) { - producer = this._createProducer(); + producer = new EventHubProducer(this._context, { + retryOptions: this._clientOptions.retryOptions + }); this._producersMap.set("", producer); } @@ -202,6 +205,8 @@ export class EventHubProducerClient { batch: EventDataBatch | EventData[], options: SendBatchOptions | OperationOptions = {} ): Promise { + throwErrorIfConnectionClosed(this._context); + let partitionId: string | undefined; let partitionKey: string | undefined; if (isEventDataBatch(batch)) { @@ -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); @@ -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 - ); - } } diff --git a/sdk/eventhub/event-hubs/src/sender.ts b/sdk/eventhub/event-hubs/src/sender.ts index 127b8c94cd22..270d20e1b79f 100644 --- a/sdk/eventhub/event-hubs/src/sender.ts +++ b/sdk/eventhub/event-hubs/src/sender.ts @@ -68,8 +68,6 @@ export class EventHubProducer { * @ignore */ constructor( - eventHubName: string, - fullyQualifiedNamespace: string, context: ConnectionContext, options?: EventHubProducerOptions ) { @@ -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; } /**