1.启动容器:docker run -p 1883:1883 -p 8161:8161 -d leo/activemq
如果没有Docker,直接启动ActiveMQ也可以.
ActiveMQ启动好后,我们只需要写好publisher和subscriber就好了.
2.需要添加2个maven依赖:
<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>
第一个依赖是activemq的,
第二个依赖的详情,请参考:https://eclipse.org/paho/clients/java/
3.Publisher代码如下:
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只发1次,并且不需要确认. 1:msg至少发1次,需要确认. 2.发且仅发1次,并且需要进行4次挥手.
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);//第一次启动是否清除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();
}
}
}
4.subscriber代码如下:
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, MqttMessage 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.先运行subscriber,再运行publisher,即可看到效果.
入门demo到此结束.