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

masstransit的message几个高级用法

1)问题,Class MessageA 基类,Class MessageB继承自MessageA;

用bus.Publish方法本想把有些消息只发给B队列,结果由于其继承关系A队列也获得了消息;

解决方法用send,

 Uri uri = new Uri(RabbitMqUriQueueB);var endPoint = await _bus.GetSendEndpoint(uri);await endPoint.Send(MessageB);

2)在rabbitmq中设置

把B类型unbind掉,应该就可以了;

 

3)可能的其他方式

Message Topology · MassTransit

 

Message Topology

Message types are extensively leveraged in MassTransit, so making it easy to configure how those message types are used by topology seemed obvious.

Entity Name Formatters

Message Type

MassTransit 的exchanges,topics名称默认是消息名称,可以通过如下改为别名.

Bus.Factory.CreateUsingRabbitMq(cfg =>{    cfg.Message<OrderSubmitted>(x => {        x.SetEntityName("omg-we-got-one");    });
});

It's also possible to create a message-specific entity name formatter, by implementing IMessageEntityNameFormatter<T> and specifying it during configuration.

class FancyNameFormatter<T> :IMessageEntityNameFormatter<T>
{public string FormatEntityName(){// seriously, please don't do this, like, ever.return type(T).Name.ToString();}
}Bus.Factory.CreateUsingRabbitMq(cfg =>
{cfg.Message<OrderSubmitted>(x =>{x.SetEntityNameFormatter(new FancyNameFormatter<OrderSubmitted>());});
});

It's also possible to replace the entity name formatter for the entire topology.

class FancyNameFormatter<T> :IMessageEntityNameFormatter<T>
{public string FormatEntityName(){// seriously, please don't do this, like, ever.return type(T).Name.ToString();}
}class FancyNameFormatter :IEntityNameFormatter
{public FancyNameFormatter(IEntityNameFormatter original){_original = original;}public string FormatEntityName<T>(){if(T is OrderSubmitted)return "we-got-one";return _original.FormatEntityName<T>();}
}Bus.Factory.CreateUsingRabbitMq(cfg =>
{cfg.Message<OrderSubmitted>(x =>{x.SetEntityNameFormatter(new FancyNameFormatter(cfg.MessageTopology.EntityNameFormatter));;});
});

Attributes

EntityName

通过EntityName 熟悉修改消息名称

[EntityName("order-submitted")]
public record LegacyOrderSubmittedEvent{}

ConfigureConsumeTopology

ConfigureConsumeTopology 可选属性,设置后决定这个消息类型对应的topic或exchange是否应该被创建并订阅到这个队列。

[ConfigureConsumeTopology(false)]
public record DeleteRecord{}

ExcludeFromTopology

ExcludeFromTopology 可选属性,决定这个消息类型实例发布时,这个消息的 topic 或 exchange 是否被创建。

如下例子中发布 ReformatHardDrive 命令不会在broker上创建 ICommand topic or exchange。

[ExcludeFromTopology]
public interface ICommand{}public record ReformatHardDrive : ICommand{}

不使用属性,可以在配置时设置:

...UsingRabbitMq((context,cfg) =>
{cfg.Publish<ICommand>(p => p.Exclude = true);
});
http://www.lryc.cn/news/37253.html

相关文章:

  • 漏洞分析丨cve-2012-0003
  • rm命令——删除文件或目录
  • 【零基础入门学习Python---Python的基本语法使用】
  • 数据仓库相关概念的解释
  • 1/4车、1/2车、整车悬架模糊PID控制仿真合集
  • Linux性能补丁升级,避免不必要的跨核Wake-Up
  • Spring Cloud Alibaba全家桶(六)——微服务组件Sentinel介绍与使用
  • 拼多多2021笔试真题集 -- 3. 多多的求和计算
  • DP算法:动态规划算法
  • 一三四——一六七
  • day29_JS
  • 【HTTP协议与Web服务器】
  • Idea+maven+spring-cloud项目搭建系列--12 整合grpc
  • Revit开洞问题:结构专业开洞口剖面显示及一键开洞
  • 0107连通分量-无向图-数据结构和算法(Java)
  • [学习笔记]黑马程序员python教程
  • 如何配置用于构建 FastReport Online Designer 的 API ?
  • 【嵌入式Linux内核驱动】02_字符设备驱动
  • 【零散整理】
  • RocketMQ重复消费的症状以及解决方案
  • 数字化时代,企业的商业模式建设
  • 项目实战典型案例23——-注册上nacos上的部分服务总是出现频繁掉线的情况
  • 玩转金山文档 3分钟让你的文档智能化
  • 安装了nodejs怎么安装nvm
  • java安全编码规范考试
  • 表格检测识别技术的发展历程
  • 设计UI - Adobe xd对象介绍
  • 优思学院|精益生产中的“单件流”真的能够做到吗?
  • 移除元素问题解决方法------LeetCode-OJ题
  • JavaScript学习笔记(1.0)