【问题标题】:Sending messages to different topics using spring integration gateway使用 spring 集成网关向不同主题发送消息
【发布时间】:2019-07-12 23:08:30
【问题描述】:

我正在尝试使用 spring 集成将 mqtt 消息发送到代理,并且我正在尝试使用网关接口。

 @Bean
public MqttPahoClientFactory mqttClientFactory() {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    //set the factory details
    return factory:
}

@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound() {
    MqttPahoMessageHandler messageHandler =
            new MqttPahoMessageHandler("randomString", mqttClientFactory());
    //set handler details
    messageHandler.setDefaultTopic(topic);
    return messageHandler;
}

@Bean
public MessageChannel mqttOutboundChannel() {
    return new DirectChannel();
}
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
private interface MyGateway {
    void sendToMqtt(String data);
}

我的问题是:如果我想使用网关处理程序将消息发送到不同的主题,我将如何做到这一点而不必为每个主题创建适配器?

谢谢。

希望我清楚地提出了我的问题并且代码格式正确。

【问题讨论】:

    标签: spring-integration spring-integration-mqtt


    【解决方案1】:

    您需要在消息头中设置目标主题。

    这是一种方法...

    void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic);
    

    网关代理将消息与标头组合起来,然后由出站适配器使用。

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • 它确实回答了这个问题,但我会添加更多的话。
    • 这正是我想要的。我发现了 @header(topic) 注释,但我不知道如何使用它。你知道我在哪里可以找到春季的文档吗?我已经找了几天了。
    • @Header的使用是documented here。 MQTT 出站通道适配器为documented here>defaultTopic - The default topic to which the message is sent (used if no mqtt_topic header is found).setDefaultTopic() 的 javadocs。
    • @GaryRussell,请同时查看stackoverflow.com/questions/60456751/…
    猜你喜欢
    • 2018-11-24
    • 2014-08-29
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多