|  |  | 
 |  |  | 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 | 
 |  |  | 
 |  |  |     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()); |