From 959a573e108540937aeabe07f6c52dfdaa8c849d Mon Sep 17 00:00:00 2001 From: Shivangi Reja Date: Thu, 9 May 2019 14:59:41 -0700 Subject: [PATCH 1/3] [Event Hubs] Update samples to pass the default consumer group in the options parameter --- .../event-hubs/samples/interactiveLogin.ts | 2 +- .../samples/loginWithAzureAccount.ts | 2 +- ...iveMessagesLoop.ts => receiveEventsLoop.ts} | 18 +++++++++--------- ...sStreaming.ts => receiveEventsStreaming.ts} | 12 ++++++------ .../samples/{sendMessages.ts => sendEvents.ts} | 12 ++++++------ .../samples/servicePrincipalLogin.ts | 2 +- sdk/eventhub/event-hubs/samples/useProxy.ts | 2 +- 7 files changed, 25 insertions(+), 25 deletions(-) rename sdk/eventhub/event-hubs/samples/{receiveMessagesLoop.ts => receiveEventsLoop.ts} (61%) rename sdk/eventhub/event-hubs/samples/{receiveMessagesStreaming.ts => receiveEventsStreaming.ts} (81%) rename sdk/eventhub/event-hubs/samples/{sendMessages.ts => sendEvents.ts} (82%) diff --git a/sdk/eventhub/event-hubs/samples/interactiveLogin.ts b/sdk/eventhub/event-hubs/samples/interactiveLogin.ts index 626f6e99fadf..1815a8331e23 100644 --- a/sdk/eventhub/event-hubs/samples/interactiveLogin.ts +++ b/sdk/eventhub/event-hubs/samples/interactiveLogin.ts @@ -22,7 +22,7 @@ async function main(): Promise { const client = EventHubClient.createFromAadTokenCredentials(evenHubsEndpoint, eventHubsName, credentials); /* Refer to other samples, and place your code here - to send/receive messages + to send/receive events */ await client.close(); } diff --git a/sdk/eventhub/event-hubs/samples/loginWithAzureAccount.ts b/sdk/eventhub/event-hubs/samples/loginWithAzureAccount.ts index 1d6a7bf74d72..b16623af68f5 100644 --- a/sdk/eventhub/event-hubs/samples/loginWithAzureAccount.ts +++ b/sdk/eventhub/event-hubs/samples/loginWithAzureAccount.ts @@ -30,7 +30,7 @@ async function main(): Promise { const client = EventHubClient.createFromAadTokenCredentials(evenHubsEndpoint, eventHubsName, credentials); /* Refer to other samples, and place your code here - to send/receive messages + to send/receive events */ await client.close(); } diff --git a/sdk/eventhub/event-hubs/samples/receiveMessagesLoop.ts b/sdk/eventhub/event-hubs/samples/receiveEventsLoop.ts similarity index 61% rename from sdk/eventhub/event-hubs/samples/receiveMessagesLoop.ts rename to sdk/eventhub/event-hubs/samples/receiveEventsLoop.ts index 73a0963c5673..a8779410115e 100644 --- a/sdk/eventhub/event-hubs/samples/receiveMessagesLoop.ts +++ b/sdk/eventhub/event-hubs/samples/receiveEventsLoop.ts @@ -2,10 +2,9 @@ Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT Licence. - This sample demonstrates how the receiveBatch() function can be used to receive Event Hubs - messages in a loop. + This sample demonstrates how the receiveBatch() function can be used to receive events in a loop. - If your Event Hubs instance doesn't have any messages, then please run "sendMesages.ts" sample + If your Event Hubs instance doesn't have any events, then please run "sendEvents.ts" sample to populate Event Hubs before running this sample. */ @@ -22,15 +21,16 @@ async function main(): Promise { const batchSize = 1; for (let i = 0; i < 10; i++) { - const messages = await client.receiveBatch(partitionIds[0], batchSize, 5, { - eventPosition: eventPosition + const events = await client.receiveBatch(partitionIds[0], batchSize, 5, { + eventPosition: eventPosition, + consumerGroup: "$Default" }); - if (!messages.length) { - console.log("No more messages to receive"); + if (!events.length) { + console.log("No more events to receive"); break; } - eventPosition = EventPosition.fromSequenceNumber(messages[messages.length - 1].sequenceNumber!); - console.log(`Received messages #${i}: ${messages.map(msg => msg.body)}`); + eventPosition = EventPosition.fromSequenceNumber(events[events.length - 1].sequenceNumber!); + console.log(`Received events #${i}: ${events.map(event => event.body)}`); } await client.close(); } diff --git a/sdk/eventhub/event-hubs/samples/receiveMessagesStreaming.ts b/sdk/eventhub/event-hubs/samples/receiveEventsStreaming.ts similarity index 81% rename from sdk/eventhub/event-hubs/samples/receiveMessagesStreaming.ts rename to sdk/eventhub/event-hubs/samples/receiveEventsStreaming.ts index 3ef6f34bb5cb..a232b19018f5 100644 --- a/sdk/eventhub/event-hubs/samples/receiveMessagesStreaming.ts +++ b/sdk/eventhub/event-hubs/samples/receiveEventsStreaming.ts @@ -2,10 +2,9 @@ Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT Licence. - This sample demonstrates how the receive() function can be used to receive Event Hubs messages - in a stream. + This sample demonstrates how the receive() function can be used to receive events in a stream. - If your Event Hubs instance doesn't have any messages, then please run "sendMesages.ts" sample + If your Event Hubs instance doesn't have any events, then please run "sendEvents.ts" sample to populate Event Hubs before running this sample. */ @@ -20,17 +19,18 @@ async function main(): Promise { const partitionIds = await client.getPartitionIds(); const onMessageHandler: OnMessage = async (brokeredMessage: EventData) => { - console.log(`Received message: ${brokeredMessage.body}`); + console.log(`Received event: ${brokeredMessage.body}`); }; const onErrorHandler: OnError = (err: MessagingError | Error) => { console.log("Error occurred: ", err); }; const rcvHandler = client.receive(partitionIds[0], onMessageHandler, onErrorHandler, { - eventPosition: EventPosition.fromStart() + eventPosition: EventPosition.fromStart(), + consumerGroup: "$Default" }); - // Waiting long enough before closing the receiver to receive messages + // Waiting long enough before closing the receiver to receive event await delay(5000); await rcvHandler.stop(); await client.close(); diff --git a/sdk/eventhub/event-hubs/samples/sendMessages.ts b/sdk/eventhub/event-hubs/samples/sendEvents.ts similarity index 82% rename from sdk/eventhub/event-hubs/samples/sendMessages.ts rename to sdk/eventhub/event-hubs/samples/sendEvents.ts index bfb40258ccfb..bc19d8b56e1f 100644 --- a/sdk/eventhub/event-hubs/samples/sendMessages.ts +++ b/sdk/eventhub/event-hubs/samples/sendEvents.ts @@ -2,13 +2,13 @@ Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT Licence. - This sample demonstrates how the send() function can be used to send messages to Event Hubs. + This sample demonstrates how the send() function can be used to send events to Event Hubs. See https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-about to learn about Event Hubs. */ -import { EventHubClient, EventData } from "@azure/event-hubs"; +import { EventHubClient } from "@azure/event-hubs"; // Define connection string and related Event Hubs entity name here const connectionString = ""; @@ -33,14 +33,14 @@ async function main(): Promise { for (let index = 0; index < listOfScientists.length; index++) { const scientist = listOfScientists[index]; - const data: EventData = { + const event = { body: `${scientist.firstName} ${scientist.name}` }; // NOTE: For receiving events from Azure Stream Analytics, please send Events to an EventHub // where the body is a JSON object/array. - // const data = { body: { "message": `${scientist.firstName} ${scientist.name}` } }; - console.log(`Sending message: ${data.body}`); - await client.send(data, partitionIds[0]); + // const event = { body: { "message": `${scientist.firstName} ${scientist.name}` } }; + console.log(`Sending event: ${event.body}`); + await client.send(event, partitionIds[0]); } await client.close(); diff --git a/sdk/eventhub/event-hubs/samples/servicePrincipalLogin.ts b/sdk/eventhub/event-hubs/samples/servicePrincipalLogin.ts index c658a47e71f8..652aaa3e43c3 100644 --- a/sdk/eventhub/event-hubs/samples/servicePrincipalLogin.ts +++ b/sdk/eventhub/event-hubs/samples/servicePrincipalLogin.ts @@ -36,7 +36,7 @@ async function main(): Promise { const client = EventHubClient.createFromAadTokenCredentials(evenHubsEndpoint, eventHubsName, credentials); /* Refer to other samples, and place your code here - to send/receive messages + to send/receive events */ await client.close(); } diff --git a/sdk/eventhub/event-hubs/samples/useProxy.ts b/sdk/eventhub/event-hubs/samples/useProxy.ts index 2192a95c6382..0ec492bb8090 100644 --- a/sdk/eventhub/event-hubs/samples/useProxy.ts +++ b/sdk/eventhub/event-hubs/samples/useProxy.ts @@ -29,7 +29,7 @@ async function main(): Promise { }); /* Refer to other samples, and place your code here - to send/receive messages + to send/receive events */ await client.close(); } From 70936e7009c1da581d15d61f15c85742a808b5e7 Mon Sep 17 00:00:00 2001 From: Shivangi Reja Date: Thu, 9 May 2019 15:46:38 -0700 Subject: [PATCH 2/3] Added event type in sendEvents sample --- sdk/eventhub/event-hubs/samples/sendEvents.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/eventhub/event-hubs/samples/sendEvents.ts b/sdk/eventhub/event-hubs/samples/sendEvents.ts index bc19d8b56e1f..a3dd9b7b98ff 100644 --- a/sdk/eventhub/event-hubs/samples/sendEvents.ts +++ b/sdk/eventhub/event-hubs/samples/sendEvents.ts @@ -8,7 +8,7 @@ to learn about Event Hubs. */ -import { EventHubClient } from "@azure/event-hubs"; +import { EventHubClient, EventData } from "@azure/event-hubs"; // Define connection string and related Event Hubs entity name here const connectionString = ""; @@ -33,7 +33,7 @@ async function main(): Promise { for (let index = 0; index < listOfScientists.length; index++) { const scientist = listOfScientists[index]; - const event = { + const event: EventData = { body: `${scientist.firstName} ${scientist.name}` }; // NOTE: For receiving events from Azure Stream Analytics, please send Events to an EventHub From e430cf7407f985a00bf8e5aef22fcf5c61338883 Mon Sep 17 00:00:00 2001 From: Shivangi Reja Date: Thu, 9 May 2019 16:21:18 -0700 Subject: [PATCH 3/3] Change event->eventData --- sdk/eventhub/event-hubs/samples/sendEvents.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/eventhub/event-hubs/samples/sendEvents.ts b/sdk/eventhub/event-hubs/samples/sendEvents.ts index a3dd9b7b98ff..49a2d62b5afb 100644 --- a/sdk/eventhub/event-hubs/samples/sendEvents.ts +++ b/sdk/eventhub/event-hubs/samples/sendEvents.ts @@ -33,14 +33,14 @@ async function main(): Promise { for (let index = 0; index < listOfScientists.length; index++) { const scientist = listOfScientists[index]; - const event: EventData = { + const eventData: EventData = { body: `${scientist.firstName} ${scientist.name}` }; // NOTE: For receiving events from Azure Stream Analytics, please send Events to an EventHub // where the body is a JSON object/array. - // const event = { body: { "message": `${scientist.firstName} ${scientist.name}` } }; - console.log(`Sending event: ${event.body}`); - await client.send(event, partitionIds[0]); + // const eventData = { body: { "message": `${scientist.firstName} ${scientist.name}` } }; + console.log(`Sending event: ${eventData.body}`); + await client.send(eventData, partitionIds[0]); } await client.close();