2013-11-14 4 views
3

동일한 주제로 많은 메시지를 받고있는 웹 서버가 있고 다른 주제로 응답 메시지를 보내고 있습니다.게시/등록을 위해 MQTT 클라이언트 재사용

현재 MQTT 클라이언트를 계속 연결하여 응답 메시지를 보내고 콜백에 동일한 MQTT 클라이언트 인스턴스를 다시 사용하고 있습니다.

그러나 메시지를 받고 응답을 보낸 후 다른 메시지를받을 수는 있지만 응답을 보낼 수 없습니다 - 응용 프로그램 서버를 다시 시작해야합니다.

하나의 MQTTclient 인스턴스를 갖는 것이 좋은 접근 방법입니까? 항상 연결 상태로 유지하는 것이 좋습니까? 이런 종류의 요구 사항에 가장 적합한 방법은 무엇입니까? 여기

내 코드입니다 :

public static void registerCallBack(String topicName, String userName, 
     String password, String clientId, MqttCallback callback, 
     MqttClient client) { 
    MqttConnectOptions options = new MqttConnectOptions(); 
    options.setCleanSession(true); 
    options.setKeepAliveInterval(30); 
    options.setUserName(userName); 
    options.setPassword(password.toCharArray()); 

    // Connect to Broker 
    try { 
     options.setSocketFactory(SslUtil.getSocketFactory(
       ManagerProps.MQTT_BROKER_CA_FILE.getValue(), "")); 

     client.setCallback(callback); 
     client.connect(options); 
     client.subscribe(topicName, 0); 
     log.info("successfuly registered callback to topic " + topicName); 
    } catch (MqttException me) { 
     log.error("MqttException, " + me); 
    } catch (Exception e) { 
     log.error("Exception, " + e); 
    } 
} 

public static String publishMessage(MqttClient client, String message, 
     String topic, String userName, String password) { 
    MqttConnectOptions options = new MqttConnectOptions(); 
    options.setCleanSession(true); 
    options.setKeepAliveInterval(30); 
    options.setUserName(userName); 
    options.setPassword(password.toCharArray()); 

    try { 
     MqttMessage msg = new MqttMessage(); 
     msg.setPayload(message.getBytes()); 
     client.publish(topic, msg); 
    } catch (MqttException e) { 
     log.error("MqttException, " + e); 
    } catch (Exception e) { 
     log.error("Exception, " + e); 
    } 

    return message; 
} 
+0

당신이 보내 할 수없는 이유에 대한 몇 가지 세부 사항을 추가 할 수 - 예외 , 게시 차단, 다른? –

+0

항상 단일 인스턴스와 모든 시간에 연결된 확실히 괜찮아요. 나는 코드를 도울 수 없다. – ralight

+0

@ 앤더스 그것에 대해 이상한 점이 있습니다. 예외 나 메시지가 없습니다. 내가 그것을 더빙 할 때 나는 어떤 예외없이 publish()가 실행되고 있음을 분명히 볼 수 있지만 아무 일도 일어나지 않는다. –

답변

0

나는 비슷한보고되었고,이 일을있어 :

final CallbackConnection connection = mqtt.callbackConnection(); 
    connection.listener(new org.fusesource.mqtt.client.Listener() { 

     public void onConnected() { 
     } 
     public void onDisconnected() { 
     } 
     public void onFailure(Throwable value) { 
      value.printStackTrace(); 
      System.exit(-2); 
     } 
     public void onPublish(UTF8Buffer topic, Buffer msg, Runnable ack) { 
      String body = msg.utf8().toString(); 
      if(body.startsWith("REPLY: ")) {  
       // Don't reply to your own reply    
       System.out.println("Replied"); 
       System.out.println(""); 
      } else {      
       try{    
       byte[] reply = "REPLY: Hello Back".getBytes(); 
       connection.publish(destination, reply, QoS.AT_MOST_ONCE, true, null) ; 
       msg.clear(); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
      } 
     } 
    }); 
관련 문제