当前位置: 首页 > news >正文

net8.0一键创建支持(RabbitMQ)

Necore项目生成器 - 在线创建Necore模板项目 | 一键下载

 RabbitMQController.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading.Tasks;
using UnT.Template.Application.Responses;
using UnT.Template.Domain;namespace UnT.Template.Controllers
{[Route("api/rabbimqs")][ApiController]public class RabbitMQController : ControllerBase{private readonly ConnectionFactory _connectionFactory;public RabbitMQController(ConnectionFactory connectionFactory){_connectionFactory = connectionFactory;}[HttpPost("publish")][Produces("application/json")][ProducesResponseType(typeof(ApiResponse<bool>), StatusCodes.Status200OK)]public async Task<IActionResult> Insert(){try{using (var connection = await _connectionFactory.CreateConnectionAsync())using (var channel = await connection.CreateChannelAsync()){await channel.QueueDeclareAsync(queue: "unt_queue",durable: true,exclusive: false,autoDelete: false,arguments: null);await channel.BasicPublishAsync(exchange: "",routingKey: "unt_queue",mandatory: false,body: System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new Pro_Product { Name = DateTime.Now.ToFileTime().ToString() })));}return Ok(new ApiResponse<bool> { Success = true, Data = true });}catch (Exception ex){return Ok(new ApiResponse<bool> { Success = false, Message = ex.Message, Data = false });}}[HttpPost("consume")][Produces("application/json")][ProducesResponseType(typeof(ApiResponse<bool>), StatusCodes.Status200OK)]public async Task<IActionResult> Consume(){try{Task.Run(() =>{var connection = _connectionFactory.CreateConnectionAsync().GetAwaiter().GetResult();var channel = connection.CreateChannelAsync().GetAwaiter().GetResult();{// 创建消费者var consumer = new AsyncEventingBasicConsumer(channel);channel.BasicConsumeAsync(queue: "unt_queue",autoAck: false,consumer: consumer).GetAwaiter().GetResult();// 注册接收事件处理程序consumer.ReceivedAsync += async (model, ea) =>{var body = ea.Body.ToArray();var message = Encoding.UTF8.GetString(body);Console.WriteLine($" [x] Received {message}");// 手动确认消息(如果autoAck=false)channel.BasicAckAsync(ea.DeliveryTag, false).ConfigureAwait(false).GetAwaiter().GetResult();};}});await Task.Delay(TimeSpan.FromSeconds(5));return Ok(new ApiResponse<bool> { Success = true, Data = true });}catch (Exception ex){return Ok(new ApiResponse<bool> { Success = false, Message = ex.Message, Data = false });}}}
}

http://www.dtcms.com/a/301473.html

相关文章:

  • 神经网络知识讨论
  • 嵌入式硬件篇---驱动板
  • 【学习笔记】DexMimicGen:通过模仿学习实现双臂灵巧操作的自动化数据生成
  • 数据结构基础内容(第三篇:堆栈)
  • 深度解析 inaSpeechSegmenter:高效音频语音分割与检测开源工具
  • Epoll事件EPOLLRDHUP详解
  • springboot基于Java与MySQL库的健身俱乐部管理系统设计与实现
  • C51:使用超声波测量距离
  • C#.NET dapper 详解
  • MySQL 中的 BufferPool 和 ChangeBuffer
  • Amazon Relational Database Service (Amazon RDS)入门课
  • 量子力学的基本假设
  • 【Java】图书管理系统设计详解
  • 《 集成异步任务与定时调度:线程池与任务中心设计》
  • C++--继承
  • 设计模式(六)创建型:单例模式详解
  • VINS外参精确自标定飘的问题
  • 2025.7.22总结-幸福的力量
  • 模型评估的介绍
  • 探秘CommonJS:Node.js模块化核心解析
  • macOS配置 GO语言环境
  • Python测试框架之pytest(一)
  • 数学基础薄弱者的大数据技术学习路径指南
  • 一、搭建springCloudAlibaba2021.1版本分布式微服务-父工程搭建
  • LeetCode 76:最小覆盖子串
  • 分布式事务:二阶段提交和三阶段提交底层原理
  • AI时代,我们更需要自己的开发方式与平台
  • java--函数式接口全面总结与使用场景指南
  • LeetCode 611.有效三角形的个数
  • python---eval函数