45 lines
1.3 KiB
Java
45 lines
1.3 KiB
Java
package com.mobai.cofig;
|
|
|
|
import com.mobai.domian.GetOptions;
|
|
import com.mobai.domian.MqttCallBackServiceImpl;
|
|
import lombok.AllArgsConstructor;
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
/**
|
|
* @ClassName MqttFactory
|
|
* @Description 描述
|
|
* @Author SaiSai.Liu
|
|
* @Date 2024/5/31 11:35
|
|
*/
|
|
|
|
@Service
|
|
@AllArgsConstructor
|
|
public class MqttFactory {
|
|
|
|
private final MqttCallBackServiceImpl mqttCallBackService;
|
|
|
|
public MqttClient buildOptions( MqttProperties mqttProperties) {
|
|
MqttClient client = null;
|
|
try {
|
|
client = new MqttClient(
|
|
mqttProperties.getBroker(),
|
|
mqttProperties.getClientid(),
|
|
new MemoryPersistence());
|
|
MqttConnectOptions optionas = GetOptions.getMqttOptions(mqttProperties);
|
|
client.connect(optionas);
|
|
client.setCallback(mqttCallBackService);
|
|
|
|
client.subscribe(mqttProperties.getTopic(), 0);
|
|
|
|
} catch (MqttException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
return client;
|
|
}
|
|
}
|