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 ?