zhubaomin
2 天以前 70f61b0638e32274d0e5f9b972ce2a18f139f1b4
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/channel/mqtt/MqttClientPooledObjectFactory.java
@@ -5,6 +5,7 @@
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
/**
 * @Author: liurunyu
@@ -16,18 +17,26 @@
    private final String broker;
    private final String username;
    private final String password;
    private final Boolean useMemoryPersistence;
    public MqttClientPooledObjectFactory(String broker, String username, String password) {
    public MqttClientPooledObjectFactory(String broker, String username, String password, boolean useMemoryPersistence) {
        this.broker = broker;
        this.username = username;
        this.password = password;
        this.useMemoryPersistence = useMemoryPersistence;
    }
    @Override
    public MqttClient create() throws Exception {
        String clientId = MqttClient.generateClientId();
        MqttClient client = new MqttClient(broker, clientId);
        MqttClient client = null ;
        // 使用内存持久化而非默认的文件持久化
        if (useMemoryPersistence) {
            MemoryPersistence persistence = new MemoryPersistence();
            client = new MqttClient(broker, clientId, persistence);
        }else{
            client = new MqttClient(broker, clientId);
        }
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName(username);
        options.setPassword(password.toCharArray());