Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

99 righe
3.0 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Mvc;
  7. namespace Sample.Kafka.SqlServer.Controllers
  8. {
  9. public class Person
  10. {
  11. public int Id { get; set; }
  12. public string Name { get; set; }
  13. public int Age { get; set; }
  14. public override string ToString()
  15. {
  16. return "Name:" + Name + ";Age:" + Age;
  17. }
  18. }
  19. [Route("api/[controller]")]
  20. public class ValuesController : Controller, ICapSubscribe
  21. {
  22. private readonly ICapPublisher _capBus;
  23. private readonly AppDbContext _dbContext;
  24. public ValuesController(ICapPublisher producer, AppDbContext dbContext)
  25. {
  26. _capBus = producer;
  27. _dbContext = dbContext;
  28. }
  29. [Route("~/publish")]
  30. public IActionResult PublishMessage()
  31. {
  32. _capBus.Publish("sample.rabbitmq.sqlserver.order.check", DateTime.Now);
  33. //var person = new Person
  34. //{
  35. // Name = "杨晓东",
  36. // Age = 11,
  37. // Id = 23
  38. //};
  39. //_capBus.Publish("sample.rabbitmq.mysql33333", person);
  40. return Ok();
  41. }
  42. [Route("~/publishWithTrans")]
  43. public async Task<IActionResult> PublishMessageWithTransaction()
  44. {
  45. using (var trans = await _dbContext.Database.BeginTransactionAsync())
  46. {
  47. await _capBus.PublishAsync("sample.rabbitmq.mysql", "");
  48. trans.Commit();
  49. }
  50. return Ok();
  51. }
  52. [CapSubscribe("sample.rabbitmq.mysql33333",Group ="Test.Group")]
  53. public void KafkaTest22(Person person)
  54. {
  55. var aa = _dbContext.Database;
  56. _dbContext.Dispose();
  57. Console.WriteLine("[sample.kafka.sqlserver] message received " + person.ToString());
  58. Debug.WriteLine("[sample.kafka.sqlserver] message received " + person.ToString());
  59. }
  60. //[CapSubscribe("sample.rabbitmq.mysql22222")]
  61. //public void KafkaTest22(DateTime time)
  62. //{
  63. // Console.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  64. // Debug.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  65. //}
  66. [CapSubscribe("sample.rabbitmq.mysql22222")]
  67. public async Task<DateTime> KafkaTest33(DateTime time)
  68. {
  69. Console.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  70. Debug.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  71. return await Task.FromResult(time);
  72. }
  73. [NonAction]
  74. [CapSubscribe("sample.kafka.sqlserver3")]
  75. [CapSubscribe("sample.kafka.sqlserver4")]
  76. public void KafkaTest()
  77. {
  78. Console.WriteLine("[sample.kafka.sqlserver] message received");
  79. Debug.WriteLine("[sample.kafka.sqlserver] message received");
  80. }
  81. }
  82. }