如何在spring中以编程方式创建通道拦截器
我想按需以编程方式创建以下XML配置:如何在spring中以编程方式创建通道拦截器,spring,spring-integration,mqtt,Spring,Spring Integration,Mqtt,我想按需以编程方式创建以下XML配置: <int-mqtt:message-driven-channel-adapter id="inboundAdapter" client-id="${mqtt.client.id}" url="${mqtt.broker.url}" topics="${mqtt.subscribed.topics}" client-factory="clientFactory" chann
<int-mqtt:message-driven-channel-adapter id="inboundAdapter"
client-id="${mqtt.client.id}"
url="${mqtt.broker.url}"
topics="${mqtt.subscribed.topics}"
client-factory="clientFactory"
channel="input-channel-1" converter="customConverter" />
<int:channel id="input-channel-1">
<int:queue/>
<int:interceptors>
<int:wire-tap channel="logger"/>
<int:ref bean="messageListener"/>
</int:interceptors>
</int:channel>
<int:channel id="logger" />
<int:logging-channel-adapter channel="logger"
auto-startup="true" level="INFO" id="loggerAdapter" log-full-message="true" />
现在我需要添加拦截器bean,通过拦截器bean,当消息到达时,每个客户机将根据各自订阅的主题得到通知
我努力实现的目标是:
1) 当客户端连接到服务器时创建mqtt适配器。(每个客户端将根据配置订阅不同的主题)
2) 在客户端断开连接时处置mqtt适配器
有人能帮我吗?不清楚你想做什么;在XML配置中,
input-channel-1
的下游有什么
messageListener
做什么
将业务逻辑放在渠道中是一种反模式;除非它是真正轻量级的东西,否则应该考虑使用<代码> <代码>来调用它——而可能是通过制作<代码>输入通道-1/代码>一个PUB子通道。
为了回答您的简单问题,要添加拦截器,您可以使用
outputChannel.addInterceptor(ctx.getBean(“messageListener”,ChannelInterceptor.class))代码>messageListener是一个bean类,它扩展了ChannelInterceptorAdapter并重写了它的preSend(消息,通道)方法。拦截器方法中的逻辑非常重要,因此我不喜欢这种添加通道拦截器的方法。我将从中查看。接得好!。顺便说一句,我可以动态创建服务激活器吗?我没有输入通道1的任何下游配置。当消息不断到达时,队列将达到其容量(?)并导致异常情况?您是否试图传递此((AbstractMessageChannel)outputChannel).addInterceptor(ctx.getBean(“messageListener”,ChannelInterceptor.class))代码>?是的,您将耗尽内存;你绝对不应该以这种方式使用监听器。是的,您可以动态创建一个“服务激活器”——只需实现MessageHandler
,将频道设为DirectChannel
,并向您的处理程序订阅即可。我建议你读一读,看看。非常感谢!!MessageHandler救了我一天…)
CustomMqttPahoMessageDrivenChannelAdapter adapter = new CustomMqttPahoMessageDrivenChannelAdapter(url, clientId, topic);
adapter.setOutputChannel(outputChannel);
adapter.setConverter(ctx.getBean("customConverter", MyPahoMessageConverter.class));