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

武汉设计工程学院官网贵州seo培训

武汉设计工程学院官网,贵州seo培训,网站设计的机构,石家庄做网络推广的公司问题描述 在一个事务内完成插入操作,通过MQ异步通知其他微服务进行事件处理。 由于是在事务内发送,其他服务消费消息,查询数据时还不存在如何解决呢? 解决方案 通过spring-tx包的TransactionSynchronizationManager事务管理器解…

问题描述

在一个事务内完成插入操作,通过MQ异步通知其他微服务进行事件处理。
由于是在事务内发送,其他服务消费消息,查询数据时还不存在如何解决呢?


解决方案

通过spring-tx包的TransactionSynchronizationManager事务管理器解决。

public abstract class TransactionSynchronizationManager {private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations =new NamedThreadLocal<>("Transaction synchronizations");/*** Return if transaction synchronization is active for the current thread.* Can be called before register to avoid unnecessary instance creation.* @see #registerSynchronization*/		public static boolean isSynchronizationActive() {return (synchronizations.get() != null);}/*** Register a new transaction synchronization for the current thread.* Typically called by resource management code.* <p>Note that synchronizations can implement the* {@link org.springframework.core.Ordered} interface.* They will be executed in an order according to their order value (if any).* @param synchronization the synchronization object to register* @throws IllegalStateException if transaction synchronization is not active* @see org.springframework.core.Ordered*/public static void registerSynchronization(TransactionSynchronization synchronization)throws IllegalStateException {Assert.notNull(synchronization, "TransactionSynchronization must not be null");Set<TransactionSynchronization> synchs = synchronizations.get();if (synchs == null) {throw new IllegalStateException("Transaction synchronization is not active");}synchs.add(synchronization);}}

Rocketmq方法封装,通过TransactionSynchronizationManager.isSynchronizationActive()判断当前方法的调用是否在事务内。
如果是,则注册一个事务同步适配器,在事务提交后发送消息。
否则直接发送。

    /*** 事务内发送 mq时使用,强制到事务结束后发送*/public SendResult sendAfterTrans(String topic, String tag, String key, String body) {final SendResult[] res = new SendResult[1];try {// 是否开启事务判断if (TransactionSynchronizationManager.isSynchronizationActive()) {log.debug("Mysql事务内Mq消息发送  延迟到事务提交后 waiting……");TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {@Overridepublic void afterCommit() {log.debug("Mysql事务内Mq消息发送  发送消息 body:{}", body);res[0] = send(topic, tag, key, body);}});} else {return this.send(topic, tag, key, body);}} catch (Exception e) {e.printStackTrace();}return res[0];}

在这里插入图片描述

http://www.hengruixuexiao.com/news/50127.html

相关文章:

  • 文章网站如何与压力做营销网站类型
  • 医院网站建设情况说明自己有域名怎么建网站
  • 中国最早做网站是谁三一crm手机客户端下载
  • 如何通过c语言来做网站软文写作技巧
  • 合肥市城乡建设委员会网站网站域名服务器查询
  • 网站建设的详细步骤郑州百度网站快速优化
  • 网站链接 动态图怎么做吉林黄页电话查询
  • 网站维护的内容郑州做网站推广电话
  • 房建设计网站外贸营销网站怎么建站
  • 推荐一些外国做产品网站临沂seo代理商
  • 上海网站建设空间百度搜索引擎优化的养成良好心态
  • 南阳在线网站制作青岛运营网络推广业务
  • 上海网络维护服务公司网站快速排名优化
  • 大连做网站优化价格湖南百度推广
  • 企业网站主要有哪四种类型接广告的平台
  • wordpress小工具插件谷歌seo排名公司
  • 威客网站系统公关公司
  • 福州建设网站公司模板网站哪个好
  • 遵义北京网站建设深圳推广平台有哪些
  • 企业官网建站步骤自助优化排名工具
  • 一起做网店的类似网站seo研究中心好客站
  • 保定有哪些做网站的地方搜索引擎营销的案例有哪些
  • 南皮做网站的吸引人的微信软文范例
  • 网站有几类百度有效点击软件
  • 做网站的像素是多少钱自动点击器app
  • html 手机网站开发seminar
  • 查询网站备案直通车优化推广
  • 做网站需要的流程郑州seo关键词优化公司
  • 常州网站制作公司排名汕头seo排名公司
  • 图片素材网站模板免费网络营销软件