1、MQTT的学习研究MQTT的学习研究1、 MQTT学习网站MQTT的官方推荐网站:http:/mqtt.org/software使用IBM 的MQTT协议实现push消息地址:google code 下载MQTT moquette Broker 地址:GIT 下载MQTT moquette client 地址:参考链接http:/dalelane.co.uk/blog/?p=1599http:/www.hardill.me.uk/wordpress/?p=207附加一个好玩的链接http:/www.hardill.me.uk/wordpress/?p=204http:/mosquitto.or
2、g/2、 moquette-mqtt 的使用之mqtt broker的启动在MQTT官网(http:/mqtt.org/software)中有众多MQTT的实现方式。具体参看官网,Moquette是基于Apache Mina 的模型的一个Java MQTT broker。使用过Mina的同学发现其实broker的启动过程就是一个Mina应用的启动。在MQTT moquette 中采用MINA作为底层消息的传递方式本类的目的启动MQTT moquette Broker 的方式,本文的源代码来自 moquette-broker-0.1-jar-with-dependencies.jar 中的ser
3、ver类如果想直接启动 moquette-broker-0.1-jar-with-dependencies.jar的jar文件方式可以执行一些命令实现 java -jar moquette-broker-0.1-jar-with-dependencies.jargoogle code 下载MQTT moquette Broker 地址:GIT 下载MQTT moquette client 地址: 在应用程序中使用MQTT的应用:MQTT moquette 的broker服务启动代码如下:java view plaincopyprint?1. packagecom.etrip.mqtt;2. 3
4、. importjava.io.File;4. importjava.io.IOException;5. import.InetAddress;6. import.InetSocketAddress;7. 8. importorg.apache.mina.core.service.IoAcceptor;9. importorg.apache.mina.core.service.IoServiceStatistics;10. importorg.apache.mina.core.session.IdleStatus;11. importorg.apache.mina.core.session.I
5、oSession;12. importorg.apache.mina.filter.codec.ProtocolCodecFilter;13. importorg.apache.mina.filter.codec.demux.DemuxingProtocolDecoder;14. importorg.apache.mina.filter.codec.demux.DemuxingProtocolEncoder;15. importorg.apache.mina.transport.socket.nio.NioSocketAcceptor;16. importorg.dna.mqtt.moquet
6、te.messaging.spi.impl.SimpleMessaging;17. importorg.dna.mqtt.moquette.proto.ConnAckEncoder;18. importorg.dna.mqtt.moquette.proto.ConnectDecoder;19. importorg.dna.mqtt.moquette.proto.DisconnectDecoder;20. importorg.dna.mqtt.moquette.proto.DisconnectEncoder;21. importorg.dna.mqtt.moquette.proto.MQTTLo
7、ggingFilter;22. importorg.dna.mqtt.moquette.proto.PingReqDecoder;23. importorg.dna.mqtt.moquette.proto.PingRespEncoder;24. importorg.dna.mqtt.moquette.proto.PubAckDecoder;25. importorg.dna.mqtt.moquette.proto.PubAckEncoder;26. importorg.dna.mqtt.moquette.proto.PubCompDecoder;27. importorg.dna.mqtt.m
8、oquette.proto.PubCompEncoder;28. importorg.dna.mqtt.moquette.proto.PubCompMessage;29. importorg.dna.mqtt.moquette.proto.PubRecDecoder;30. importorg.dna.mqtt.moquette.proto.PubRecEncoder;31. importorg.dna.mqtt.moquette.proto.PubRelDecoder;32. importorg.dna.mqtt.moquette.proto.PubRelEncoder;33. import
9、org.dna.mqtt.moquette.proto.PublishDecoder;34. importorg.dna.mqtt.moquette.proto.PublishEncoder;35. importorg.dna.mqtt.moquette.proto.SubAckEncoder;36. importorg.dna.mqtt.moquette.proto.SubscribeDecoder;37. importorg.dna.mqtt.moquette.proto.UnsubAckEncoder;38. importorg.dna.mqtt.moquette.proto.Unsub
10、scribeDecoder;39. importorg.dna.mqtt.moquette.proto.messages.ConnAckMessage;40. importorg.dna.mqtt.moquette.proto.messages.DisconnectMessage;41. importorg.dna.mqtt.moquette.proto.messages.PingRespMessage;42. importorg.dna.mqtt.moquette.proto.messages.PubAckMessage;43. importorg.dna.mqtt.moquette.pro
11、to.messages.PubRecMessage;44. importorg.dna.mqtt.moquette.proto.messages.PubRelMessage;45. importorg.dna.mqtt.moquette.proto.messages.PublishMessage;46. importorg.dna.mqtt.moquette.proto.messages.SubAckMessage;47. importorg.dna.mqtt.moquette.proto.messages.UnsubAckMessage;48. importorg.dna.mqtt.moqu
12、ette.server.MQTTHandler;49. importorg.slf4j.Logger;50. importorg.slf4j.LoggerFactory;51. /*52. *53. *在MQTTmoquette中采用MINA作为底层消息的传递方式54. *55. *本类的目的启动MQTTmoquetteBroker的方式,56. *本文的源代码来自moquette-broker-0.1-jar-with-dependencies.jar中的server类57. *如果想直接启动moquette-broker-0.1-jar-with-dependencies.jar的jar文
13、件方式58. *可以执行一些命令实现59. *java-jarmoquette-broker-0.1-jar-with-dependencies.jar60. *61. *62. *googlecode下载MQTTmoquetteBroker地址:63. *64. *65. *GIT下载MQTTmoquetteclient地址:66. *67. *68. *authorlonggangbai69. *70. *71. */72. publicclassMQTTBrokerProxyServer73. privatestaticfinalLoggerLOG=LoggerFactory.getLo
14、gger(MQTTBrokerProxyServer.class);74. 75. publicstaticfinalStringSTORAGE_FILE_PATH=System.getProperty(user.home)+File.separator+moquette_store.hawtdb;76. privateIoAcceptorm_acceptor;77. SimpleMessagingmessaging;78. 79. publicstaticvoidmain(Stringargs)80. throwsIOException81. 82. newMQTTBrokerProxySe
15、rver().startServer();83. 84. 85. protectedvoidstartServer()throwsIOException86. 87. /编码协议类编码器88. DemuxingProtocolDecoderdecoder=newDemuxingProtocolDecoder();89. decoder.addMessageDecoder(newConnectDecoder();/连接编码90. decoder.addMessageDecoder(newPublishDecoder();/发布编码91. decoder.addMessageDecoder(new
16、PubAckDecoder();/发布回执编码92. decoder.addMessageDecoder(newPubRelDecoder();93. decoder.addMessageDecoder(newPubRecDecoder();/接收编码94. decoder.addMessageDecoder(newPubCompDecoder();95. decoder.addMessageDecoder(newSubscribeDecoder();/订阅编码96. decoder.addMessageDecoder(newUnsubscribeDecoder();/取消订阅编码97. de
17、coder.addMessageDecoder(newDisconnectDecoder();/断开连接编码98. decoder.addMessageDecoder(newPingReqDecoder();/心跳ping请求编码99. 100. /解码协议类解码器101. DemuxingProtocolEncoderencoder=newDemuxingProtocolEncoder();102. 103. encoder.addMessageEncoder(ConnAckMessage.class,newConnAckEncoder();/连接解码104. encoder.addMess
18、ageEncoder(SubAckMessage.class,newSubAckEncoder();/订阅通知解码105. encoder.addMessageEncoder(UnsubAckMessage.class,newUnsubAckEncoder();/取消订阅解码106. encoder.addMessageEncoder(PubAckMessage.class,newPubAckEncoder();/发布回执解码107. encoder.addMessageEncoder(PubRecMessage.class,newPubRecEncoder();/接收解码108. encod
19、er.addMessageEncoder(PubCompMessage.class,newPubCompEncoder();109. encoder.addMessageEncoder(PubRelMessage.class,newPubRelEncoder();110. encoder.addMessageEncoder(PublishMessage.class,newPublishEncoder();/发布解码111. encoder.addMessageEncoder(PingRespMessage.class,newPingRespEncoder();/心跳ping相应解码112. e
20、ncoder.addMessageEncoder(DisconnectMessage.class,newDisconnectEncoder();/断开连接解码113. 114. this.m_acceptor=newNioSocketAcceptor();115. /设置日志的过滤链116. this.m_acceptor.getFilterChain().addLast(logger,newMQTTLoggingFilter(SERVERLOG);117. /设置编码的过滤链118. this.m_acceptor.getFilterChain().addLast(codec,newProt
21、ocolCodecFilter(encoder,decoder);119. /创建业务处理器类120. MQTTHandlerhandler=newMQTTHandler();121. /创建一个处理消息体的消息122. this.messaging=SimpleMessaging.getInstance();123. this.messaging.init();124. /设置消息体125. handler.setMessaging(this.messaging);126. /设置业务处理器类127. this.m_acceptor.setHandler(handler);128. 129.
22、 (NioSocketAcceptor)this.m_acceptor).setReuseAddress(true);130. (NioSocketAcceptor)this.m_acceptor).getSessionConfig().setReuseAddress(true);131. this.m_acceptor.getSessionConfig().setReadBufferSize(2048);132. this.m_acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10);133. this.m_accept
23、or.getStatistics().setThroughputCalculationInterval(10);134. this.m_acceptor.getStatistics().updateThroughput(System.currentTimeMillis();135. /设置端口号136. this.m_acceptor.bind(newInetSocketAddress(1883);137. /获取绑定的本地的ip地址138. LOG.info(Serverbinded+InetAddress.getLocalHost().getHostAddress();139. try14
24、0. Thread.sleep(100000000000000L);141. catch(InterruptedExceptione)142. /TODOAuto-generatedcatchblock143. e.printStackTrace();144. 145. /销毁broker对象的各种信息146. Runtime.getRuntime().addShutdownHook(newThread()147. 148. publicvoidrun()149. MQTTBrokerProxyServer.this.stopServer();150. 151. );152. 153. 154. protectedvoidstopServer()155. LOG.info(Serverstopping.);156. 157. this.messaging.stop();158. /MinaIO统计类159. IoServiceStatisticsstatistics=this.m_acceptor.getStatistics();160. statistics.updateThroughput(System.currentTimeMillis();161. System.out.println(String.format(Totalreadbytes:%d,readthr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1