Black Glix
Black Glix

Reputation: 709

Eclipse Paho Mqtt - Spring Java configuration

I want to use MqTT in my SpringMVC project. In this link,the official example, creates all the objects with new keyword. As far as I know, this is not Spring style. The recommended way to do this creating bean, isn't?

I found some examples (spring-integration-mqtt, which based on eclipse-paho-mqtt) configured xml-based, but I want to make it Java based configuration. I congifured whole project Java-based. There is no .xml file in the project (not even web.xml).

If you suggest me an example with Java-config or good document about converting xml-config to java-config I will be appriciated.

Thanks in advance.

Upvotes: 2

Views: 1600

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121427

You can track the Pull Request on the matter, but let me share a piece of code to track more info here as well:

@Bean
public MessageProducer inbound() {
    MqttPahoMessageDrivenChannelAdapter adapter =
            new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "testClient",
                                             "topic1", "topic2");
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    adapter.setOutputChannel(mqttInputChannel());
    return adapter;
}

@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler amqpOutbound() {
    MqttPahoMessageHandler messageHandler =
                   new MqttPahoMessageHandler("testClient", mqttClientFactory());
    messageHandler.setAsync(true);
    messageHandler.setDefaultTopic("testTopic");
    return messageHandler;
}

Upvotes: 4

Related Questions