Skip to content

Commit

Permalink
Update Priority Queue Cloud Pattern
Browse files Browse the repository at this point in the history
Update to .NET 8.0
Updated README
  • Loading branch information
skabou committed Mar 13, 2024
1 parent 820387b commit 617a48f
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enabled</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Functions.Worker;

namespace PriorityQueueConsumerHigh
{
public static class PriorityQueueConsumerHighFn
public class PriorityQueueConsumerHighFn
{
[FunctionName("HighPriorityQueueConsumerFunction")]
public static void Run([ServiceBusTrigger("messages", "highPriority", Connection = "ServiceBusConnection")]string highPriorityMessage, ILogger log)
private readonly ILogger _logger;

public PriorityQueueConsumerHighFn(ILogger<PriorityQueueConsumerHighFn> logger)
{
_logger = logger;
}

[Function("HighPriorityQueueConsumerFunction")]
public void Run([ServiceBusTrigger("messages", "highPriority", Connection = "ServiceBusConnectionString")] string highPriorityMessage)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {highPriorityMessage}");
_logger.LogInformation($"C# ServiceBus topic trigger function processed message: {highPriorityMessage}");
}
}
}
}
7 changes: 7 additions & 0 deletions priority-queue/PriorityQueueConsumerHigh/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();

host.Run();
8 changes: 0 additions & 8 deletions priority-queue/PriorityQueueConsumerHigh/local.settings.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"ServiceBusConnectionString": "SERVICE_BUS_CONNECTION_STRING"
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<UserSecretsId>736bb6a2-68b4-463b-a8fb-3a90cba7cd4f</UserSecretsId>
<OutputType>Exe</OutputType>
<ImplicitUsings>enabled</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Functions.Worker;

namespace PriorityQueueConsumerLow
{
public static class PriorityQueueConsumerLowFn
public class PriorityQueueConsumerLowFn
{
[FunctionName("LowPriorityQueueConsumerFunction")]
public static void Run([ServiceBusTrigger("messages", "lowPriority", Connection = "ServiceBusConnection")]string lowPriorityMessage, ILogger log)
private readonly ILogger _logger;

public PriorityQueueConsumerLowFn(ILogger<PriorityQueueConsumerLowFn> logger)
{
_logger = logger;
}

[Function("LowPriorityQueueConsumerFunction")]
public void Run([ServiceBusTrigger("messages", "lowPriority", Connection = "ServiceBusConnectionString")] string lowPriorityMessage)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {lowPriorityMessage}");
_logger.LogInformation($"C# ServiceBus topic trigger function processed message: {lowPriorityMessage}");
}
}
}
}
7 changes: 7 additions & 0 deletions priority-queue/PriorityQueueConsumerLow/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();

host.Run();
8 changes: 0 additions & 8 deletions priority-queue/PriorityQueueConsumerLow/local.settings.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"ServiceBusConnectionString": "SERVICE_BUS_CONNECTION_STRING"
}
}
25 changes: 12 additions & 13 deletions priority-queue/PriorityQueueSender/PriorityQueueSender.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enabled</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
</ItemGroup>
</Project>
21 changes: 12 additions & 9 deletions priority-queue/PriorityQueueSender/PriorityQueueSenderFn.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Functions.Worker;

namespace PriorityQueueSender
{
public static class PriorityQueueSenderFn
public class PriorityQueueSenderFn(ILogger<PriorityQueueSenderFn> logger, ServiceBusClient client)
{
[FunctionName("PriorityQueueSenderFunction")]
public static async Task Run(
[TimerTrigger("0,30 * * * * *")] TimerInfo myTimer,
[ServiceBus("messages", Connection = "ServiceBusConnection")] IAsyncCollector<ServiceBusMessage> collector )
private readonly ILogger<PriorityQueueSenderFn> _logger = logger;
private readonly ServiceBusClient _client = client;

[Function("PriorityQueueSenderFunction")]
public async Task Run([TimerTrigger("0,30 * * * * *")] TimerInfo myTimer)
{
var sender = _client.CreateSender("messages");
for (int i = 0; i < 10; i++)
{
var messageId = Guid.NewGuid().ToString();
var lpMessage = new ServiceBusMessage() { MessageId = messageId };
lpMessage.ApplicationProperties["Priority"] = Priority.Low;
lpMessage.Body = BinaryData.FromString($"Low priority message with Id: {messageId}");
await collector.AddAsync(lpMessage);
await sender.SendMessageAsync(lpMessage);

messageId = Guid.NewGuid().ToString();
var hpMessage = new ServiceBusMessage() { MessageId = messageId };
hpMessage.ApplicationProperties["Priority"] = Priority.High;
hpMessage.Body = BinaryData.FromString($"High priority message with Id: {messageId}");
await collector.AddAsync(hpMessage);
await sender.SendMessageAsync(hpMessage);
}
}
}
}
}
26 changes: 26 additions & 0 deletions priority-queue/PriorityQueueSender/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
})
.ConfigureServices(services =>
{
var configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();

services.AddSingleton(configuration);

services.AddSingleton<ServiceBusClient>(sp =>
{
var connectionString = configuration.GetValue<string>("ServiceBusConnectionString");
return new ServiceBusClient(connectionString);
});
})
.Build();

host.Run();
8 changes: 0 additions & 8 deletions priority-queue/PriorityQueueSender/local.settings.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"ServiceBusConnectionString": "SERVICE_BUS_CONNECTION_STRING"
}
}
Loading

0 comments on commit 617a48f

Please sign in to comment.