You cannot 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
C#
60 lines
1.6 KiB
C#
using DotNetCore.CAP;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Savorboard.CAP.InMemoryMessageQueue;
|
|
|
|
namespace DS.Module.Nuget
|
|
{
|
|
/// <summary>
|
|
/// https://cap.dotnetcore.xyz/user-guide/zh/cap/configuration/
|
|
/// </summary>
|
|
internal class captest
|
|
{
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddCap(x =>
|
|
{
|
|
x.UseInMemoryStorage();
|
|
x.UseInMemoryMessageQueue();
|
|
});
|
|
}
|
|
|
|
public class PublishController : Controller
|
|
{
|
|
[Route("~/send")]
|
|
public IActionResult SendMessage([FromServices] ICapPublisher capBus)
|
|
{
|
|
capBus.Publish("test.show.time", DateTime.Now);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送延时消息
|
|
/// </summary>
|
|
public class PublishyController : Controller
|
|
{
|
|
[Route("~/send/delay")]
|
|
public IActionResult SendDelayMessage([FromServices] ICapPublisher capBus)
|
|
{
|
|
capBus.PublishDelay(TimeSpan.FromSeconds(100), "test.show.time", DateTime.Now);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理消息
|
|
/// </summary>
|
|
public class ConsumerController : Controller
|
|
{
|
|
[NonAction]
|
|
[CapSubscribe("test.show.time")]
|
|
public void ReceiveMessage(DateTime time)
|
|
{
|
|
Console.WriteLine("message time is:" + time);
|
|
}
|
|
}
|
|
}
|
|
} |