first commit
This commit is contained in:
43
starter/mica-mqtt-server-jfinal-plugin/README.md
Normal file
43
starter/mica-mqtt-server-jfinal-plugin/README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# jfinal mica-mqtt-server
|
||||
|
||||
## 使用
|
||||
|
||||
#### 1. 添加依赖
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.dromara.mica-mqtt</groupId>
|
||||
<artifactId>mica-mqtt-server-jfinal-plugin</artifactId>
|
||||
<version>${最新版本}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
#### 2. 删除 jfinal-demo 中的 slf4j-nop 依赖
|
||||
|
||||
#### 3. 添加 slf4j-log4j12
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.33</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
#### 4. 插件配置
|
||||
```java
|
||||
MqttServerPlugin plugin = new MqttServerPlugin();
|
||||
plugin.config(mqttServerCreator -> {
|
||||
// mqttServerCreator 上有很多方法,详见 mica-mqtt-core
|
||||
mqttServerCreator
|
||||
.enableMqtt()
|
||||
.enableMqttWs()
|
||||
.enableMqttHttpApi()
|
||||
;
|
||||
});
|
||||
plugin.start();
|
||||
```
|
||||
|
||||
#### 5. 插件使用
|
||||
```java
|
||||
// 更多方法可以直接使用 MqttServerKit 点出来
|
||||
MqttServerKit.publish(String clientId, String topic, byte[] payload);
|
||||
```
|
||||
32
starter/mica-mqtt-server-jfinal-plugin/pom.xml
Normal file
32
starter/mica-mqtt-server-jfinal-plugin/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.dromara.mica-mqtt</groupId>
|
||||
<artifactId>starter</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<artifactId>mica-mqtt-server-jfinal-plugin</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<url>https://mica-mqtt.dreamlu.net/guide/jfinal/server.html</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara.mica-mqtt</groupId>
|
||||
<artifactId>mica-mqtt-server</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jfinal</groupId>
|
||||
<artifactId>jfinal</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.mica.mqtt.jfinal.server;
|
||||
|
||||
import org.dromara.mica.mqtt.codec.MqttQoS;
|
||||
import org.dromara.mica.mqtt.core.server.MqttServer;
|
||||
import org.tio.core.ChannelContext;
|
||||
|
||||
/**
|
||||
* mica mqtt server kit
|
||||
*
|
||||
* @author L.cm
|
||||
* @author ChangJin Wei (魏昌进)
|
||||
*/
|
||||
public class MqttServerKit {
|
||||
private static MqttServer mqttServer;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param mqttServer MqttServer
|
||||
*/
|
||||
static void init(MqttServer mqttServer) {
|
||||
MqttServerKit.mqttServer = mqttServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param clientId clientId
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String clientId, String topic, Object payload) {
|
||||
return mqttServer.publish(clientId, topic, payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param clientId clientId
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param qos MqttQoS
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String clientId, String topic, Object payload, MqttQoS qos) {
|
||||
return mqttServer.publish(clientId, topic, payload, qos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param clientId clientId
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param retain 是否在服务器上保留消息
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String clientId, String topic, Object payload, boolean retain) {
|
||||
return mqttServer.publish(clientId, topic, payload, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param clientId clientId
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param qos MqttQoS
|
||||
* @param retain 是否在服务器上保留消息
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String clientId, String topic, Object payload, MqttQoS qos, boolean retain) {
|
||||
return mqttServer.publish(clientId, topic, payload, qos, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息给所以的在线设备
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publishAll(String topic, Object payload) {
|
||||
return mqttServer.publishAll(topic, payload, MqttQoS.QOS0, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param qos MqttQoS
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publishAll(String topic, Object payload, MqttQoS qos) {
|
||||
return mqttServer.publishAll(topic, payload, qos, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息给所以的在线设备
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param retain 是否在服务器上保留消息
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publishAll(String topic, Object payload, boolean retain) {
|
||||
return mqttServer.publishAll(topic, payload, MqttQoS.QOS0, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息给所以的在线设备
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param qos MqttQoS
|
||||
* @param retain 是否在服务器上保留消息
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publishAll(String topic, Object payload, MqttQoS qos, boolean retain) {
|
||||
return mqttServer.publishAll(topic, payload, qos, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 ChannelContext
|
||||
*
|
||||
* @param clientId clientId
|
||||
* @return ChannelContext
|
||||
*/
|
||||
public static ChannelContext getChannelContext(String clientId) {
|
||||
return mqttServer.getChannelContext(clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端主动断开连接
|
||||
*
|
||||
* @param clientId clientId
|
||||
*/
|
||||
public static void close(String clientId) {
|
||||
mqttServer.close(clientId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.mica.mqtt.jfinal.server;
|
||||
|
||||
import com.jfinal.plugin.IPlugin;
|
||||
import org.dromara.mica.mqtt.core.server.MqttServer;
|
||||
import org.dromara.mica.mqtt.core.server.MqttServerCreator;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* mica mqtt server 插件
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class MqttServerPlugin implements IPlugin {
|
||||
private final MqttServerCreator serverCreator;
|
||||
private MqttServer mqttServer;
|
||||
|
||||
public MqttServerPlugin() {
|
||||
this.serverCreator = new MqttServerCreator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 mica mqtt
|
||||
*
|
||||
* @param consumer MqttServerCreator Consumer
|
||||
*/
|
||||
public void config(Consumer<MqttServerCreator> consumer) {
|
||||
consumer.accept(this.serverCreator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
if (this.mqttServer == null) {
|
||||
this.mqttServer = serverCreator.start();
|
||||
}
|
||||
MqttServerKit.init(this.mqttServer);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
if (this.mqttServer != null) {
|
||||
this.mqttServer.stop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
open module org.dromara.mica.mqtt.server.jfinal.plugin {
|
||||
requires jfinal;
|
||||
requires transitive org.dromara.mica.mqtt.server;
|
||||
exports org.dromara.mica.mqtt.jfinal.server;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.dromara.mica.mqtt.jfinal.server;
|
||||
|
||||
/**
|
||||
* mica mqtt server 插件测试
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class MqttServerPluginTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MqttServerPlugin plugin = new MqttServerPlugin();
|
||||
plugin.config(mqttServerCreator -> {
|
||||
// mqttServerCreator 上有很多方法,详见 mica-mqtt-core
|
||||
mqttServerCreator
|
||||
.enableMqtt()
|
||||
.enableMqttWs()
|
||||
.enableMqttHttpApi()
|
||||
;
|
||||
});
|
||||
plugin.start();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user