Study of Mqtt (iv) MOQUETTE-MQTT use of MQTT Blocking API clients subscribe to and receive topic information

Source: Internet
Author: User

After posting the subject information on the above two launch of the broker for MQTT and the server for MQTT, our client needs to subscribe to the relevant information and receive the relevant topic information.

Java code
  1. Package com.etrip.mqtt;
  2. Import java.net.URISyntaxException;
  3. Import org.fusesource.mqtt.client.BlockingConnection;
  4. Import Org.fusesource.mqtt.client.MQTT;
  5. Import Org.fusesource.mqtt.client.Message;
  6. Import Org.fusesource.mqtt.client.QoS;
  7. Import Org.fusesource.mqtt.client.Topic;
  8. Import Org.slf4j.Logger;
  9. Import Org.slf4j.LoggerFactory;
  10. /**
  11. *
  12. * The client segment of MQTT Moquette is used to subscribe to topics and receive topic information
  13. *
  14. * Using a blocked subscription theme
  15. *
  16. * @author Longgangbai
  17. */
  18. Public class Mqttclient {
  19. private static final Logger LOG = Loggerfactory.getlogger (mqttclient.   Class);
  20. Private final static String connection_string = "tcp://192.168.208.46:1883";
  21. Private Final static Boolean clean_start = true;
  22. Private final static short keep_alive = ; Low-power network, but need to get data in time, heartbeat 30s
  23. Private final static String client_id = "PublishService";
  24. public static topic[] topics = {
  25. New Topic ("china/beijing", qos.exactly_once),
  26. New Topic ("China/tianjin", qos.at_least_once),
  27. New Topic ("China/henan", Qos.at_most_once)};
  28. public final static long reconnection_attempt_max=6;
  29. public final static long reconnection_delay=2000;
  30. public final static int send_buffer_size=1024*; Send maximum buffer to 2M
  31. public static void Main (string[] args) {
  32. //Create an Mqtt object
  33. Mqtt Mqtt = new Mqtt ();
  34. Blockingconnection connection=null;
  35. try {
  36. //Set the IP and port of MQTT broker
  37. Mqtt.sethost (connection_string);
  38. //Clear session information before connecting
  39. Mqtt.setcleansession (Clean_start);
  40. //Set number of times to reconnect
  41. Mqtt.setreconnectattemptsmax (Reconnection_attempt_max);
  42. //Set the time interval for re-connection
  43. Mqtt.setreconnectdelay (Reconnection_delay);
  44. //Set heartbeat time
  45. Mqtt.setkeepalive (keep_alive);
  46. //Set the size of the buffer
  47. Mqtt.setsendbuffersize (send_buffer_size);
  48. //Get the Connection object for Mqtt blockingconnection
  49. Connection = Mqtt.blockingconnection ();
  50. //mqtt The creation of a connection
  51. Connection.connect ();
  52. //Create a list of topics related to Mqtt
  53. Topic[] topics = {new Topic ("china/beijing", Qos.at_least_once)};
  54. //Subscribe to related topic information
  55. byte[] qoses = connection.subscribe (topics);
  56. //  
  57. While (true) {
  58. //Receive the message content of the subscription
  59. Message message = Connection.receive ();
  60. //Get the message content of the subscription
  61. byte[] payload = message.getpayload ();
  62. //Process the message then:
  63. Log.info ("mqttclient Message topic=" +message.gettopic () +"Content:" +new String (payload));
  64. //receipts for receipt of messages
  65. Message.ack ();
  66. Thread.Sleep (2000);
  67. }
  68. } catch (URISyntaxException e) {
  69. //TODO auto-generated catch block
  70. E.printstacktrace ();
  71. } catch (Exception e) {
  72. //TODO auto-generated catch block
  73. E.printstacktrace ();
  74. }finally{
  75. try {
  76. Connection.disconnect ();
  77. } catch (Exception e) {
  78. //TODO auto-generated catch block
  79. E.printstacktrace ();
  80. }
  81. }
  82. }
  83. }

Study of Mqtt (iv) MOQUETTE-MQTT use of MQTT Blocking API clients subscribe to and receive topic information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.