Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package mqtt;
  2.  
  3. import org.eclipse.paho.client.mqttv3.MqttClient;
  4. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  5. import org.eclipse.paho.client.mqttv3.MqttException;
  6. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  7.  
  8. public class MqttSubscribe {
  9. static int qos = 2;
  10.  
  11. public static void main(String[] args) {
  12.  
  13. String topic = "sensor/temp";
  14. String broker = "ws://10.129.144.26:9001";
  15. String clientId = "subsensor";
  16.  
  17.  
  18. MemoryPersistence persistence = new MemoryPersistence();
  19.  
  20. try {
  21. MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
  22. MqttConnectOptions connOpts = new MqttConnectOptions();
  23. connOpts.setCleanSession(true);
  24. System.out.println("Connecting to broker: "+broker);
  25. sampleClient.connect(connOpts);
  26. System.out.println("Connected");
  27. MensagemListener mensagem = new MensagemListener();
  28. sampleClient.subscribe("sensor/temp", mensagem);
  29.  
  30.  
  31. } catch(MqttException me) {
  32. System.out.println("reason "+me.getReasonCode());
  33. System.out.println("msg "+me.getMessage());
  34. System.out.println("loc "+me.getLocalizedMessage());
  35. System.out.println("cause "+me.getCause());
  36. System.out.println("excep "+me);
  37. me.printStackTrace();
  38. }
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement