|
| 1 | +#region Licence |
| 2 | +/* The MIT License (MIT) |
| 3 | +Copyright © 2026 Jonny Olliff-Lee <jonny.ollifflee@gmail.com> |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in |
| 13 | +all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +THE SOFTWARE. */ |
| 22 | + |
| 23 | +#endregion |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | +using Microsoft.Extensions.DependencyInjection; |
| 28 | +using Microsoft.Extensions.Hosting; |
| 29 | +using Paramore.Brighter; |
| 30 | +using Paramore.Brighter.AsyncAPI; |
| 31 | +using Neuroglia.AsyncApi.v3; |
| 32 | +using Paramore.Brighter.Extensions.DependencyInjection; |
| 33 | +using Paramore.Brighter.MessagingGateway.Kafka; |
| 34 | +using Paramore.Brighter.ServiceActivator.Extensions.DependencyInjection; |
| 35 | +using KafkaAsyncAPI.Events; |
| 36 | +using Serilog; |
| 37 | + |
| 38 | +Log.Logger = new LoggerConfiguration() |
| 39 | + .WriteTo.Console() |
| 40 | + .CreateLogger(); |
| 41 | + |
| 42 | +var generateAsyncApi = args.Length > 0 && args[0] == "--generate-asyncapi"; |
| 43 | + |
| 44 | +var kafkaConfig = new KafkaMessagingGatewayConfiguration |
| 45 | +{ |
| 46 | + Name = "paramore.brighter.kafkaasyncapi", |
| 47 | + BootStrapServers = new[] { "localhost:9092" } |
| 48 | +}; |
| 49 | + |
| 50 | +var kafkaMessageConsumerFactory = new KafkaMessageConsumerFactory(kafkaConfig); |
| 51 | + |
| 52 | +// KafkaProducerRegistryFactory.Create() opens a real broker connection at construction |
| 53 | +// time, so we only build the registry when actually starting the producer side. In |
| 54 | +// --generate-asyncapi mode the [PublicationTopic]-decorated event types are picked up |
| 55 | +// by assembly scanning, so the document still describes publications correctly. |
| 56 | +var host = new HostBuilder() |
| 57 | + .ConfigureServices((_, services) => |
| 58 | + { |
| 59 | + var brighter = services.AddConsumers(options => |
| 60 | + { |
| 61 | + options.Subscriptions = new Subscription[] |
| 62 | + { |
| 63 | + new KafkaSubscription<PaymentReceivedEvent>( |
| 64 | + new SubscriptionName("paramore.asyncapi.payment"), |
| 65 | + new ChannelName("payment.received"), |
| 66 | + new RoutingKey("payment.received"), |
| 67 | + groupId: "kafka-asyncapi-sample", |
| 68 | + timeOut: TimeSpan.FromMilliseconds(200), |
| 69 | + messagePumpType: MessagePumpType.Reactor, |
| 70 | + makeChannels: OnMissingChannel.Create) |
| 71 | + }; |
| 72 | + options.DefaultChannelFactory = new ChannelFactory(kafkaMessageConsumerFactory); |
| 73 | + }); |
| 74 | + |
| 75 | + if (!generateAsyncApi) |
| 76 | + { |
| 77 | + var producerRegistry = new KafkaProducerRegistryFactory( |
| 78 | + kafkaConfig, |
| 79 | + new KafkaPublication<OrderCreatedEvent>[] |
| 80 | + { |
| 81 | + new() |
| 82 | + { |
| 83 | + Topic = new RoutingKey("order.created"), |
| 84 | + NumPartitions = 3, |
| 85 | + MessageSendMaxRetries = 3, |
| 86 | + MessageTimeoutMs = 1000, |
| 87 | + MaxInFlightRequestsPerConnection = 1 |
| 88 | + } |
| 89 | + }).Create(); |
| 90 | + |
| 91 | + brighter.AddProducers(configure => |
| 92 | + { |
| 93 | + configure.ProducerRegistry = producerRegistry; |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + brighter.UseAsyncApi(opts => |
| 98 | + { |
| 99 | + opts.Title = "Kafka AsyncAPI Sample"; |
| 100 | + opts.Version = "1.0.0"; |
| 101 | + opts.Description = "Sample demonstrating AsyncAPI 3.0 generation with Kafka, showcasing subscription, publication, and assembly scanning discovery"; |
| 102 | + opts.Servers = new Dictionary<string, V3ServerDefinition> |
| 103 | + { |
| 104 | + ["kafka"] = new V3ServerDefinition |
| 105 | + { |
| 106 | + Host = "localhost:9092", |
| 107 | + Protocol = "kafka", |
| 108 | + Description = "Local Kafka broker" |
| 109 | + } |
| 110 | + }; |
| 111 | + }) |
| 112 | + .AutoFromAssemblies(); |
| 113 | + }) |
| 114 | + .UseSerilog() |
| 115 | + .Build(); |
| 116 | + |
| 117 | +if (generateAsyncApi) |
| 118 | +{ |
| 119 | + var document = await host.GenerateAsyncApiDocumentAsync("asyncapi.json"); |
| 120 | + Console.WriteLine($"AsyncAPI document generated: {document.Info.Title} v{document.Info.Version} (JSON + YAML)"); |
| 121 | + return; |
| 122 | +} |
| 123 | + |
| 124 | +Console.WriteLine("Run with --generate-asyncapi to generate the AsyncAPI document."); |
0 commit comments