package com.missile.mqtt.publisher;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class PublishController {
private static final String HOST = "tcp://127.0.0.1:61613";
private String TOPIC;
private String MESSAGE;
private static final String clientid = "server";
private MqttClient client;
private MqttTopic topic;
private MqttMessage message;
private String username = "admin";
private String password = "password";
@RequestMapping(value = "/", method = RequestMethod.GET)
public String publish0() {
return "publish";
}
@RequestMapping(value = "/publish", method = RequestMethod.GET)
public String publish() {
return "publish";
}
@RequestMapping(value = "/redirect")
public String doPublish(HttpServletRequest request) throws MqttPersistenceException, MqttException {
// 获取前台传过来的两个参数
TOPIC = request.getParameter("topic");
MESSAGE = request.getParameter("message");
// new mqttClient
// MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(HOST, clientid, new MemoryPersistence());
// 与activeMQ连接的方法
connect();
// new mqttMessage
message = new MqttMessage();
// 设置服务质量
message.setQos(2);
// 设置是否在服务器中保存消息体
message.setRetained(true);
// 设置消息的内容
message.setPayload(MESSAGE.getBytes());
// 发布
publish(topic, message);
System.out.println("已发送");
return "result";
}
private void connect() {
// new mqttConnection 用来设置一些连接的属性
MqttConnectOptions options = new MqttConnectOptions();
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
// 换而言之,设置为false时可以客户端可以接受离线消息
options.setCleanSession(false);
// 设置连接的用户名和密码
options.setUserName(username);
options.setPassword(password.toCharArray());
// 设置超时时间
options.setConnectionTimeout(10);
// 设置会话心跳时间
options.setKeepAliveInterval(20);
try {
// 设置回调类
client.setCallback(new PushCallback());
// 连接
client.connect(options);
// 获取activeMQ上名为TOPIC的topic
topic = client.getTopic(TOPIC);
} catch (Exception e) {
e.printStackTrace();
}
}
public void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException, MqttException {
// 发布的方法
// new mqttDeliveryToken
MqttDeliveryToken token = topic.publish(message);
// 发布
token.waitForCompletion();
System.out.println("message is published completely! " + token.isComplete());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于MQTT的实时消息推送系统 (129个子文件)
app-release.apk 3.92MB
gradlew.bat 2KB
fileSnapshots.bin 2.23MB
classAnalysis.bin 715KB
jarAnalysis.bin 313KB
fileHashes.bin 283KB
taskHistory.bin 137KB
taskHistory.bin 66KB
taskJars.bin 24KB
resourceHashesCache.bin 20KB
last-build.bin 1B
built.bin 0B
PublishController.class 4KB
PushCallback.class 2KB
.classpath 3KB
org.eclipse.wst.common.component 491B
org.eclipse.wst.jsdt.ui.superType.container 49B
.gitignore 127B
.gitignore 8B
build.gradle 1KB
build.gradle 575B
settings.gradle 16B
gradlew 5KB
app.iml 12KB
MqttSubscriber.iml 868B
spring-context-4.2.4.RELEASE.jar 1.05MB
spring-core-4.2.4.RELEASE.jar 1.03MB
spring-webmvc-4.2.4.RELEASE.jar 854KB
spring-web-4.2.4.RELEASE.jar 749KB
spring-beans-4.2.4.RELEASE.jar 714KB
spring-test-4.2.4.RELEASE.jar 549KB
spring-orm-4.2.4.RELEASE.jar 455KB
spring-websocket-4.2.4.RELEASE.jar 427KB
spring-jdbc-4.2.4.RELEASE.jar 413KB
jstl-1.2.jar 405KB
spring-aop-4.2.4.RELEASE.jar 361KB
spring-jms-4.2.4.RELEASE.jar 273KB
spring-tx-4.2.4.RELEASE.jar 259KB
spring-expression-4.2.4.RELEASE.jar 257KB
spring-webmvc-portlet-4.2.4.RELEASE.jar 173KB
org.eclipse.paho.client.mqttv3-1.0.1.jar 167KB
spring-oxm-4.2.4.RELEASE.jar 82KB
commons-logging-1.2.jar 60KB
spring-aspects-4.2.4.RELEASE.jar 57KB
gradle-wrapper.jar 52KB
spring-instrument-tomcat-4.2.4.RELEASE.jar 10KB
spring-instrument-4.2.4.RELEASE.jar 7KB
PublishController.java 4KB
MQTTService.java 3KB
MainActivity.java 2KB
PushCallback.java 2KB
PushCallback.java 1KB
ExampleInstrumentedTest.java 792B
ExampleUnitTest.java 432B
.jsdtscope 503B
publish.jsp 757B
result.jsp 380B
taskHistory.lock 17B
fileHashes.lock 17B
fileContent.lock 17B
javaCompile.lock 17B
cache.properties.lock 2B
MANIFEST.MF 39B
org.eclipse.wst.jsdt.ui.superType.name 6B
ic_launcher_round.png 15KB
ic_launcher_round.png 10KB
ic_launcher.png 9KB
ic_launcher_round.png 7KB
ic_launcher.png 6KB
snow.png 5KB
ic_launcher_round.png 5KB
ic_launcher.png 4KB
ic_launcher.png 3KB
ic_launcher_round.png 3KB
ic_launcher.png 2KB
org.eclipse.jdt.core.prefs 364B
proguard-rules.pro 772B
.project 1KB
gradle.properties 747B
local.properties 512B
gradle-wrapper.properties 236B
cache.properties 51B
workspace.xml 168KB
ic_launcher_background.xml 6KB
ic_launcher_foreground.xml 2KB
misc.xml 2KB
springmvc-servlet.xml 1KB
AndroidManifest.xml 1KB
activity_main.xml 900B
com_android_support_test_espresso_espresso_idling_resource_3_0_2.xml 765B
com_android_support_animated_vector_drawable_27_1_1.xml 742B
com_android_support_support_vector_drawable_27_1_1.xml 737B
com_android_support_support_core_utils_27_1_1.xml 712B
com_android_support_test_espresso_espresso_core_3_0_2.xml 710B
com_android_support_support_fragment_27_1_1.xml 702B
com_android_support_support_core_ui_27_1_1.xml 697B
com_android_support_support_compat_27_1_1.xml 692B
android_arch_lifecycle_livedata_core_1_1_0.xml 688B
com_android_support_appcompat_v7_27_1_1.xml 682B
android_arch_lifecycle_viewmodel_1_1_0.xml 668B
共 129 条
- 1
- 2
资源评论
newsuperior
- 粉丝: 5
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功