4

我正在尝试使用 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);
}

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

谢谢。

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

4

1 回答 1

2

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

这是一种方法...

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

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

于 2019-02-19T14:54:01.270 回答