← Back to team overview

mosquitto-users team mailing list archive

?????? ?????? I met a strange question

 

thanks . here it is:
  
 public class Subscriber  extends HttpServlet{
  private static final long serialVersionUID = -758602144721002871L;
 private static MqttClient mqttClient;
 
 @Override
 public void init() throws ServletException {  
  sub();  
 }
 
  public void sub(){
  
  if(mqttClient==null || !mqttClient.isConnected()){
   mqttClient = getConnection();  
  }
  
  try {
   mqttClient.setCallback(new MqttCallback(){
     public void connectionLost(Throwable throwable) {
     System.out.println("subscriber connection lost");
     getConnection();
     boolean flag = true;
     while(flag){
      try {
       Thread.sleep(5000); 
       if(!mqttClient.isConnected()){        
        getConnection();
       }else{
        flag = false;
        System.out.println("subscriber connection lost , reconnected.");
        sub();
       }
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     }
    }
     public void messageArrived(MqttTopic mqttTopic, MqttMessage mqttMessage) throws Exception {
     String topic = mqttTopic.toString();
     String payload = mqttMessage.toString();
     String clientid ;
     String command ;
     String regex = "dsms/([a-zA-Z0-9]+)/([a-z]+)";
     Pattern patten = Pattern.compile(regex);
     Matcher mat = patten.matcher(topic);
     if(mat.matches()){ 
      System.out.println("topic is valid. topic:" + topic + " payload:" + payload);
      clientid = mat.group(1);
      command = mat.group(2);
      if(command.equals("test")){
       new TestHandler(clientid,command,payload).handler();
      }      
     }else{
      System.err.println("topic is not valid. topic:" + topic + " payload:" + payload);
     }
    }
 
    public void deliveryComplete(
      MqttDeliveryToken paramMqttDeliveryToken) {
    }
   });
    //sub message
   mqttClient.subscribe("dsms/#",2);//test
  } catch (MqttException e) {
   e.printStackTrace();
   System.out.println("exception e is:" + e);
  }
  }
 
 
 
 /**
  * get mqtt connection
  * @return
  */
 public static MqttClient getConnection(){
  String clientID = "dsms-sub";  
  char passwd[] = {'a','d','m','i','n'};
  MemoryPersistence persistence = new MemoryPersistence();
  try{
   String url = FilesHelper.readProperties("MQTT_BROKER").trim();   
   mqttClient = new MqttClient(url, clientID,persistence);
   MqttConnectOptions connectOptions = new  MqttConnectOptions();
   connectOptions.setUserName("admin");
   connectOptions.setPassword(passwd);
   connectOptions.setCleanSession(false); //durable subscribe
   mqttClient.connect(connectOptions);
  }catch(Exception e){
   System.out.println("subscriber connection lost , reconnecting.");
   try {
    Thread.sleep(10000);
   } catch (InterruptedException e1) {
    e1.printStackTrace();
   }
   getConnection();
  }
   return mqttClient;
 }
 
  /**
  * @param args
  */
 public static void main(String[] args) {
  Subscriber sub = new Subscriber();
  sub.sub();
 }
 }
  

 

 ------------------ ???????? ------------------
  ??????: "Karl Palsson";<karlp@xxxxxxxxxxxx>;
 ????????: 2013??12??11??(??????) ????10:00
 ??????: "mosquitto-users"<mosquitto-users@xxxxxxxxxxxxxxxxxxx>; 
 
 ????: Re: [Mosquitto-users] ?????? I met a strange question

 


If you could share your paho code, people might know more, it's very hard to understand what you're doing I'm
sorry.

What I _think_ is happening, is that while you are publishing with qos2, you might not have
clean_session=False, or you might not have stable/persistent client ids.  In that case, paho might reconnect,
but it won't resubscribe.  (If paho even reconnects)


On Wed, Dec 11, 2013 at 10:10:49AM +0800, ??(Grey)?? wrote:
> thanks . even though i pub a new message , my paho client also can't sub any messages , so i think this problem has nothing with retained flag.
>   
> 
>  
> 
>  ------------------ Original ------------------
>   From:  "Jan-Piet Mens";<jpmens@xxxxxxxxx>;
>  Date:  Tue, Dec 10, 2013 07:40 PM
>  To:  "mosquitto-users"<mosquitto-users@xxxxxxxxxxxxxxxxxxx>; 
>  
>  Subject:  Re: [Mosquitto-users] ?????? I met a strange question
> 
>  
> 
> Hello,
> 
> >         1.delete mosquitto.db then restart mosquitto 
> >         2. pub a qos 2 message without sub 
> >         3. restart subscriber and cannot receive message any more.
> 
> If you intend to be able to get messages previously published to the
> broker (i.e. retained *by* the broker), you have to set the `retain'
> flag on messages as you publish them. With the command-line tools, you'd
> do something like this:
> 
>         mosquitto_pub -t topic -m payload -r
> 
> A subsequent SUB on that topic (provided ACLs allow you to do so) will
> read that last retained message for that particular topic.
> 
>         -JP
> 
> -- 
> Mailing list: https://launchpad.net/~mosquitto-users
> Post to     : mosquitto-users@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~mosquitto-users
> More help   : https://help.launchpad.net/ListHelp
> .

> -- 
> Mailing list: https://launchpad.net/~mosquitto-users
> Post to     : mosquitto-users@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~mosquitto-users
> More help   : https://help.launchpad.net/ListHelp


-- 
Mailing list: https://launchpad.net/~mosquitto-users
Post to     : mosquitto-users@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~mosquitto-users
More help   : https://help.launchpad.net/ListHelp

References