Benpian record it chose to develop and test MQTT server, client, JAVA client

MQTT server selection and installation process;
MQTT client test tool installation and testing;
MQTT JAVA client selection and development, testing,


MQTT server selection and installation

The current mainstream open source server MQTT mainly the following three:
1)  EMQX : GitHub 4882 Stars
2)  mosquitto : 1645 Stars GitHub
3)  Apollo : an upgraded version of ActiveMQ, github 109 stars

We chose this MQTT EMQX as a server, first go to EMQ official website to download EMQX installed version, up to now the latest version to V4.0.4

See the examiner network installation steps given help documentation

According to the official website operation can not start the service, card emqx start over here in
  this step is very pit, be sure to install the system needs a good ERLANG development environment, ERLANG jump address , probably because EMQX is ERLANG to explain or develop

After starting the installation is complete, if it is installed in the machine, you can open in a browser http://127.0.0.1 : 18083, enter the default username "admin" and the default password "public", can successfully enter the Admin Console Help EMQX server installation is complete.
Screenshot micro-channel _20200316231950.png

MQTT client installation and testing

Client testing tools recommended MQTTBox

MQTTBox There are two ways: one for Chrome plug-in; another installation program for Windows.

1) Chrome Chrome plug-in plug-ins directly to the market to search mqttbox (over the wall needed)
2) Windows installation package here: Download

MQTT server connection:

Referring next to FIG disposed connection server, protocol selection mqtt / tcp; Host port 1883;
other temporarily affect test parameters, its own research interest.

Screenshot micro-channel _20200316232820.png

Successfully connect to the server, Connected icon will be displayed in green
Screenshot micro-channel _20200316233123.png

Add a subscriber

Orders topic is "hello"; Qos is the default 0

Screenshot micro-channel _20200316233356.png

Add a publisher

Post a topic for the "hello" message period { "hello": "world"}, Qos default 0

News release, subscribers will receive this message window immediately. At this point, the client test is complete.
Screenshot micro-channel _20200316233529.png

MQTT JAVA client development and testing

JAVA client selects
EMQX SDK_TOOLS open source client tools under development in almost all the languages listed. JAVA SDK currently under the main Eclipse Paho, Xenqtt, MeQanTT, mqtt -client.

Because a Spring Integration Endpoints to connect officially supported DEMO is used inside Eclipse Paho, so this time we chose the JAVA client is Eclipse Paho.

POM introduced

  <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.0</version>
        </dependency>

 

Examples of release-side development

 1 import org.eclipse.paho.client.mqttv3.MqttClient;
 2 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 3 import org.eclipse.paho.client.mqttv3.MqttException;
 4 import org.eclipse.paho.client.mqttv3.MqttMessage;
 5 import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 6 
 7 /**
 8  * 发布端未例
 9  */
10 public class PublishSample {
11     public static void main(String[] args) {
12 
13         Topic = String "MQTT / OKOK" ;
 14          String Content = "Hello ha. 1" ;
 15          int QoS =. 1 ;
 16          String Broker = "TCP: //127.0.0.1: 1883" ;
 . 17          String the userName = "Test" ;
 18 is          password = String "Test" ;
 . 19          String clientId = "pubClient1" ;
 20 is          // memory storage 
21 is          MemoryPersistence Persistence = new new MemoryPersistence ();
 22 is  
23 is          the try {
 24              // create a client 
25             SampleClient = MqttClient new new MqttClient (Broker, clientId, Persistence);
 26              // create a link parameter 
27              MqttConnectOptions connOpts = new new MqttConnectOptions ();
 28              // remember the state upon reboot and reconnect 
29              connOpts.setCleanSession ( false );
 30              // set the name of the user connection 
31 is              connOpts.setUserName (the userName);
 32              connOpts.setPassword (password.toCharArray ());
 33 is              // establish a connection 
34 is              sampleClient.connect (connOpts);
 35              // create message 
36             Message = MqttMessage new new MqttMessage (content.getBytes ());
 37 [              // set quality of service message 
38 is              message.setQos (QoS);
 39              // release message 
40              sampleClient.publish (Topic, Message);
 41 is              // disconnect 
42 is              sampleClient.disconnect ();
 43 is              // Close the client 
44 is              sampleClient.close ();
 45          } the catch (MqttException Me) {
 46 is              System.out.println ( "reason" + me.getReasonCode ());
 47              the System.out .println ( "msg" + me.getMessage());
48             System.out.println("loc " + me.getLocalizedMessage());
49             System.out.println("cause " + me.getCause());
50             System.out.println("excep " + me);
51             me.printStackTrace();
52         }
53     }
54 }

 

Subscribe to end publication instance

 1 import org.eclipse.paho.client.mqttv3.*;
 2 import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 3 
 4 /**
 5  * 订阅端
 6  */
 7 public class SubscribeSample {
 8     public static void main(String[] args) throws MqttException {
 9         String HOST = "tcp://127.0.0.1:1883";
10         String TOPIC = "mqtt/okok";
11         int qos = 1;
12         String clientid = "subClient1";
 13 is          String the userName = "Test" ;
 14          String = passWord, "Test" ;
 15          the try {
 16              // Host name is the host, the client ID for Test clientid connected MQTT i.e., generally represents the unique identifier to the client, MemoryPersistence provided clientid storage format, the default is to save memory 
. 17              MqttClient Client = new new MqttClient (the HOST, clientid, new new MemoryPersistence ());
 18 is              // connected MQTT provided 
. 19              MqttConnectOptions Options = new new MqttConnectOptions ();
 20 is              // settings are empty session here If set to false indicates that the server retains the client's connection record, set here to represent true every time you connect to the server are connected to a new identity 
21             options.setCleanSession ( to true );
 22 is              // username provided connection 
23 is              options.setUserName (the userName);
 24              // password connection 
25              options.setPassword (passWord.toCharArray ());
 26 is              // set the timeout time is sec 
27              options.setConnectionTimeout (10 );
 28              // set the session seconds heartbeat message server sends the client determines whether the line to the client every 1.5 * 20 seconds, but this method does not reconnection mechanism 
29              options.setKeepAliveInterval (20 is );
 30              // set the callback function 
31 is              client.setCallback ( new new MqttCallback () {
 32 
33                 @Override
34                 public void connectionLost(Throwable cause) {
35                     System.out.println("connectionLost");
36                 }
37 
38                 @Override
39                 public void messageArrived(String topic, MqttMessage message) throws Exception {
40                     System.out.println("topic:"+topic);
41                     System.out.println("Qos:"+message.getQos());
42                     System.out.println("message content:"+new String(message.getPayload()));
43 
44                 }
45 
46                 @Override
47                 public void deliveryComplete(IMqttDeliveryToken token) {
48                     System.out.println("deliveryComplete---------"+ token.isComplete());
49                 }
50 
51             });
52             client.connect(options);
53             //订阅消息
54             client.subscribe(TOPIC, qos);
55         } catch (Exception e) {
56             e.printStackTrace();
57         }
58     }
59 }

 

Next: the MQTT integrated into Spring Mvc

More Reference website:

1.EMQX Help Center  https://docs.emqx.io/broker/latest/cn/
2.Spring the MQTT Support  https://docs.spring.io/spring-integration/docs/5.0.8.RELEASE/reference/ html / mqtt.html