Spring MVC + Mosquitto + MQTT Integration can't get any message

paroxit

With Spring's integration libraries, I am trying to connect to mosquitto and read/send messages... But there are some things I couldn't figure out.

1 - When initilazing app, app connects to mosquitto, but mosquitto receives hundreds of connection requests again from same app with same id in seconds. This is the example of log :

New connection from 127.0.0.1 on port 1555.
Client springClient already connected, closing old connection.
Client springClient disconnected.
New client connected from 127.0.0.1 as springClient (c1, k60).
Sending CONNACK to springClient (0, 0)
Received SUBSCRIBE from springClient
    0001/001/INF (QoS 1)
springClient 1 0001/001/INF
Sending SUBACK to springClient
New connection from 127.0.0.1 on port 1555.
Client springClient already connected, closing old connection.
Client springClient disconnected.

2 - I can't get any messages from mosquitto using this configuration :

Spring XML :

<!-- This is for reading messages -->
<bean id="mqttInbound" class="com.mobistech.drc.m2mproject.mqtt.MqttCustomInboundAdapter">
    <beans:constructor-arg name="clientId" value="springClient" />
    <beans:constructor-arg name="clientFactory" ref="clientFactory" />
    <beans:constructor-arg name="topic" value="0001/001/INF" />
    <beans:property name="autoStartup" value="true"></beans:property>
    <beans:property name="outputChannel" ref="fromBrokerChannel"></beans:property>
</bean>

 <int:channel  id="fromBrokerChannel" />

Custom Adapter :

public class MqttCustomInboundAdapter extends MqttPahoMessageDrivenChannelAdapter {

    public MqttCustomInboundAdapter(String clientId,
            MqttPahoClientFactory clientFactory, String[] topic) {
        super(clientId, clientFactory, topic);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception
    {
        super.messageArrived(topic, message);
        System.out.println("**************** Message from topic : " + topic);
        System.out.println("**************** Message : " + new String(message.getPayload()));
    }

    public void addTopicIfNotExists(String topic)
    {
        for(String topicName:getTopic())
        {
            if(topicName.equals(topic))return;
        }

        addTopic(topic);

        System.out.println("************* Added Topic : " + topic);

        for(String topicName:getTopic())
        {
            System.out.println(topicName);
        }
    }
}

I'm not using service-activator because I need to know which topic that arrived message sent from, so I've wrapped the MqttPahoMessageDrivenChannelAdapter as its mentioned within the Spring Integration Docs

So is there any suggestions ?

paroxit

I've found it. After doing some research, I've decided to use service-activator for activating the service (which is obvious). I can get messages after this point.

And about mosquitto's strange behaivour, I figured it out that, it wasn't about the mosquitto. When MqttCustomInboundAdapter's autoStartup property set to true, application sends too many connect requests. That was the reason that mosquitto getting this connection requests and trying to connect them one by one causing the old one getting disconnected.

New XML looks like this :

 <bean id="mqttInbound" class="com.mobistech.drc.m2mproject.mqtt.MqttCustomInboundAdapter">
    <beans:constructor-arg name="clientId" value="springClient" />
    <beans:constructor-arg name="clientFactory" ref="clientFactory" />
    <beans:constructor-arg name="topic" value="0001/001/INF" />
    <beans:property name="autoStartup" value="false"></beans:property>
    <beans:property name="outputChannel" ref="fromBrokerChannel"></beans:property>
    <beans:property name="converter" ref="mqttMessageConverter"></beans:property>
</bean>

 <int:channel id="fromBrokerChannel" />
<int:service-activator input-channel="fromBrokerChannel" ref="mqttServiceActivator" ></int:service-activator>

Now I can get messages...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring integration route can't dispatch message

From Dev

can we batch up groups of 10 message load in mosquitto using spring integration

From Java

Spring Integration(MQTT): Retrieving published message

From Dev

Is there any limit on the size of data that can be stored in Spring Integration message header

From Dev

Use Paho under Spring Integration to receive binary MQTT message

From Dev

How to disable mqtt duplicate message in Mosquitto

From Dev

How to publish a message to a specific client in Mosquitto MQTT

From Dev

Mosquitto: published mqtt message not delivered to websockets client

From Java

Spring MVC - Dispatcher Servlet can't find any page

From Java

Spring Integration IMAP - cannot get the body of the message

From Dev

MQTT with Mosquitto

From Dev

how can I get the response message from Axis2 in spring integration

From Dev

Paho MQTT vs MQTT paho spring integration

From Dev

Is there any way to send empty payload message using spring integration gateway?

From Java

Can't get any unit tests to run in Spring Boot

From Dev

Display the json message from an mqtt server on my terminal (mosquitto)

From Java

Spring Integration: MQTT integration test basics

From Dev

How can I get the message data of a mqtt connection in python

From Java

Trying Spring MVC with hibernate integration for the first time.. unable to resolve the error please any one can help . error 505

From Dev

Spring Integration message polling

From Java

Spring Integration Message History

From Dev

Home Assistant Mosquitto - What can I use as MQTT broker address?

From Dev

How to get message headers in an interceptor in Spring-Integration

From Dev

spring integration amqp, how get message by routing key

From Dev

Spring Integration @ServiceActivator Message Handler doesn't work

From Dev

Can't get user name on google +integration?

From Dev

Spring Integration, MQTT Support, get Broker answer after calling MessageChannel send

From Dev

ActiveMQ Spring Integration using MQTT topic

From Dev

Which Spring Integration Channel should be used for MQTT

Related Related

  1. 1

    Spring integration route can't dispatch message

  2. 2

    can we batch up groups of 10 message load in mosquitto using spring integration

  3. 3

    Spring Integration(MQTT): Retrieving published message

  4. 4

    Is there any limit on the size of data that can be stored in Spring Integration message header

  5. 5

    Use Paho under Spring Integration to receive binary MQTT message

  6. 6

    How to disable mqtt duplicate message in Mosquitto

  7. 7

    How to publish a message to a specific client in Mosquitto MQTT

  8. 8

    Mosquitto: published mqtt message not delivered to websockets client

  9. 9

    Spring MVC - Dispatcher Servlet can't find any page

  10. 10

    Spring Integration IMAP - cannot get the body of the message

  11. 11

    MQTT with Mosquitto

  12. 12

    how can I get the response message from Axis2 in spring integration

  13. 13

    Paho MQTT vs MQTT paho spring integration

  14. 14

    Is there any way to send empty payload message using spring integration gateway?

  15. 15

    Can't get any unit tests to run in Spring Boot

  16. 16

    Display the json message from an mqtt server on my terminal (mosquitto)

  17. 17

    Spring Integration: MQTT integration test basics

  18. 18

    How can I get the message data of a mqtt connection in python

  19. 19

    Trying Spring MVC with hibernate integration for the first time.. unable to resolve the error please any one can help . error 505

  20. 20

    Spring Integration message polling

  21. 21

    Spring Integration Message History

  22. 22

    Home Assistant Mosquitto - What can I use as MQTT broker address?

  23. 23

    How to get message headers in an interceptor in Spring-Integration

  24. 24

    spring integration amqp, how get message by routing key

  25. 25

    Spring Integration @ServiceActivator Message Handler doesn't work

  26. 26

    Can't get user name on google +integration?

  27. 27

    Spring Integration, MQTT Support, get Broker answer after calling MessageChannel send

  28. 28

    ActiveMQ Spring Integration using MQTT topic

  29. 29

    Which Spring Integration Channel should be used for MQTT

HotTag

Archive