Rawat
Rawat

Reputation: 469

Mqtt Paho Client Connection always open

i have created mqtt connection suscribeData() as follows to receive data 24/7 from multiple devices

@RequestMapping("/suscribe")
@ResponseBody
public String suscribeData(){

    connOpt= new MqttConnectOptions();
    connOptPublish= new MqttConnectOptions();
    boolean result=false;
    int retry=0;
    while(!result){
        try{
            result=initializeMqTTParameters(retry); 
            retry++;
            if(!result){
                Thread.sleep(1000);
            }

        }catch(Exception ex){

        }
        if(retry>10){
            return "mqtterror";
        }

    }
        suscribeReceivedData("DATA/#",2);
        Calendar cal=TimeZoneJu.getCalenderInstance();
        ModemOnlineStatus status=new ModemOnlineStatus();
        status.setActiondate(cal.getTime());
        status.setActiontime(cal.getTime());
        status.setModemid("mqttreceive");
        status.setRemark("online");
        status.setStatus(true);
        try{

            service.checkAndInsertModemStatus(true, status);    
        }catch(Exception ex1){

        }
        return "ok";
}

checkAndInsertModemStatus() function is used to save the online offline state of connection i check the connection from another url with cronjobs.

and following is my initializeMqTTParameters

public boolean initializeMqTTParameters(int retry){
        String clientID = M2MIO_THING;
        connOpt = new MqttConnectOptions();

        connOpt.setCleanSession(false);
        connOpt.setUserName(M2MIO_USERNAME);
        connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray());
        connOpt.setWill("WILL/mqttreceive", "mqttreceive:disconnected".getBytes(), 2, true);
        try {

            if(retry%2==0){
                MQTTReceive.myClient = new MqttClient(BROKER_URL, "mqtt_virtual_received_sus_1");   
            }else{
                MQTTReceive.myClient = new MqttClient(BROKER_URL, "mqtt_virtual_received_sus_2");   
            }

            MQTTReceive.myClient.setCallback(this);
            MQTTReceive.myClient.connect(connOpt);

        } catch (MqttException e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

on connectionLost i am trying update status in database and try reconnect

@Override
    public void connectionLost(Throwable ex) {
        System.out.println("Mqtt Received Connection Lost"+ex.getMessage()); 
        Calendar cal=TimeZoneJu.getCalenderInstance();
        ModemOnlineStatus status=new ModemOnlineStatus();
        status.setActiondate(cal.getTime());
        status.setActiontime(cal.getTime());
        status.setModemid("mqttreceive");
        status.setRemark("offline");
        status.setStatus(false);
        try{

        service.updateModemStatus(false, status);   
    }catch(Exception ex1){

    }

    suscribeData();
}

and from the above code i got my connections were online but i stopped receving data on subscribed topics so another method i tried that i am checking connection status from another function from database like i set the connection online when i connect and offline on connectionLost and i can call to connect(suscribeData()) but it leaves me with too many open files in tomcat within few days how can i make sure previous open files should close

like System.exit(1) on connectionLost() so that current process will close and i will reconnect after checking the status

or please suggest me if there is any other logic i can use to maintain my connections

Upvotes: 1

Views: 931

Answers (1)

Koustav Ray
Koustav Ray

Reputation: 1142

Your catch block does nothing in method connectionLost.. it should do something like this:

 catch (Exception e)
         {
              e.printStackTrace();
              System.exit(1);
          }

You should also disconnect your MQTT client whenever not required..i.e processing over:

myClient.disconnect();

Upvotes: 0

Related Questions