搜索
首页Java使用 Spring Integration 适配器处理底层 MQTT 和 STOMP 连接

问题内容

我们有一些 spring integration 流程来处理通过 mqtt 或 stomp 到达的消息。为此,我们使用适配器 mqttpahomessagedrivenchanneladapterstompinboundchanneladapter。 在 mqtt 的情况下,我们观察到,如果流中的任何端点抛出异常,适配器将关闭连接并且不再接收消息。同样,如果我们重新启动代理,则不会再次建立与其的连接。

为了处理异常问题,我们将错误通道名称设置为spring默认处理的值“errorchannel”的适配器。我们的目的是只记录异常,而不关闭底层连接。这是在整个流程中处理异常的正确方法吗?

关于重新连接问题,我们对每种传输协议都有不同的方法。

  • 对于 mqtt,我们将 connectionoptionsautomaticreconnect 设置为 true
var clientfactory = new defaultmqttpahoclientfactory();
clientfactory.getconnectionoptions().setautomaticreconnect(true);

var adapter = new mqttpahomessagedrivenchanneladapter("tcp://localhost:1883", mqttasyncclient.generateclientid(), clientfactory, "/topic/mytopic");
adapter.seterrorchannelname("errorchannel");
  • 对于 stomp,我们将上下文中的 taskscheduler 设置为 reactornettytcpstompclient
var stompClient = new ReactorNettyTcpStompClient(host, port);
stompClient.setTaskScheduler(taskScheduler);

var stompSessionManager = new ReactorNettyTcpStompSessionManager(stompClient);

var adapter = new StompInboundChannelAdapter(stompSessionManager, "/queue/myQueue");
adapter.setErrorChannelName("errorChannel");

这是处理这个问题的最佳方法吗?


正确答案


是的,errorchannel 选项是抑制向 mqtt 客户端抛出异常的好方法。不必是全局 errorchannel ,它可能在许多不同的地方使用。 setautomaticreconnect(true) 确实推荐用于入站通道适配器。

reactornettytcpstompclienttaskscheduler 不适用于重新连接。请参阅其 javadocs。我认为重新连接逻辑在 reactornettytcpstompclient 中没有使用:

public completablefuture<stompsession> connectasync(@nullable stompheaders connectheaders, stompsessionhandler handler) {
    connectionhandlingstompsession session = createsession(connectheaders, handler);
    this.tcpclient.connectasync(session);
    return session.getsession();
}

通过另一种变体重新连接的情况:

CompletableFuture<Void> connectAsync(TcpConnectionHandler<P> connectionHandler, ReconnectStrategy reconnectStrategy);

以上是使用 Spring Integration 适配器处理底层 MQTT 和 STOMP 连接的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能