1. Start-up vessel: Docker run-p 1883:1883-p 8161:8161-d leo/activemq
If there is no Docker, it is also possible to start activemq directly.
We just need to write publisher and Subscriber when the ACTIVEMQ is ready to start.
2. Need to add 2 maven dependencies:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactid>activemq-all </artifactId>
<version>5.13.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactid>org.eclipse.paho.client.mqttv3</ artifactid>
<version>1.1.0</version>
</dependency>
The first dependency is ACTIVEMQ, the second depends on the details, please refer to: https://eclipse.org/paho/clients/java/
The 3.Publisher code is as follows:
Package leo.mqtt;
Import org.eclipse.paho.client.mqttv3.MqttClient;
Import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
Import org.eclipse.paho.client.mqttv3.MqttException;
Import Org.eclipse.paho.client.mqttv3.MqttMessage;
Import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class Publisher {public static void main (string[] args) {String topic = "Topicofleo";
String content = "Hello leo!"; int QoS = 1;//0:msg is only issued 1 times and does not require confirmation. The 1:msg is issued at least 1 times and needs confirmation.
2. Hair and only 1 times, and need to do 4 waves.
String broker = "tcp://127.0.0.1:1883";
String clientId = "Publisher1";
Memorypersistence persistence = new memorypersistence ();
try {mqttclient client = new Mqttclient (broker, clientId, persistence);
Mqttconnectoptions connopts = new Mqttconnectoptions ();
Connopts.setcleansession (false);//first start whether to clear session client.connect (connopts);
Mqttmessage message = new Mqttmessage (Content.getbytes ());
Message.setqos (QoS);
Client.publish (topic, message); Client.disconnecT ();
System.exit (0);
catch (mqttexception me) {System.out.println ("reason" + me.getreasoncode ());
System.out.println ("msg" + me.getmessage ());
System.out.println ("loc" + Me.getlocalizedmessage ());
SYSTEM.OUT.PRINTLN ("Cause" + me.getcause ());
System.out.println ("excep" + Me);
Me.printstacktrace ();
}
}
}
The 4.subscriber code is as follows:
Package leo.mqtt;
Import Org.eclipse.paho.client.mqttv3.IMqttMessageListener;
Import org.eclipse.paho.client.mqttv3.MqttClient;
Import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
Import org.eclipse.paho.client.mqttv3.MqttException;
Import Org.eclipse.paho.client.mqttv3.MqttMessage;
Import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class Subscriber {public static void main (string[] args) {String topic = "Topicofleo";
String broker = "tcp://127.0.0.1:1883";
String clientId = "Subscriber1";
Memorypersistence persistence = new memorypersistence ();
try {mqttclient client = new Mqttclient (broker, clientId, persistence);
Mqttconnectoptions connopts = new Mqttconnectoptions ();
Connopts.setcleansession (FALSE);
Client.connect (connopts); Client.subscribe (topic, new Imqttmessagelistener () {@Override public void messagearrived (String topic, Mqttmessa GE message) throws Exception {System.out.println (topic + "->" + MessaGE);
}
});
catch (mqttexception me) {System.out.println ("reason" + me.getreasoncode ());
System.out.println ("msg" + me.getmessage ());
System.out.println ("loc" + Me.getlocalizedmessage ());
SYSTEM.OUT.PRINTLN ("Cause" + me.getcause ());
System.out.println ("excep" + Me);
Me.printstacktrace ();
}
}
}
5. Run Subscriber first, and then run Publisher to see the effect.
This is the end of the introductory demo.