first commit
This commit is contained in:
81
starter/mica-mqtt-client-jfinal-plugin/README.md
Normal file
81
starter/mica-mqtt-client-jfinal-plugin/README.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# jfinal mica-mqtt client
|
||||
|
||||
## 使用
|
||||
|
||||
#### 1. 添加依赖
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.dromara.mica-mqtt</groupId>
|
||||
<artifactId>mica-mqtt-client-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. 在 jfinal Config configPlugin 中添加 mica-mqtt client 插件
|
||||
```java
|
||||
MqttClientPlugin mqttClientPlugin = new MqttClientPlugin();
|
||||
mqttClientPlugin.config(mqttClientCreator -> {
|
||||
// 设置 mqtt 连接配置信息
|
||||
mqttClientCreator
|
||||
.clientId("clientId") // 按需配置,相同的会互踢
|
||||
.ip("mqtt.dreamlu.net")
|
||||
.port(1883)
|
||||
.connectListener(Aop.get(MqttClientConnectListener.class));
|
||||
});
|
||||
me.add(mqttClientPlugin);
|
||||
```
|
||||
|
||||
#### 5. 在 jfinal Config onStart 启动完成之后添加 mqtt 订阅
|
||||
```java
|
||||
@Override
|
||||
public void onStart() {
|
||||
IMqttClientMessageListener clientMessageListener = Aop.get(TestMqttClientMessageListener.class);
|
||||
MqttClientKit.subQos0("#", clientMessageListener);
|
||||
}
|
||||
```
|
||||
|
||||
#### 6. 使用 MqttClientKit 发送消息
|
||||
```java
|
||||
MqttClientKit.publish("mica", "hello".getBytes(StandardCharsets.UTF_8));
|
||||
```
|
||||
|
||||
### 7. 示例代码 MqttClientConnectListener
|
||||
```java
|
||||
public class MqttClientConnectListener implements IMqttClientConnectListener {
|
||||
|
||||
@Override
|
||||
public void onConnected(ChannelContext channelContext, boolean isReconnect) {
|
||||
if (isReconnect) {
|
||||
System.out.println("重连 mqtt 服务器重连成功...");
|
||||
} else {
|
||||
System.out.println("连接 mqtt 服务器成功...");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnect(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
|
||||
System.out.println("mqtt 链接断开 remark:" + remark + " isRemove:" + isRemove);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 8. 示例 TestMqttClientMessageListener
|
||||
```java
|
||||
public class TestMqttClientMessageListener implements IMqttClientMessageListener {
|
||||
@Override
|
||||
public void onMessage(String topic, MqttPublishMessage message, byte[] payload) {
|
||||
System.out.println("收到消息 topic:" + topic + "内容:\n" + new String(payload, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
```
|
||||
32
starter/mica-mqtt-client-jfinal-plugin/pom.xml
Normal file
32
starter/mica-mqtt-client-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-client-jfinal-plugin</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<url>https://mica-mqtt.dreamlu.net/guide/jfinal/client.html</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara.mica-mqtt</groupId>
|
||||
<artifactId>mica-mqtt-client</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,354 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import org.dromara.mica.mqtt.codec.MqttQoS;
|
||||
import org.dromara.mica.mqtt.codec.message.builder.MqttPublishBuilder;
|
||||
import org.dromara.mica.mqtt.codec.properties.MqttProperties;
|
||||
import org.dromara.mica.mqtt.core.client.IMqttClientMessageListener;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClient;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientSubscription;
|
||||
import org.tio.client.ClientChannelContext;
|
||||
import org.tio.client.TioClient;
|
||||
import org.tio.client.TioClientConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* mica mqtt client kit
|
||||
*
|
||||
* @author L.cm
|
||||
* @author ChangJin Wei (魏昌进)
|
||||
*/
|
||||
public class MqttClientKit {
|
||||
private static MqttClient client;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param client MqttClient
|
||||
*/
|
||||
static void init(MqttClient client) {
|
||||
MqttClientKit.client = client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilter topicFilter
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subQos0(String topicFilter, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, MqttQoS.QOS0, listener, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilter topicFilter
|
||||
* @param properties MqttProperties
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subQos0(String topicFilter, MqttProperties properties, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, MqttQoS.QOS0, listener, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilter topicFilter
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subQos1(String topicFilter, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, MqttQoS.QOS1, listener, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilter topicFilter
|
||||
* @param properties MqttProperties
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subQos1(String topicFilter, MqttProperties properties, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, MqttQoS.QOS1, listener, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilter topicFilter
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subQos2(String topicFilter, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, MqttQoS.QOS2, listener, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilter topicFilter
|
||||
* @param properties MqttProperties
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subQos2(String topicFilter, MqttProperties properties, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, MqttQoS.QOS2, listener, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param mqttQoS MqttQoS
|
||||
* @param topicFilter topicFilter
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(MqttQoS mqttQoS, String topicFilter, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, mqttQoS, listener, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param mqttQoS MqttQoS
|
||||
* @param topicFilter topicFilter
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(String topicFilter, MqttQoS mqttQoS, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilter, mqttQoS, listener, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param mqttQoS MqttQoS
|
||||
* @param topicFilter topicFilter
|
||||
* @param listener MqttMessageListener
|
||||
* @param properties MqttProperties
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(String topicFilter, MqttQoS mqttQoS, IMqttClientMessageListener listener, MqttProperties properties) {
|
||||
return client.subscribe(topicFilter, mqttQoS, listener, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilters topicFilter 数组
|
||||
* @param mqttQoS MqttQoS
|
||||
* @param listener MqttMessageListener
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(String[] topicFilters, MqttQoS mqttQoS, IMqttClientMessageListener listener) {
|
||||
return client.subscribe(topicFilters, mqttQoS, listener, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅
|
||||
*
|
||||
* @param topicFilters topicFilter 数组
|
||||
* @param mqttQoS MqttQoS
|
||||
* @param listener MqttMessageListener
|
||||
* @param properties MqttProperties
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(String[] topicFilters, MqttQoS mqttQoS, IMqttClientMessageListener listener, MqttProperties properties) {
|
||||
return client.subscribe(topicFilters, mqttQoS, listener, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量订阅
|
||||
*
|
||||
* @param subscriptionList 订阅集合
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(List<MqttClientSubscription> subscriptionList) {
|
||||
return client.subscribe(subscriptionList, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量订阅
|
||||
*
|
||||
* @param subscriptionList 订阅集合
|
||||
* @param properties MqttProperties
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient subscribe(List<MqttClientSubscription> subscriptionList, MqttProperties properties) {
|
||||
return client.subscribe(subscriptionList, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订阅
|
||||
*
|
||||
* @param topicFilters topicFilter 集合
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient unSubscribe(String... topicFilters) {
|
||||
return client.unSubscribe(topicFilters);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订阅
|
||||
*
|
||||
* @param topicFilters topicFilter 集合
|
||||
* @return MqttClient
|
||||
*/
|
||||
public static MqttClient unSubscribe(List<String> topicFilters) {
|
||||
return client.unSubscribe(topicFilters);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息内容
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String topic, Object payload) {
|
||||
return client.publish(topic, payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息内容
|
||||
* @param qos MqttQoS
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String topic, Object payload, MqttQoS qos) {
|
||||
return client.publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息内容
|
||||
* @param retain 是否在服务器上保留消息
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String topic, Object payload, boolean retain) {
|
||||
return client.publish(topic, payload, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param qos MqttQoS
|
||||
* @param retain 是否在服务器上保留消息
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String topic, Object payload, MqttQoS qos, boolean retain) {
|
||||
return client.publish(topic, payload, qos, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
*
|
||||
* @param topic topic
|
||||
* @param payload 消息体
|
||||
* @param qos MqttQoS
|
||||
* @param builder PublishBuilder
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean publish(String topic, Object payload, MqttQoS qos, Consumer<MqttPublishBuilder> builder) {
|
||||
return client.publish(topic, payload, qos, builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重连
|
||||
*/
|
||||
public static void reconnect() {
|
||||
client.reconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开 mqtt 连接
|
||||
*
|
||||
* @return 是否成功
|
||||
*/
|
||||
public static boolean disconnect() {
|
||||
return client.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 TioClient
|
||||
*
|
||||
* @return TioClient
|
||||
*/
|
||||
public static TioClient getTioClient() {
|
||||
return client.getTioClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*
|
||||
* @return MqttClientCreator
|
||||
*/
|
||||
public static MqttClientCreator getClientCreator() {
|
||||
return client.getClientCreator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 TioClientConfig
|
||||
*
|
||||
* @return TioClientConfig
|
||||
*/
|
||||
public static TioClientConfig getClientTioConfig() {
|
||||
return client.getClientTioConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 ClientChannelContext
|
||||
*
|
||||
* @return ClientChannelContext
|
||||
*/
|
||||
public static ClientChannelContext getContext() {
|
||||
return client.getContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断客户端跟服务端是否连接
|
||||
*
|
||||
* @return 是否已经连接成功
|
||||
*/
|
||||
public static boolean isConnected() {
|
||||
return client.isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断客户端跟服务端是否断开连接
|
||||
*
|
||||
* @return 是否断连
|
||||
*/
|
||||
public static boolean isDisconnected() {
|
||||
return client.isDisconnected();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import com.jfinal.plugin.IPlugin;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClient;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* mica mqtt client 插件
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class MqttClientPlugin implements IPlugin {
|
||||
private final MqttClientCreator clientCreator;
|
||||
private MqttClient mqttClient;
|
||||
|
||||
public MqttClientPlugin() {
|
||||
this.clientCreator = new MqttClientCreator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 mica mqtt
|
||||
*
|
||||
* @param consumer MqttClientCreator Consumer
|
||||
*/
|
||||
public void config(Consumer<MqttClientCreator> consumer) {
|
||||
consumer.accept(this.clientCreator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
if (this.mqttClient == null) {
|
||||
// 连接超时时间,如果没设置,改成 3s,减少因连不上卡顿时间
|
||||
Integer timeout = clientCreator.getTimeout();
|
||||
if (timeout == null) {
|
||||
clientCreator.timeout(3);
|
||||
}
|
||||
// 使用同步连接
|
||||
this.mqttClient = clientCreator.connectSync();
|
||||
} else {
|
||||
this.mqttClient.reconnect();
|
||||
}
|
||||
MqttClientKit.init(this.mqttClient);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
if (this.mqttClient != null) {
|
||||
this.mqttClient.stop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
open module org.dromara.mica.mqtt.client.jfinal.plugin {
|
||||
requires jfinal;
|
||||
requires transitive org.dromara.mica.mqtt.client;
|
||||
exports org.dromara.mica.mqtt.jfinal.client;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.dromara.mica.mqtt.jfinal.client;
|
||||
|
||||
/**
|
||||
* mica mqtt client 插件测试
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class MqttClientPluginTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MqttClientPlugin plugin = new MqttClientPlugin();
|
||||
plugin.config(mqttClientCreator -> {
|
||||
// mqttClientCreator 上有很多方法,详见 mica-mqtt-core
|
||||
mqttClientCreator.port(1883).username("mica").password("mica");
|
||||
});
|
||||
plugin.start();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user