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.
 
 
 

60 lines
1.6 KiB

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