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


with spring's integration libraries, trying connect mosquitto , read/send messages... there things couldn't figure out.

1 - when initilazing app, app connects mosquitto, mosquitto receives hundreds of connection requests again same app same id in seconds. example of log :

new connection 127.0.0.1 on port 1555. client springclient connected, closing old connection. client springclient disconnected. new client connected 127.0.0.1 springclient (c1, k60). sending connack springclient (0, 0) received subscribe springclient     0001/001/inf (qos 1) springclient 1 0001/001/inf sending suback springclient new connection 127.0.0.1 on port 1555. client springclient connected, closing old connection. client springclient disconnected. 

2 - can't messages mosquitto using configuration :

spring xml :

<!-- 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 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 need know topic arrived message sent from, i've wrapped mqttpahomessagedrivenchanneladapter mentioned within spring integration docs

so there suggestions ?

i managed configure mqtt java config

@bean public mqttpahomessagedrivenchanneladapter mqttinbound() {      mqttpahomessagedrivenchanneladapter mqtt = new mqttpahomessagedrivenchanneladapter( applicationname + "-sub", clientfactory( ), "/#" );     mqtt.setqos( 2 );     mqtt.setoutputchannel( outbount( ) );     mqtt.setautostartup( true );     mqtt.settaskscheduler( taskscheduler( ) );      return mqtt; }  @bean public mqttpahomessagehandler mqqtmessagehandler() {      return new mqttpahomessagehandler( applicationname + "-pub", clientfactory( ) ); }  @bean public defaultmqttpahoclientfactory clientfactory() {      defaultmqttpahoclientfactory clientfactory = new defaultmqttpahoclientfactory( );     clientfactory.setusername( "test" );     clientfactory.setpassword( "test" );     clientfactory.setserveruris( new string[] { "tcp://url:1883" } );     return clientfactory; }  @bean public publishsubscribechannel outbount() {      publishsubscribechannel psc = new publishsubscribechannel( );     psc.subscribe( new messagehandler( ) {          @override         public void handlemessage( message<?> message ) throws messagingexception {              logger.warn( message );          }     } );      return psc; } 

to send message add following :

@autowired mqttpahomessagehandler mqtt;  @requestmapping( "/" ) public modelandview gethomepage() throws mqttpersistenceexception, mqttexception {      message<string> message = messagebuilder.withpayload( "spring - test" ).setheader( mqttheaders.topic, "/topic" ).build( );      mqtt.handlemessage( message );      return new modelandview( "home" ); }    

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -