-
Notifications
You must be signed in to change notification settings - Fork 374
Implement Bulk Publish functionality #1001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,3 +101,6 @@ coverage.json | |
|
|
||
| # Examples bloat | ||
| examples/**/tmp | ||
|
|
||
| # MacOS | ||
| **/.DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
examples/Client/PublishSubscribe/BulkPublishEventExample/BulkPublishEventExample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2023 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Dapr.Client; | ||
|
|
||
| namespace Samples.Client | ||
| { | ||
| public class BulkPublishEventExample : Example | ||
| { | ||
| private const string PubsubName = "pubsub"; | ||
| private const string TopicName = "deposit"; | ||
|
|
||
| IReadOnlyList<object> BulkPublishData = new List<object>() { | ||
| new { Id = "17", Amount = 10m }, | ||
| new { Id = "18", Amount = 20m }, | ||
| new { Id = "19", Amount = 30m } | ||
| }; | ||
|
|
||
| public override string DisplayName => "Bulk Publishing Events"; | ||
|
|
||
| public override async Task RunAsync(CancellationToken cancellationToken) | ||
| { | ||
| using var client = new DaprClientBuilder().Build(); | ||
|
|
||
| var res = await client.BulkPublishEventAsync(PubsubName, TopicName, | ||
| BulkPublishData); | ||
|
|
||
| if (res != null) { | ||
| if (res.FailedEntries.Count > 0) | ||
| { | ||
| Console.WriteLine("Some events failed to be published!"); | ||
|
|
||
| foreach (var failedEntry in res.FailedEntries) | ||
| { | ||
| Console.WriteLine("EntryId : " + failedEntry.Entry.EntryId + " Error message : " + | ||
| failedEntry.ErrorMessage); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("Published multiple deposit events!"); | ||
| } | ||
| } else { | ||
| throw new Exception("null response from dapr"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
examples/Client/PublishSubscribe/BulkPublishEventExample/BulkPublishEventExample.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| <RootNamespace>Samples.Client</RootNamespace> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Protobuf Include="..\..\..\AspNetCore\GrpcServiceSample\Protos\*.proto" ProtoRoot="..\..\..\AspNetCore\GrpcServiceSample\Protos\" GrpcServices="None" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\Dapr.Client\Dapr.Client.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Google.Protobuf" Version="3.15.0" /> | ||
| <PackageReference Include="Google.Api.CommonProtos" Version="2.2.0" /> | ||
| <PackageReference Include="Grpc.Tools" Version="2.47.0" PrivateAssets="All" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
25 changes: 25 additions & 0 deletions
25
examples/Client/PublishSubscribe/BulkPublishEventExample/Example.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2023 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Samples.Client | ||
| { | ||
| public abstract class Example | ||
| { | ||
| public abstract string DisplayName { get; } | ||
|
|
||
| public abstract Task RunAsync(CancellationToken cancellationToken); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
examples/Client/PublishSubscribe/BulkPublishEventExample/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2023 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Samples.Client | ||
| { | ||
| class Program | ||
| { | ||
| private static readonly Example[] Examples = new Example[] | ||
| { | ||
| new BulkPublishEventExample(), | ||
| }; | ||
|
|
||
| static async Task<int> Main(string[] args) | ||
| { | ||
| if (args.Length > 0 && int.TryParse(args[0], out var index) && index >= 0 && index < Examples.Length) | ||
| { | ||
| var cts = new CancellationTokenSource(); | ||
| Console.CancelKeyPress += (object? sender, ConsoleCancelEventArgs e) => cts.Cancel(); | ||
|
|
||
| await Examples[index].RunAsync(cts.Token); | ||
| return 0; | ||
| } | ||
|
|
||
| Console.WriteLine("Hello, please choose a sample to run:"); | ||
| for (var i = 0; i < Examples.Length; i++) | ||
| { | ||
| Console.WriteLine($"{i}: {Examples[i].DisplayName}"); | ||
| } | ||
| Console.WriteLine(); | ||
| return 0; | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
examples/Client/PublishSubscribe/BulkPublishEventExample/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Dapr .NET SDK Bulk publish example | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [.NET Core 3.1 or .NET 5+](https://dotnet.microsoft.com/download) installed | ||
|
yash-nisar marked this conversation as resolved.
Outdated
|
||
| - [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/) | ||
| - [Initialized Dapr environment](https://docs.dapr.io/getting-started/install-dapr-selfhost/) | ||
| - [Dapr .NET SDK](https://docs.dapr.io/developing-applications/sdks/dotnet/) | ||
|
|
||
| ## Running the example | ||
|
|
||
| ### Run the subscriber | ||
|
|
||
| Navigate to the [ControllerSample](https://github.com/dapr/dotnet-sdk/tree/master/examples/AspNetCore/ControllerSample) directory and run the subscriber. It will subscribe to the `deposit` topic that the publisher will publish messages to. | ||
|
|
||
| ```sh | ||
| dapr run --app-id controller --app-port 5000 -- dotnet run | ||
| ``` | ||
|
|
||
| ### Run the bulk publisher | ||
| After running the subscriber, run the bulk publisher. To run the sample locally, run this command in this project root directory: | ||
|
|
||
| ```sh | ||
| dapr run --app-id DaprClient -- dotnet run <sample number> | ||
| ``` | ||
|
|
||
| Running the following command will output a list of the samples included: | ||
|
|
||
| ```sh | ||
| dapr run --app-id DaprClient -- dotnet run | ||
| ``` | ||
|
|
||
| Press Ctrl+C to exit, and then run the command again and provide a sample number to run the samples. | ||
|
|
||
| For example run this command to run the 0th sample from the list produced earlier. | ||
|
|
||
| ```sh | ||
| dapr run --app-id DaprClient -- dotnet run 0 | ||
| ``` | ||
|
|
||
| ## Publishing Bulk Pub/Sub Events | ||
|
|
||
| See [BulkPublishEventExample.cs](./BulkPublishEventExample.cs) for an example using the `DaprClient` to publish a pub/sub event. | ||
2 changes: 1 addition & 1 deletion
2
examples/Client/PublishSubscribe/Example.cs → ...hSubscribe/PublishEventExample/Example.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
examples/Client/PublishSubscribe/PublishEventExample/PublishBytesExample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2023 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Mime; | ||
| using System.Text.Json; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Dapr.Client; | ||
|
|
||
| namespace Samples.Client | ||
| { | ||
| public class PublishBytesExample : Example | ||
| { | ||
| public override string DisplayName => "Publish Bytes"; | ||
|
|
||
| public async override Task RunAsync(CancellationToken cancellationToken) | ||
| { | ||
| using var client = new DaprClientBuilder().Build(); | ||
|
|
||
| var transaction = new { Id = "17", Amount = 30m }; | ||
| var content = JsonSerializer.SerializeToUtf8Bytes(transaction); | ||
|
|
||
| await client.PublishByteEventAsync(pubsubName, "deposit", content.AsMemory(), MediaTypeNames.Application.Json, new Dictionary<string, string> { }, cancellationToken); | ||
| Console.WriteLine("Published deposit event!"); | ||
| } | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...t/PublishSubscribe/PublishEventExample.cs → ...ublishEventExample/PublishEventExample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.