parent
c8e49a65d9
commit
bfeecd37f2
|
@ -2,9 +2,11 @@ package cc.iotkit.ruleengine.link.impl;
|
||||||
|
|
||||||
import cc.iotkit.common.utils.FIUtil;
|
import cc.iotkit.common.utils.FIUtil;
|
||||||
import cc.iotkit.ruleengine.link.BaseSinkLink;
|
import cc.iotkit.ruleengine.link.BaseSinkLink;
|
||||||
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
import io.vertx.kafka.client.producer.KafkaProducer;
|
import io.vertx.kafka.client.producer.KafkaProducer;
|
||||||
import io.vertx.kafka.client.producer.KafkaProducerRecord;
|
import io.vertx.kafka.client.producer.KafkaProducerRecord;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
import org.apache.kafka.common.serialization.StringSerializer;
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
|
|
||||||
|
@ -21,6 +23,7 @@ import java.util.function.Consumer;
|
||||||
* @author huangwenl
|
* @author huangwenl
|
||||||
* @date 2022-11-11
|
* @date 2022-11-11
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class KafkaLink implements BaseSinkLink {
|
public class KafkaLink implements BaseSinkLink {
|
||||||
public static final String LINK_TYPE = "kafka";
|
public static final String LINK_TYPE = "kafka";
|
||||||
public static final String TOPIC = "topic";
|
public static final String TOPIC = "topic";
|
||||||
|
@ -60,16 +63,25 @@ public class KafkaLink implements BaseSinkLink {
|
||||||
FIUtil.isTotF(msg.containsKey(PARTITION)).handler(
|
FIUtil.isTotF(msg.containsKey(PARTITION)).handler(
|
||||||
() -> record.set(KafkaProducerRecord.create((String) msg.get(TOPIC), "", msg.get(PAYLOAD).toString(), (Integer) msg.get(PARTITION))),
|
() -> record.set(KafkaProducerRecord.create((String) msg.get(TOPIC), "", msg.get(PAYLOAD).toString(), (Integer) msg.get(PARTITION))),
|
||||||
() -> record.set(KafkaProducerRecord.create((String) msg.get(TOPIC), msg.get(PAYLOAD).toString())));
|
() -> record.set(KafkaProducerRecord.create((String) msg.get(TOPIC), msg.get(PAYLOAD).toString())));
|
||||||
// todo 异步发送(不能确认是否成功)
|
// 同步等待结果
|
||||||
producer.write(record.get());
|
try {
|
||||||
consumer.accept(String.format("kafka,topic[%s],发送成功:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString()));
|
Future<Void> write = producer.write(record.get());
|
||||||
|
write.toCompletionStage().toCompletableFuture().get();
|
||||||
|
FIUtil.isTotF(write.succeeded()).handler(
|
||||||
|
() -> consumer.accept(String.format("kafka,topic[%s],发送成功:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString())),
|
||||||
|
() -> consumer.accept(String.format("kafka,topic[%s],发送失败:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString()))
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
consumer.accept(String.format("kafka,topic[%s],发送异常:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
producer.close();
|
producer.close();
|
||||||
producer = null;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -3,10 +3,12 @@ package cc.iotkit.ruleengine.link.impl;
|
||||||
import cc.iotkit.common.utils.FIUtil;
|
import cc.iotkit.common.utils.FIUtil;
|
||||||
import cc.iotkit.ruleengine.link.BaseSinkLink;
|
import cc.iotkit.ruleengine.link.BaseSinkLink;
|
||||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||||
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
import io.vertx.core.buffer.Buffer;
|
import io.vertx.core.buffer.Buffer;
|
||||||
import io.vertx.mqtt.MqttClient;
|
import io.vertx.mqtt.MqttClient;
|
||||||
import io.vertx.mqtt.MqttClientOptions;
|
import io.vertx.mqtt.MqttClientOptions;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
@ -16,6 +18,7 @@ import java.util.function.Consumer;
|
||||||
* @author huangwenl
|
* @author huangwenl
|
||||||
* @date 2022-11-10
|
* @date 2022-11-10
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class MqttClientLink implements BaseSinkLink {
|
public class MqttClientLink implements BaseSinkLink {
|
||||||
public static final String LINK_TYPE = "mqtt";
|
public static final String LINK_TYPE = "mqtt";
|
||||||
public static final String TOPIC = "topic";
|
public static final String TOPIC = "topic";
|
||||||
|
@ -27,6 +30,10 @@ public class MqttClientLink implements BaseSinkLink {
|
||||||
|
|
||||||
private MqttClient mqttClient;
|
private MqttClient mqttClient;
|
||||||
private Consumer<Void> closeHandler;
|
private Consumer<Void> closeHandler;
|
||||||
|
private MqttClientOptions clientOptions;
|
||||||
|
private String host;
|
||||||
|
private int port;
|
||||||
|
private boolean connecting;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -37,14 +44,24 @@ public class MqttClientLink implements BaseSinkLink {
|
||||||
() -> vertx.set(Vertx.vertx()),
|
() -> vertx.set(Vertx.vertx()),
|
||||||
() -> vertx.set(Vertx.currentContext().owner())
|
() -> vertx.set(Vertx.currentContext().owner())
|
||||||
);
|
);
|
||||||
MqttClientOptions clientOptions = new MqttClientOptions();
|
clientOptions = new MqttClientOptions();
|
||||||
clientOptions.setUsername((String) config.get(USERNAME));
|
clientOptions.setUsername((String) config.get(USERNAME));
|
||||||
clientOptions.setPassword((String) config.get(PASSWORD));
|
clientOptions.setPassword((String) config.get(PASSWORD));
|
||||||
mqttClient = MqttClient.create(vertx.get(), clientOptions);
|
mqttClient = MqttClient.create(vertx.get(), clientOptions);
|
||||||
mqttClient.connect((int) config.get(PORT), (String) config.get(HOST));
|
host = (String) config.get(HOST);
|
||||||
mqttClient.closeHandler(Void -> closeHandler.accept(null));
|
port = (int) config.get(PORT);
|
||||||
|
mqttClient = MqttClient.create(vertx.get(), clientOptions);
|
||||||
|
connecting = true;
|
||||||
|
mqttClient.connect(port, host,
|
||||||
|
s -> {
|
||||||
|
connecting = false;
|
||||||
|
if (!s.succeeded()) {
|
||||||
|
closeHandler.accept(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
connecting = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -54,18 +71,39 @@ public class MqttClientLink implements BaseSinkLink {
|
||||||
public void send(Map<String, Object> msg, Consumer<String> consumer) {
|
public void send(Map<String, Object> msg, Consumer<String> consumer) {
|
||||||
FIUtil.isTotF(mqttClient.isConnected()).handler(
|
FIUtil.isTotF(mqttClient.isConnected()).handler(
|
||||||
() -> {
|
() -> {
|
||||||
mqttClient.publish((String) msg.get(TOPIC),
|
Future<Integer> publish = mqttClient.publish((String) msg.get(TOPIC),
|
||||||
Buffer.buffer(msg.get(PAYLOAD).toString()),
|
Buffer.buffer(msg.get(PAYLOAD).toString()),
|
||||||
MqttQoS.AT_MOST_ONCE, false, false);
|
MqttQoS.AT_MOST_ONCE, false, false);
|
||||||
consumer.accept(String.format("mqtt, topic:[%s],发送成功:,%s", msg.get(TOPIC), msg.get(PAYLOAD).toString()));
|
try {
|
||||||
|
publish.toCompletionStage().toCompletableFuture().get();
|
||||||
|
FIUtil.isTotF(publish.succeeded()).handler(
|
||||||
|
() -> consumer.accept(String.format("mqtt,topic[%s],发送成功:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString())),
|
||||||
|
() -> consumer.accept(String.format("mqtt,topic[%s],发送失败:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString()))
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
consumer.accept(String.format("mqtt,topic[%s],发送异常:%s", msg.get(TOPIC), msg.get(PAYLOAD).toString()));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
() -> consumer.accept("mqtt,连接断开,发送失败"));
|
() -> {
|
||||||
|
consumer.accept("mqtt,连接断开,发送失败");
|
||||||
|
if (!connecting) {
|
||||||
|
log.info("mqtt重连!");
|
||||||
|
connecting = true;
|
||||||
|
mqttClient.connect(port, host, s -> connecting = false);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
mqttClient.disconnect();
|
try {
|
||||||
mqttClient = null;
|
mqttClient.disconnect();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
closeHandler.accept(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue