Share via


EventHubConsumerClient class

類別 EventHubConsumerClient 是用來取用事件中樞的事件。

有多種方式可以建立 EventHubConsumerClient

  • 使用針對事件中樞實例建立的 SAS 原則中的連接字串。
  • 使用針對事件中樞命名空間建立之 SAS 原則的連接字串,以及事件中樞實例的名稱
  • 使用完整的命名空間,例如 <yournamespace>.servicebus.windows.net 和 認證物件。

您也可以選擇性地傳遞:

  • 設定重試原則或 Proxy 設定的選項包。
  • 用戶端用來讀取檢查點的檢查點存放區,以判斷應用程式重新開機時,應該繼續接收事件的位置。 用戶端也會使用檢查點存放區來負載平衡應用程式的多個實例。

建構函式

EventHubConsumerClient(string, string, CheckpointStore, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

EventHubConsumerClient(string, string, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

EventHubConsumerClient(string, string, string, CheckpointStore, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

EventHubConsumerClient(string, string, string, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

EventHubConsumerClient(string, string, string, TokenCredential | NamedKeyCredential | SASCredential, CheckpointStore, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

EventHubConsumerClient(string, string, string, TokenCredential | NamedKeyCredential | SASCredential, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

屬性

defaultConsumerGroupName

事件中樞服務中預設取用者群組的名稱。

eventHubName

建立此用戶端的事件中樞實例名稱。

fullyQualifiedNamespace

建立此用戶端之事件中樞實例的完整命名空間。 這可能類似于 .servicebus.windows.net。

identifier

用來識別此 EventHubConsumerClient 的名稱。 如果未指定或空白,則會產生隨機唯一的 。

方法

close()

關閉與事件中樞實例的 AMQP 連線,並傳回在中斷連線完成時將解決的承諾。

getEventHubProperties(GetEventHubPropertiesOptions)

提供事件中樞執行時間資訊。

getPartitionIds(GetPartitionIdsOptions)

提供與事件中樞相關聯之每個分割區的識別碼。

getPartitionProperties(string, GetPartitionPropertiesOptions)

提供指定之分割區狀態的相關資訊。

subscribe(string, SubscriptionEventHandlers, SubscribeOptions)

訂閱單一分割區的事件。 在傳回的物件上呼叫 close () 以停止接收事件。

使用方式範例:

const client = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
const subscription = client.subscribe(
 partitionId,
 {
   processEvents: (events, context) => { console.log("Received event count: ", events.length) },
   processError: (err, context) => { console.log("Error: ", err) }
 },
 { startPosition: earliestEventPosition }
);
subscribe(SubscriptionEventHandlers, SubscribeOptions)

訂閱來自所有分割區的事件。

如果提供檢查點存放區給 EventHubConsumerClient ,而且您的應用程式有多個實例,則每個實例都會訂閱分割區的子集,讓負載在兩者之間平衡。

在傳回的物件上呼叫 close () 以停止接收事件。

使用方式範例:

const client = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
const subscription = client.subscribe(
 {
   processEvents: (events, context) => { console.log("Received event count: ", events.length) },
   processError: (err, context) => { console.log("Error: ", err) }
 },
 { startPosition: earliestEventPosition }
);

建構函式詳細資料

EventHubConsumerClient(string, string, CheckpointStore, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

new EventHubConsumerClient(consumerGroup: string, connectionString: string, checkpointStore: CheckpointStore, options?: EventHubConsumerClientOptions)

參數

consumerGroup

string

您要處理事件之取用者群組的名稱。

connectionString

string

用來連線到事件中樞實例的連接字串。 預期共用索引鍵屬性和事件中樞路徑會包含在此連接字串中。 例如 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;EntityPath=my-event-hub-name'。

checkpointStore
CheckpointStore

用戶端用來讀取檢查點的檢查點存放區,以判斷應用程式重新開機時,應該繼續接收事件的位置。 用戶端也會使用它來平衡應用程式的多個實例。

options
EventHubConsumerClientOptions

設定用戶端時要套用的一組選項。

  • retryOptions :設定用戶端上所有作業的重試原則。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:設定透過 Web 通訊端的 AMQP 連線通道。
  • userAgent :要附加至傳遞至服務之內建使用者代理程式字串的字串。

EventHubConsumerClient(string, string, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

new EventHubConsumerClient(consumerGroup: string, connectionString: string, options?: EventHubConsumerClientOptions)

參數

consumerGroup

string

您要處理事件之取用者群組的名稱。

connectionString

string

用來連線到事件中樞實例的連接字串。 預期共用索引鍵屬性和事件中樞路徑會包含在此連接字串中。 例如 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;EntityPath=my-event-hub-name'。

options
EventHubConsumerClientOptions

設定用戶端時要套用的一組選項。

  • retryOptions :設定用戶端上所有作業的重試原則。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:設定透過 Web 通訊端的 AMQP 連線通道。
  • userAgent :要附加至傳遞至服務之內建使用者代理程式字串的字串。

EventHubConsumerClient(string, string, string, CheckpointStore, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

new EventHubConsumerClient(consumerGroup: string, connectionString: string, eventHubName: string, checkpointStore: CheckpointStore, options?: EventHubConsumerClientOptions)

參數

consumerGroup

string

您要處理事件之取用者群組的名稱。

connectionString

string

用來連線到事件中樞命名空間的連接字串。 預期共用金鑰屬性包含在此連接字串中,但不包含事件中樞路徑,例如 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;'。

eventHubName

string

要連接用戶端的特定事件中樞名稱。

checkpointStore
CheckpointStore

用戶端用來讀取檢查點的檢查點存放區,以判斷應用程式重新開機時,應該繼續接收事件的位置。 用戶端也會使用它來平衡應用程式的多個實例。

options
EventHubConsumerClientOptions

設定用戶端時要套用的一組選項。

  • retryOptions :設定用戶端上所有作業的重試原則。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:設定透過 Web 通訊端的 AMQP 連線通道。
  • userAgent :要附加至傳遞至服務之內建使用者代理程式字串的字串。

EventHubConsumerClient(string, string, string, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

new EventHubConsumerClient(consumerGroup: string, connectionString: string, eventHubName: string, options?: EventHubConsumerClientOptions)

參數

consumerGroup

string

您要處理事件之取用者群組的名稱。

connectionString

string

用來連線到事件中樞命名空間的連接字串。 預期共用金鑰屬性包含在此連接字串中,但不包含事件中樞路徑,例如 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;'。

eventHubName

string

要連接用戶端的特定事件中樞名稱。

options
EventHubConsumerClientOptions

設定用戶端時要套用的一組選項。

  • retryOptions :設定用戶端上所有作業的重試原則。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:設定透過 Web 通訊端的 AMQP 連線通道。
  • userAgent :要附加至傳遞至服務之內建使用者代理程式字串的字串。

EventHubConsumerClient(string, string, string, TokenCredential | NamedKeyCredential | SASCredential, CheckpointStore, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

new EventHubConsumerClient(consumerGroup: string, fullyQualifiedNamespace: string, eventHubName: string, credential: TokenCredential | NamedKeyCredential | SASCredential, checkpointStore: CheckpointStore, options?: EventHubConsumerClientOptions)

參數

consumerGroup

string

您要處理事件之取用者群組的名稱。

fullyQualifiedNamespace

string

可能類似于 .servicebus.windows.net 的完整命名空間

eventHubName

string

要連接用戶端的特定事件中樞名稱。

credential

TokenCredential | NamedKeyCredential | SASCredential

用戶端用來取得權杖的認證物件,用來向Azure 事件中樞服務驗證連線。 如需建立支援 AAD 驗證的認證,請參閱@azure/身分識別。 AzureNamedKeyCredential 如果您想要傳入 SharedAccessKeyNameSharedAccessKey 而不要使用連接字串,請使用 from @azure/core-auth。 這些欄位分別對應至 name 中的 AzureNamedKeyCredentialkey 欄位。 AzureSASCredential如果您想要 SharedAccessSignature 傳入 而不使用連接字串,請使用 from @azure/core-auth。 此欄位對應至 signature 中的 AzureSASCredential

checkpointStore
CheckpointStore

用戶端用來讀取檢查點的檢查點存放區,以判斷應用程式重新開機時,應該繼續接收事件的位置。 用戶端也會使用它來平衡應用程式的多個實例。

options
EventHubConsumerClientOptions

設定用戶端時要套用的一組選項。

  • retryOptions :設定用戶端上所有作業的重試原則。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:設定透過 Web 通訊端的 AMQP 連線通道。
  • userAgent :要附加至傳遞至服務之內建使用者代理程式字串的字串。

EventHubConsumerClient(string, string, string, TokenCredential | NamedKeyCredential | SASCredential, EventHubConsumerClientOptions)

類別 EventHubConsumerClient 是用來取用事件中樞的事件。 options使用參數來設定重試原則或 Proxy 設定。

new EventHubConsumerClient(consumerGroup: string, fullyQualifiedNamespace: string, eventHubName: string, credential: TokenCredential | NamedKeyCredential | SASCredential, options?: EventHubConsumerClientOptions)

參數

consumerGroup

string

您要處理事件之取用者群組的名稱。

fullyQualifiedNamespace

string

可能類似于 .servicebus.windows.net 的完整命名空間

eventHubName

string

要連接用戶端的特定事件中樞名稱。

credential

TokenCredential | NamedKeyCredential | SASCredential

用戶端用來取得權杖的認證物件,用來向Azure 事件中樞服務驗證連線。 如需建立支援 AAD 驗證的認證,請參閱@azure/身分識別。 AzureNamedKeyCredential 如果您想要傳入 SharedAccessKeyNameSharedAccessKey 而不要使用連接字串,請使用 from @azure/core-auth。 這些欄位分別對應至 name 中的 AzureNamedKeyCredentialkey 欄位。 AzureSASCredential如果您想要 SharedAccessSignature 傳入 而不使用連接字串,請使用 from @azure/core-auth。 此欄位對應至 signature 中的 AzureSASCredential

options
EventHubConsumerClientOptions

設定用戶端時要套用的一組選項。

  • retryOptions :設定用戶端上所有作業的重試原則。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:設定透過 Web 通訊端的 AMQP 連線通道。
  • userAgent :要附加至傳遞至服務之內建使用者代理程式字串的字串。

屬性詳細資料

defaultConsumerGroupName

事件中樞服務中預設取用者群組的名稱。

static defaultConsumerGroupName: string

屬性值

string

eventHubName

建立此用戶端的事件中樞實例名稱。

string eventHubName

屬性值

string

fullyQualifiedNamespace

建立此用戶端之事件中樞實例的完整命名空間。 這可能類似于 .servicebus.windows.net。

string fullyQualifiedNamespace

屬性值

string

identifier

用來識別此 EventHubConsumerClient 的名稱。 如果未指定或空白,則會產生隨機唯一的 。

identifier: string

屬性值

string

方法詳細資料

close()

關閉與事件中樞實例的 AMQP 連線,並傳回在中斷連線完成時將解決的承諾。

function close(): Promise<void>

傳回

Promise<void>

Promise

getEventHubProperties(GetEventHubPropertiesOptions)

提供事件中樞執行時間資訊。

function getEventHubProperties(options?: GetEventHubPropertiesOptions): Promise<EventHubProperties>

參數

options
GetEventHubPropertiesOptions

要套用至作業呼叫的選項組。

傳回

可解決事件中樞實例相關資訊的承諾。

getPartitionIds(GetPartitionIdsOptions)

提供與事件中樞相關聯之每個分割區的識別碼。

function getPartitionIds(options?: GetPartitionIdsOptions): Promise<string[]>

參數

options
GetPartitionIdsOptions

要套用至作業呼叫的選項組。

傳回

Promise<string[]>

承諾,會使用字串陣列解析,此陣列代表與事件中樞相關聯之每個分割區的識別碼。

getPartitionProperties(string, GetPartitionPropertiesOptions)

提供指定之分割區狀態的相關資訊。

function getPartitionProperties(partitionId: string, options?: GetPartitionPropertiesOptions): Promise<PartitionProperties>

參數

partitionId

string

需要資訊的分割區識別碼。

options
GetPartitionPropertiesOptions

要套用至作業呼叫的選項組。

傳回

一項承諾,其會解決資料分割狀態的相關資訊。

subscribe(string, SubscriptionEventHandlers, SubscribeOptions)

訂閱單一分割區的事件。 在傳回的物件上呼叫 close () 以停止接收事件。

使用方式範例:

const client = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
const subscription = client.subscribe(
 partitionId,
 {
   processEvents: (events, context) => { console.log("Received event count: ", events.length) },
   processError: (err, context) => { console.log("Error: ", err) }
 },
 { startPosition: earliestEventPosition }
);
function subscribe(partitionId: string, handlers: SubscriptionEventHandlers, options?: SubscribeOptions): Subscription

參數

partitionId

string

要訂閱之資料分割的識別碼。

handlers
SubscriptionEventHandlers

訂閱生命週期的處理常式 - 資料分割的訂用帳戶初始化、接收事件、處理錯誤,以及分割區的訂閱關閉。

options
SubscribeOptions

設定接收事件的方式。 最常見的是 maxBatchSize ,以及 maxWaitTimeInSeconds 控制提供給接收事件之處理常式的事件流程,以及開始位置。 例如, { maxBatchSize: 20, maxWaitTimeInSeconds: 120, startPosition: { sequenceNumber: 123 } }

傳回

subscribe(SubscriptionEventHandlers, SubscribeOptions)

訂閱來自所有分割區的事件。

如果提供檢查點存放區給 EventHubConsumerClient ,而且您的應用程式有多個實例,則每個實例都會訂閱分割區的子集,讓負載在兩者之間平衡。

在傳回的物件上呼叫 close () 以停止接收事件。

使用方式範例:

const client = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
const subscription = client.subscribe(
 {
   processEvents: (events, context) => { console.log("Received event count: ", events.length) },
   processError: (err, context) => { console.log("Error: ", err) }
 },
 { startPosition: earliestEventPosition }
);
function subscribe(handlers: SubscriptionEventHandlers, options?: SubscribeOptions): Subscription

參數

handlers
SubscriptionEventHandlers

訂閱生命週期的處理常式 - 每個分割區的訂用帳戶初始化、接收事件、處理錯誤,以及每個分割區的訂閱關閉。

options
SubscribeOptions

設定接收事件的方式。 最常見的是 maxBatchSize ,以及 maxWaitTimeInSeconds 控制提供給接收事件之處理常式的事件流程,以及開始位置。 例如, { maxBatchSize: 20, maxWaitTimeInSeconds: 120, startPosition: { sequenceNumber: 123 } }

傳回