You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 line
1.5 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP;
  4. using Microsoft.AspNetCore.Mvc;
  5. namespace Sample.RabbitMQ.MySql.Controllers
  6. {
  7. [Route("api/[controller]")]
  8. public class ValuesController : Controller
  9. {
  10. private readonly AppDbContext _dbContext;
  11. private readonly ICapPublisher _capBus;
  12. public ValuesController(AppDbContext dbContext, ICapPublisher capPublisher)
  13. {
  14. _dbContext = dbContext;
  15. _capBus = capPublisher;
  16. }
  17. [Route("~/publish")]
  18. public IActionResult PublishMessage()
  19. {
  20. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  21. return Ok();
  22. }
  23. [Route("~/publish2")]
  24. public IActionResult PublishMessage2()
  25. {
  26. _capBus.Publish("sample.kafka.sqlserver4", DateTime.Now);
  27. return Ok();
  28. }
  29. [Route("~/publishWithTrans")]
  30. public async Task<IActionResult> PublishMessageWithTransaction()
  31. {
  32. using (var trans = await _dbContext.Database.BeginTransactionAsync())
  33. {
  34. await _capBus.PublishAsync("sample.kafka.sqlserver", "");
  35. trans.Commit();
  36. }
  37. return Ok();
  38. }
  39. [NonAction]
  40. [CapSubscribe("#.rabbitmq.mysql")]
  41. public void ReceiveMessage(DateTime time)
  42. {
  43. Console.WriteLine("[sample.rabbitmq.mysql] message received: " + DateTime.Now + ",sent time: " + time);
  44. }
  45. }
  46. }