package com.mqtt.recvice.mqttrecvice.mqtt;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MqttPushClient {
private static final Logger log = LoggerFactory.getLogger(MqttPushClient.class);
private static MqttClient client;
public static MqttClient getClient() {
return client;
}
public static void setClient(MqttClient client) {
MqttPushClient.client = client;
}
private MqttConnectOptions getOption(String userName, String password, int outTime, int KeepAlive) {
//MQTT连接设置
MqttConnectOptions option = new MqttConnectOptions();
//设置是否清空session,false表示服务器会保留客户端的连接记录,true表示每次连接到服务器都以新的身份连接
option.setCleanSession(true);
//设置连接的用户名
option.setUserName(userName);
//设置连接的密码
option.setPassword(password.toCharArray());
//设置超时时间 单位为秒
option.setConnectionTimeout(outTime);
//设置会话心跳时间 单位为秒 服务器会每隔(1.5*keepTime)秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
option.setKeepAliveInterval(KeepAlive);
//setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
//option.setWill(topic, "close".getBytes(), 2, true);
return option;
}
/**
* 连接
* @param host
* @param clientID
* @param username
* @param password
* @param timeout
* @param KeepAlive
*/
public void connect(String host, String clientID, String username, String password, int timeout, int KeepAlive){
MqttClient client;
try {
client = new MqttClient(host, clientID, new MemoryPersistence());
MqttConnectOptions options = getOption(username,password,timeout,KeepAlive);
MqttPushClient.setClient(client);
try {
client.setCallback(new PushCallback());
if (!client.isConnected()) {
client.connect(options);
log.info("MQTT连接成功");
}else {//这里的逻辑是如果连接成功就重新连接
client.disconnect();
client.connect(options);
log.info("MQTT断连成功");
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 断线重连
* @throws Exception
*/
public Boolean reConnect() throws Exception {
Boolean isConnected = false;
if(null != client) {
client.connect();
if(client.isConnected()){
isConnected = true;
}
}
return isConnected;
}
/**
* 发布,默认qos为0,非持久化
* @param topic
* @param pushMessage
*/
public void publish(String topic,String pushMessage){
publish(0, false, topic, pushMessage);
}
/**
* 发布
* @param qos
* @param retained
* @param topic
* @param pushMessage
*/
public void publish(int qos,boolean retained,String topic,String pushMessage){
MqttMessage message = new MqttMessage();
message.setQos(qos);
message.setRetained(retained);
message.setPayload(pushMessage.getBytes());
MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic);
if(null == mTopic){
log.error("MQTT topic 不存在");
}
MqttDeliveryToken token;
try {
token = mTopic.publish(message);
token.waitForCompletion();
} catch (MqttPersistenceException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 订阅某个主题,qos默认为0
* @param topic
*/
public void subscribe(String topic){
subscribe(topic,0);
}
/**
* 订阅某个主题
* @param topic
* @param qos
*/
public void subscribe(String topic,int qos){
try {
MqttPushClient.getClient().subscribe(topic, qos);
} catch (MqttException e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
spring-boot-mqtt(1).zip (75个子文件)
mqtt-recvice
mvnw.cmd 6KB
mvnw 9KB
src
test
java
com
mqtt
recvice
mqttrecvice
MqttRecviceApplicationTests.java 359B
main
resources
application.properties 407B
java
com
mqtt
recvice
mqttrecvice
MqttRecviceApplication.java 3KB
mqtt
MqttConfiguration.java 2KB
MqttPushClient.java 5KB
PushCallback.java 2KB
target
generated-test-sources
test-annotations
generated-sources
annotations
classes
application.properties 573B
com
mqtt
recvice
mqttrecvice
MqttRecviceApplication$1.class 2KB
MqttRecviceApplication.class 3KB
mqtt
MqttPushClient.class 5KB
PushCallback.class 2KB
MqttConfiguration.class 3KB
test-classes
com
mqtt
recvice
mqttrecvice
MqttRecviceApplicationTests.class 667B
mqtt-recvice.iml 6KB
.idea
libraries
Maven__org_eclipse_paho_org_eclipse_paho_client_mqttv3_1_2_0.xml 681B
Maven__org_apache_logging_log4j_log4j_api_2_11_1.xml 573B
Maven__org_springframework_retry_spring_retry_1_2_2_RELEASE.xml 647B
Maven__org_springframework_spring_beans_5_1_3_RELEASE.xml 623B
Maven__ch_qos_logback_logback_core_1_2_3.xml 547B
Maven__org_slf4j_slf4j_api_1_7_25.xml 513B
Maven__junit_junit_4_12.xml 455B
Maven__org_springframework_spring_jcl_5_1_3_RELEASE.xml 609B
Maven__com_jayway_jsonpath_json_path_2_4_0.xml 546B
Maven__org_springframework_spring_tx_5_1_3_RELEASE.xml 602B
Maven__javax_annotation_javax_annotation_api_1_3_2.xml 611B
Maven__ch_qos_logback_logback_classic_1_2_3.xml 568B
Maven__org_springframework_spring_context_5_1_3_RELEASE.xml 637B
Maven__org_springframework_boot_spring_boot_starter_aop_2_1_1_RELEASE.xml 720B
Maven__org_springframework_integration_spring_integration_core_5_1_1_RELEASE.xml 748B
Maven__net_bytebuddy_byte_buddy_1_9_5.xml 529B
Maven__org_reactivestreams_reactive_streams_1_0_2.xml 595B
Maven__org_springframework_spring_core_5_1_3_RELEASE.xml 616B
Maven__org_springframework_integration_spring_integration_stream_5_1_1_RELEASE.xml 762B
Maven__org_xmlunit_xmlunit_core_2_6_2.xml 535B
Maven__org_springframework_boot_spring_boot_test_2_1_1_RELEASE.xml 671B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 700B
Maven__org_hamcrest_hamcrest_core_1_3.xml 532B
Maven__org_springframework_boot_spring_boot_starter_logging_2_1_1_RELEASE.xml 748B
Maven__net_minidev_accessors_smart_1_2.xml 542B
Maven__org_ow2_asm_asm_5_0_4.xml 472B
Maven__org_springframework_boot_spring_boot_2_1_1_RELEASE.xml 636B
Maven__org_springframework_spring_test_5_1_3_RELEASE.xml 616B
Maven__io_projectreactor_reactor_core_3_2_3_RELEASE.xml 615B
Maven__org_hamcrest_hamcrest_library_1_3.xml 553B
Maven__org_slf4j_jul_to_slf4j_1_7_25.xml 534B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_11_1.xml 608B
Maven__org_springframework_boot_spring_boot_starter_2_1_1_RELEASE.xml 692B
Maven__org_mockito_mockito_core_2_23_4.xml 542B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_1_1_RELEASE.xml 734B
Maven__org_springframework_spring_aop_5_1_3_RELEASE.xml 609B
Maven__org_springframework_spring_messaging_5_1_3_RELEASE.xml 651B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_1_1_RELEASE.xml 769B
Maven__org_springframework_boot_spring_boot_starter_test_2_1_1_RELEASE.xml 727B
Maven__org_objenesis_objenesis_2_6.xml 508B
Maven__org_skyscreamer_jsonassert_1_5_0.xml 537B
Maven__org_springframework_spring_expression_5_1_3_RELEASE.xml 658B
Maven__org_aspectj_aspectjweaver_1_9_2.xml 542B
Maven__org_springframework_boot_spring_boot_starter_integration_2_1_1_RELEASE.xml 776B
Maven__org_assertj_assertj_core_3_11_1.xml 542B
Maven__net_bytebuddy_byte_buddy_agent_1_9_5.xml 571B
Maven__net_minidev_json_smart_2_3.xml 507B
Maven__org_yaml_snakeyaml_1_23.xml 495B
Maven__org_springframework_integration_spring_integration_mqtt_5_1_1_RELEASE.xml 748B
misc.xml 451B
compiler.xml 827B
workspace.xml 26KB
encodings.xml 177B
inspectionProfiles
Project_Default.xml 265KB
modules.xml 271B
.mvn
wrapper
maven-wrapper.jar 47KB
maven-wrapper.properties 116B
pom.xml 2KB
.gitignore 268B
共 75 条
- 1
资源评论
- qiangbaixiao2019-04-01还不错,但是还可以改进哦
qq_34974974
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功