|
| 1 | +// Copyright 2020 Confluent Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// Refer to LICENSE for more information. |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using Microsoft.AspNetCore.Mvc; |
| 20 | +using Microsoft.Extensions.Configuration; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | +using Confluent.Kafka; |
| 23 | + |
| 24 | + |
| 25 | +namespace Web.Controllers |
| 26 | +{ |
| 27 | + public class HomeController : Controller |
| 28 | + { |
| 29 | + private string topic; |
| 30 | + private readonly KafkaDependentProducer<Null, string> producer; |
| 31 | + private readonly ILogger logger; |
| 32 | + |
| 33 | + public HomeController(KafkaDependentProducer<Null, string> producer, IConfiguration config, ILogger<HomeController> logger) |
| 34 | + { |
| 35 | + // In a real-world application, you might be using Kafka as a system of record and may wish |
| 36 | + // to update application state in your request handlers. Or you may wish to write some |
| 37 | + // analytics data related to your handler logic. In this example, we aren't doing anything |
| 38 | + // interesting, so we just write frivolous messages to demonstrate how to set things up. |
| 39 | + this.topic = config.GetValue<string>("Kafka:FrivolousTopic"); |
| 40 | + this.producer = producer; |
| 41 | + this.logger = logger; |
| 42 | + } |
| 43 | + |
| 44 | + public async Task<IActionResult> Index() |
| 45 | + { |
| 46 | + // Simulate a complex request handler by delaying a random amount of time. |
| 47 | + await Task.Delay((int)(new Random((int)DateTime.Now.Ticks).NextDouble()*100)); |
| 48 | + |
| 49 | + // Important note: DO NOT create a new producer instance every time you |
| 50 | + // need to produce a message (this is a common pattern with relational database |
| 51 | + // drivers, but it is extremely inefficient here). Instead, use a long-lived |
| 52 | + // singleton instance, as per this example. |
| 53 | + |
| 54 | + // Do not delay completion of the page request on the result of produce call. |
| 55 | + // Any errors are handled out of band in deliveryReportHandler. |
| 56 | + this.producer.Produce(topic, new Message<Null, string> { Value = "Frivolous message (index)" }, deliveryReportHandler); |
| 57 | + return View(); |
| 58 | + } |
| 59 | + |
| 60 | + public async Task<IActionResult> Page1() |
| 61 | + { |
| 62 | + await Task.Delay((int)(new Random((int)DateTime.Now.Ticks).NextDouble()*100)); |
| 63 | + |
| 64 | + // Delay completion of the page request on the result of the produce call. |
| 65 | + // An exception will be thrown in the case of an error. |
| 66 | + await this.producer.ProduceAsync(topic, new Message<Null, string> { Value = "Frivolous message #1" }); |
| 67 | + return View(); |
| 68 | + } |
| 69 | + |
| 70 | + private void deliveryReportHandler(DeliveryReport<Null, string> deliveryReport) |
| 71 | + { |
| 72 | + if (deliveryReport.Status == PersistenceStatus.NotPersisted) |
| 73 | + { |
| 74 | + // It is common to write application logs to Kafka (note: this project does not provide |
| 75 | + // an example logger implementation that does this). Such an implementation should |
| 76 | + // ideally fall back to logging messages locally in the case of delivery problems. |
| 77 | + this.logger.Log(LogLevel.Warning, $"Message delivery failed: {deliveryReport.Message.Value}"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments