下载地址demo 及apache-apollo-1.7.1,mqttv3.jar
package com.mqtt.demo;
import java.util.concurrent.ScheduledExecutorService;
import com.ibm.micro.client.mqttv3.MqttClient;
import com.ibm.micro.client.mqttv3.MqttConnectOptions;
import com.ibm.micro.client.mqttv3.MqttException;
import com.ibm.micro.client.mqttv3.MqttTopic;
import com.ibm.micro.client.mqttv3.internal.MemoryPersistence;
/**
*
* @author bxh
*
*/
public class Client {
// apache-apollo-1.7.1 配置的地址
public static final String HOST = "tcp://127.0.0.1:61613";
//消息主题
public static final String TOPIC = "topic_news";
// client标识
private static final String clientid = "bxh";
private MqttClient client;
//mqtt 选项
private MqttConnectOptions options;
//apache-apollo-1.7.1 服务器用户及密码
private String userName = "admin";
private String passWord = "password";
private void start() {
try {
//初始化mqtt
client = new MqttClient(HOST, clientid, new MemoryPersistence());
options = new MqttConnectOptions();
// 设置连接的用户名
options.setUserName(userName);
// 设置连接的密码
options.setPassword(passWord.toCharArray());
options.setCleanSession(true);
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒
options.setKeepAliveInterval(20);
// 设置回调
client.setCallback(new PushCallback());
//加载主题对象类
MqttTopic topic = client.getTopic(TOPIC);
//setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
options.setWill(topic, "Will".getBytes(), 2, true);
//连接服务器
client.connect(options);
//订阅关注这个主题消息 名字叫 topic_news
String[] topic1 = {TOPIC};
int[] Qos = {1};
//关注
client.subscribe(topic1, Qos);
//一旦关注 了,有消息就会调用回调,也就是pushcallback.java 的messageArrived 方法
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws MqttException {
Client client = new Client();
client.start();
}
}
package com.mqtt.demo;
import com.ibm.micro.client.mqttv3.MqttClient;
import com.ibm.micro.client.mqttv3.MqttConnectOptions;
import com.ibm.micro.client.mqttv3.MqttDeliveryToken;
import com.ibm.micro.client.mqttv3.MqttException;
import com.ibm.micro.client.mqttv3.MqttMessage;
import com.ibm.micro.client.mqttv3.MqttPersistenceException;
import com.ibm.micro.client.mqttv3.MqttSecurityException;
import com.ibm.micro.client.mqttv3.MqttTopic;
import com.ibm.micro.client.mqttv3.internal.MemoryPersistence;
/**
* @author bxh
*/
public class Server {
public static final String HOST = "tcp://127.0.0.1:61613";
public static final String TOPIC_1 = "topic_news";
public static final String TOPIC_2 = "topic_weather";
private static final String clientid = "server";
private MqttClient client;
private MqttTopic topic_news;
private MqttTopic topic_tv;
private String userName = "admin";
private String passWord = "password";
public Server() {
try {
client = new MqttClient(HOST, clientid, new MemoryPersistence());
} catch (MqttException mqttException) {
mqttException.printStackTrace();
}
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setUserName(userName);
options.setPassword(passWord.toCharArray());
// 设置超时时间
//options.setConnectionTimeout(10);
// 设置会话心跳时间
//options.setKeepAliveInterval(20);
try {
client.setCallback(new PushCallback());
client.connect(options);
topic_news = client.getTopic(TOPIC_1);
topic_tv = client.getTopic(TOPIC_2);
} catch (Exception e) {
e.printStackTrace();
}
}
//该方法用来发布一个主题消息
/**
* 消息推送
* @param topic 主题类对象
* @param message 消息
*
*/
public void publish(MqttTopic topic , MqttMessage message) {
try {
//推送消息
MqttDeliveryToken token = topic.publish(message);
//等待完成
token.waitForCompletion();
System.out.println("isComplete"+ token.isComplete());
} catch (MqttSecurityException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
}
public static void main(String[] args)throws MqttException {
//创建本类
Server server = new Server();
//创建消息类
MqttMessage message1 = new MqttMessage();
message1.setQos(2);
//设置保留
message1.setRetained(true);
//消息内容
message1.setPayload("发布新闻内容。。。。。。。。。。。".getBytes());
//推送消息对象到指定主题
server.publish(server.topic_news , message1);
MqttMessage message2 = new MqttMessage();
message2.setQos(2);
message2.setRetained(true);
message2.setPayload("发布明日天气内容。。。。。。。。。。。".getBytes());
server.publish(server.topic_tv , message2);
}
}
package com.mqtt.demo;
import com.ibm.micro.client.mqttv3.MqttCallback;
import com.ibm.micro.client.mqttv3.MqttDeliveryToken;
import com.ibm.micro.client.mqttv3.MqttException;
import com.ibm.micro.client.mqttv3.MqttMessage;
import com.ibm.micro.client.mqttv3.MqttTopic;
/**
*
* @author bxh
*
*/
public class PushCallback implements MqttCallback {
@Override
public void connectionLost(Throwable cause) {
System.out.println("断开连接");
}
/**
* 消息到达
*/
@Override
public void deliveryComplete(MqttDeliveryToken token) {
System.out.println("deliveryComplete ->" + token.isComplete());
}
/**
* 接收消息
*/
@Override
public void messageArrived(MqttTopic topic, MqttMessage message){
System.out.println("接收消息主题 :" + topic);
System.out.println("接收消息 Qos:" + message.getQos());
try {
System.out.println("接收消息内容 :" + new String(message.getPayload()));
} catch (MqttException e) {
e.printStackTrace();
}
}
}