La synchronisation et l’interface asynchrone de l’object MqttClient ne fonctionnent pas

J’ai créé un client de type MqttClient et, comme indiqué ci-dessous dans le code, je crée un client avec son Asynchronous callback . Le problème est,

1-quand je lance le programme, le System.out.println("Client is Connected"); apparaît, mais je ne reçois aucune réponse du onSuccess ou de o onFailure , pourquoi? wha je me trompe dans le code.

2-i a implémenté le static IMqttAsyncClient asynchClientCB = new IMqttAsyncClient() interface static IMqttAsyncClient asynchClientCB = new IMqttAsyncClient() , mais comme j’ai un client de type MqttClient , je ne peux pas utiliser cette interface IMqttAsyncClient . J’ai essayé d’utiliser mqttAsynchClien mais parce que je programme pour Java et pas pour Android je ne peux pas l’utiliser. Comment utiliser l’interface IMqttAsyncClient .?

Mise à jour_1

dans le code ci-dessous “Updated_code_1”, j’ai légèrement modifié le code, mais je m’attends à chaque fois que je onSuccess connecte avec succès au broker le message dans le rappel synchrone onSuccess sera imprimé et le message dans le onFailure synchrone sera imprimé dans le cas de la connexion terminé, par exemple lorsque je déconnecte volontairement le réseau. Mais au moment où je me connecte au broker , ni onSuccess , ni onSuccess ne peut rien faire. Alors, pour quoi sont-ils conçus?

* Update_2_17_Dec_2014

J’ai une demande qui pourrait nous mener à une solution: est-ce important si je me connecte au courtier via un réseau câblé / sans fil? cela changerait-il le comportement de l’écouteur synchrone et asynchrone?

Updated_1_code :

 MqttConnectOptions opts = getClientOptions(); client = MQTTClientFactory.newClient(broker, port, clientID); if (client != null) { System.out.println("Client is not Null"); client.setCallback(AsynchCallBack); if (opts != null) { iMQTTToken = client.connectWithResult(opts); publishMSG(client, TOPIC,"010101".getBytes(), QoS, pub_isRetained); iMQTTToken.setActionCallback(synchCallBack); if (client.isConnected()) { System.out.println("Client CONNECTED."); publishMSG(client, TOPIC,"010101".getBytes(), QoS, pub_isRetained); } } } .... .... .... .... IMqttToken iMQTTToken = new IMqttToken() { @Override public void waitForCompletion(long arg0) throws MqttException { // TODO Auto-generated method stub System.out.println("@waitForCompletion(): waiting " + (arg0 * 1000) + " seconds for connection to be established."); } @Override public void waitForCompletion() throws MqttException { // TODO Auto-generated method stub System.out.println("@waitForCompletion(): waiting for connection to be established."); } @Override public void setUserContext(Object arg0) { // TODO Auto-generated method stub } @Override public void setActionCallback(IMqttActionListener arg0) { // TODO Auto-generated method stub arg0.onSuccess(iMQTTToken); //System.out.println(" " + arg0.onSuccess()); //System.out.println(" " + arg0.onSuccess(iMQTTToken)); iMQTTToken.setActionCallback(synchCallBack); } @Override public boolean isComplete() { // TODO Auto-generated method stub return false; } @Override public Object getUserContext() { // TODO Auto-generated method stub return null; } @Override public Ssortingng[] getTopics() { // TODO Auto-generated method stub return null; } @Override public boolean getSessionPresent() { // TODO Auto-generated method stub return false; } @Override public MqttWireMessage getResponse() { // TODO Auto-generated method stub return null; } @Override public int getMessageId() { // TODO Auto-generated method stub return 0; } @Override public int[] getGrantedQos() { // TODO Auto-generated method stub return null; } @Override public MqttException getException() { // TODO Auto-generated method stub return null; } @Override public IMqttAsyncClient getClient() { // TODO Auto-generated method stub return null; } @Override public IMqttActionListener getActionCallback() { // TODO Auto-generated method stub return null; } }; IMqttActionListener synchCallBack = new IMqttActionListener() { @Override public void onSuccess(IMqttToken arg0) { // TODO Auto-generated method stub System.out.println("@onSuccess: Connection Successful."); } @Override public void onFailure(IMqttToken arg0, Throwable arg1) { // TODO Auto-generated method stub System.out.println("@onFailure: Connection Failed."); setViewEnableState(Bconnect, true); } }; MqttCallback AsynchCallBack = new MqttCallback() { @Override public void messageArrived(Ssortingng topic, MqttMessage msg) throws Exception { // TODO Auto-generated method stub System.out.println("@messageArrived: Message Delivered."); } @Override public void deliveryComplete(IMqttDeliveryToken token) { // TODO Auto-generated method stub System.out.println("@deliveryComplete: Delivery Completed."); } @Override public void connectionLost(Throwable thrw) { // TODO Auto-generated method stub System.out.println("@Connection Lost: Connection Lost."); setViewEnableState(Bconnect, true); } }; 

Nouveau client :

  MqttConnectOptions opts = new MqttConnectOptions(); opts.setCleanSession(CS); opts.setKeepAliveInterval(KATimer); HashMap LWTData = WILLFactory.newWILL("LWT", "LWT MS".getBytes(), 1, false); opts.setWill(LWTData.get(0).getWILLTopic(), LWTData.get(0).getWILLPayLoad(), LWTData.get(0).getWILLQoS(), LWTData.get(0).isWILLRetained()); client = MQTTClientFactory.newClient(IP, PORT, clientID); if (client != null) { System.out.println("client is not null"); client.setCallback(AsynchCB); IMqttToken token = client.connectWithResult(opts); if (client.isConnected()) { System.out.println("Client is Connected"); token.setActionCallback(new IMqttActionListener() { public void onSuccess(IMqttToken arg0) { // TODO Auto-generated method stub System.out.println("synchCB->@onSuccess(): Connection Successful"); try { client.subscribe(TOPIC, QoS); } catch (MqttException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { client.disconnect(); } catch (MqttException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void onFailure(IMqttToken arg0, Throwable arg1) { // TODO Auto-generated method stub System.out.println("synchCB->@onFailure(): Connection Failed"); } }); }else { System.out.println("client is not connected"); } }else { System.out.println("client = null"); } 

Asynch CallBack :

 /** * Asynchronous Callback to inform the user about events that might happens Asynchronously. If it is not used, any pending * messages destined to the client would not be received. */ private static MqttCallback AsynchCB = new MqttCallback() { public void messageArrived(Ssortingng topic, MqttMessage msg) throws Exception { // TODO Auto-generated method stub System.out.println("AsynchCB->@messageArrived(): "); System.out.println("Topic: " + topic); System.out.println("MSG: " + msg.toSsortingng()); } public void deliveryComplete(IMqttDeliveryToken arg0) { // TODO Auto-generated method stub System.out.println("AsynchCB->@deliveryComplete(): "); } public void connectionLost(Throwable arg0) { // TODO Auto-generated method stub System.out.println("AsynchCB->@connectionLost(): "); } }; 

Votre machine sur laquelle réside le client, qui traite votre rappel, peut avoir le port sortant bloqué par le pare-feu de la machine.