checkpoint rabbitMq

This commit is contained in:
AdisakKanthawilang 2024-11-26 17:17:13 +07:00
parent be18edf0a6
commit 93d0c2711f
2 changed files with 96 additions and 0 deletions

View file

@ -13,10 +13,13 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using RabbitMQ.Client.Events;
using RabbitMQ.Client;
using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
namespace BMA.EHR.Placement.Service.Controllers
{
@ -59,6 +62,55 @@ namespace BMA.EHR.Placement.Service.Controllers
#endregion
/// <summary>
/// ทดสอบ (Rabbit MQ)
/// </summary>
/// <returns></returns>
[HttpPost("test-queue")]
public async Task<ActionResult<ResponseObject>> TestRabbitMQ([FromBody] NotiRequest req)
{
var host = "localhost";
var userName = "guest";
var password = "guest";
var factory = new ConnectionFactory()
{
HostName = host,
UserName = userName,
Password = password
};
Console.WriteLine("Send to Consume!");
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "test_dotnet", durable: false, exclusive: false, autoDelete: false, arguments: null);
var jsonString = JsonConvert.SerializeObject(req);
var body = Encoding.UTF8.GetBytes(jsonString);
channel.BasicPublish(exchange: "", routingKey: "test_dotnet", basicProperties: null, body: body);
var consumer = new EventingBasicConsumer(channel);
var receivedTaskCompletionSource = new TaskCompletionSource<bool>();
consumer.Received += async (model, x) =>
{
await _repositoryNoti.PushNotificationAsync2(
Guid.Parse(req.ReceiverUserId),
req.Subject,
req.Body,
req.Payload,
"",
req.IsSendInbox,
req.IsSendMail
);
receivedTaskCompletionSource.SetResult(true);
};
channel.BasicConsume("test_dotnet", autoAck: true, consumer);
Console.WriteLine("Consume Worked!");
return Success();
}
}
[HttpPost()]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUser([FromBody] NotiRequest req)
{