org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence Java Examples
The following examples show how to use
org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MQTTTestClient.java From micro-integrator with Apache License 2.0 | 6 votes |
/** * Generate a MQTT client with given parameters * * @param brokerURL url of MQTT provider * @param userName username to connect to MQTT provider * @param password password to connect to MQTT provider * @param clientId unique id for the publisher/subscriber client * @throws MqttException in case of issue of connect/publish/consume */ public MQTTTestClient(String brokerURL, String userName, char[] password, String clientId) throws MqttException { this.brokerURL = brokerURL; //Store messages until server fetches them MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(JAVA_TMP_DIR + "/" + clientId); mqttClient = new MqttClient(brokerURL, clientId, dataStore); SimpleMQTTCallback callback = new SimpleMQTTCallback(); mqttClient.setCallback(callback); MqttConnectOptions connectOptions = new MqttConnectOptions(); connectOptions.setUserName(userName); connectOptions.setPassword(password); connectOptions.setCleanSession(true); mqttClient.connect(connectOptions); log.info("MQTTTestClient successfully connected to broker at " + this.brokerURL); }
Example #2
Source File: MqttConfiguration.java From mqtt with Apache License 2.0 | 6 votes |
@Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); factory.setServerURIs(mqttProperties.getUrl()); factory.setUserName(mqttProperties.getUsername()); factory.setPassword(mqttProperties.getPassword()); factory.setCleanSession(mqttProperties.isCleanSession()); factory.setConnectionTimeout(mqttProperties.getConnectionTimeout()); factory.setKeepAliveInterval(mqttProperties.getKeepAliveInterval()); if (ObjectUtils.nullSafeEquals(mqttProperties.getPersistence(), "file")) { factory.setPersistence(new MqttDefaultFilePersistence(mqttProperties.getPersistenceDirectory())); } else if (ObjectUtils.nullSafeEquals(mqttProperties.getPersistence(), "memory")) { factory.setPersistence(new MemoryPersistence()); } return factory; }
Example #3
Source File: MQService.java From HelloMQTT with Apache License 2.0 | 6 votes |
/** * 服务初始化回调函数 */ @Override public void onCreate() { super.onCreate(); /**创建一个Handler*/ mConnHandler = new Handler(); try { /**新建一个本地临时存储数据的目录,该目录存储将要发送到服务器的数据,直到数据被发送到服务器*/ mDataStore = new MqttDefaultFilePersistence(getCacheDir().getAbsolutePath()); } catch(Exception e) { e.printStackTrace(); /**新建一个内存临时存储数据的目录*/ mDataStore = null; mMemStore = new MemoryPersistence(); } /**连接的参数选项*/ mOpts = new MqttConnectOptions(); /**删除以前的Session*/ mOpts.setCleanSession(MQTT_CLEAN_SESSION); // Do not set keep alive interval on mOpts we keep track of it with alarm's /**定时器用来实现心跳*/ mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); /**管理网络连接*/ mConnectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); }
Example #4
Source File: MQTTTestClient.java From product-ei with Apache License 2.0 | 6 votes |
/** * Generate a MQTT client with given parameters * * @param brokerURL url of MQTT provider * @param userName username to connect to MQTT provider * @param password password to connect to MQTT provider * @param clientId unique id for the publisher/subscriber client * @throws MqttException in case of issue of connect/publish/consume */ public MQTTTestClient(String brokerURL, String userName, char[] password, String clientId) throws MqttException { this.brokerURL = brokerURL; //Store messages until server fetches them MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(JAVA_TMP_DIR + "/" + clientId); mqttClient = new MqttClient(brokerURL, clientId, dataStore); SimpleMQTTCallback callback = new SimpleMQTTCallback(); mqttClient.setCallback(callback); MqttConnectOptions connectOptions = new MqttConnectOptions(); connectOptions.setUserName(userName); connectOptions.setPassword(password); connectOptions.setCleanSession(true); mqttClient.connect(connectOptions); log.info("MQTTTestClient successfully connected to broker at " + this.brokerURL); }
Example #5
Source File: MoquetteProxyContext.java From cloud-pubsub-mqtt-proxy with Apache License 2.0 | 5 votes |
/** * Initializes an object that can be used for sending messages to the broker * which is running on localhost. */ public MoquetteProxyContext() { try { this.dataStore = new MqttDefaultFilePersistence(); this.client = new MqttAsyncClient(getFullMqttBrokerUrl(), MQTT_CLIENT_NAME, dataStore); } catch (MqttException e) { // The exception is thrown when there is an unrecognized MQTT Message in the persistant // storage location. Messages are removed from persistant storage once the broker // sends the message to subscribers (does not wait for confirmation) throw new IllegalStateException("Unrecognized message in the persistent data store location." + " Consider clearing the default persistent storage location."); } }
Example #6
Source File: MoquetteProxyContext.java From cloud-pubsub-mqtt-proxy with Apache License 2.0 | 5 votes |
/** * Initializes an object that can be used for sending messages to the broker * which is running on localhost. * * @param persistenceDir the location of the persistent storage used by the MQTT client library. */ public MoquetteProxyContext(String persistenceDir) { try { dataStore = new MqttDefaultFilePersistence(checkNotNull(persistenceDir)); client = new MqttAsyncClient(getFullMqttBrokerUrl(), MQTT_CLIENT_NAME, dataStore); } catch (MqttException e) { // The exception is thrown when there is an unrecognized MQTT Message in the persistant // storage location. Messages are removed from persistant storage once the broker // sends the message to subscribers (does not wait for confirmation) throw new IllegalStateException("Unrecognized message in the persistent data store location." + " Consider clearing the default persistent storage location."); } }
Example #7
Source File: BlockingClient.java From mqtt-jmeter with Apache License 2.0 | 5 votes |
/** * Constructs an instance of the sample client wrapper * * @param brokerUrl the url of the server to connect to * @param clientId the client id to connect with * @param cleanSession clear state at end of connection or not (durable or non-durable subscriptions) * @param userName the username to connect with * @param password the password for the user * @throws MqttException */ public BlockingClient(String brokerUrl, String clientId, boolean cleanSession, String userName, String password, int keepAlive) throws MqttException { this.brokerUrl = brokerUrl; String testPlanFileDir = System.getProperty("java.io.tmpdir") + File.separator + "mqtt" + File.separator + clientId + File.separator + Thread.currentThread().getId(); MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(testPlanFileDir); // Construct the connection options object that contains connection parameters // such as cleanSession and LWT MqttConnectOptions conOpt = new MqttConnectOptions(); conOpt.setCleanSession(cleanSession); if (password != null && !password.isEmpty()) { conOpt.setPassword(password.toCharArray()); } if (userName != null && !userName.isEmpty()) { conOpt.setUserName(userName); } // Setting keep alive time conOpt.setKeepAliveInterval(keepAlive); // Construct an MQTT blocking mode client client = new MqttClient(this.brokerUrl, clientId, dataStore); // Set this wrapper as the callback handler client.setCallback(this); // Connect to the MQTT server log.info("Connecting to " + brokerUrl + " with client ID '" + client.getClientId() + "' and cleanSession is " + String.valueOf(cleanSession) + " as a blocking client"); client.connect(conOpt); log.info("Connected"); }
Example #8
Source File: MqttFacade.java From iaf with Apache License 2.0 | 5 votes |
public void configure() throws ConfigurationException { if (StringUtils.isEmpty(getClientId())) { throw new ConfigurationException("clientId must be specified"); } if (StringUtils.isEmpty(getBrokerUrl())) { throw new ConfigurationException("brokerUrl must be specified"); } if (StringUtils.isEmpty(getTopic())) { throw new ConfigurationException("topic must be specified"); } if (StringUtils.isEmpty(getPersistenceDirectory())) { throw new ConfigurationException("persistenceDirectory must be specified"); } connectOptions = new MqttConnectOptions(); connectOptions.setCleanSession(isCleanSession()); connectOptions.setAutomaticReconnect(isAutomaticReconnect()); connectOptions.setConnectionTimeout(getTimeout()); connectOptions.setKeepAliveInterval(getKeepAliveInterval()); connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_DEFAULT); //Default: 0, V3.1: 3, V3.1.1: 4 if(!StringUtils.isEmpty(getAuthAlias()) || (!StringUtils.isEmpty(getUsername()) && !StringUtils.isEmpty(getPassword()))) { CredentialFactory credentialFactory = new CredentialFactory(getAuthAlias(), getUsername(), getPassword()); connectOptions.setUserName(credentialFactory.getUsername()); connectOptions.setPassword(credentialFactory.getPassword().toCharArray()); } MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(getPersistenceDirectory()); try { client = new MqttClient(brokerUrl, clientId, dataStore); } catch (MqttException e) { throw new ConfigurationException("Could not create client", e); } }
Example #9
Source File: MQTTConnectorFactory.java From mqtt-client-connector with Eclipse Public License 1.0 | 5 votes |
/** * This is called by the integration node when an integration server is started up. * Any general messaging provider initialization should be done at this point. * It is not recommended to do any connections at this point to external systems, just validate that the properties being used are valid. * This method is the pair to the "terminate" method. * @param name name of the connector factory as defined in the ConnectorProviders configurable service * @param properties the properties defined on the ConnectorProviders configurable service * @throws MbException Throwing an exception will stop the ConnectionFactory being available for use. */ @Override public void initialize(String providerName, Properties providerProps) throws MbException { writeServiceTraceEntry(clsName, "initialize", "Entry"); try { writeServiceTraceData(clsName, "initialize", "Connector properties: " + providerProps.toString()); if (providerProps.getProperty("property1") != null) { useBuildMode = providerProps.getProperty("property1") .equalsIgnoreCase("useBuildMode"); changeAdminToActivityLog = providerProps.getProperty( "property1").equalsIgnoreCase( "changeAdminToActivityLog"); } if ("file".equalsIgnoreCase(providerProps.getProperty("property2"))) { String cpDir = getContainerServices().getWorkDirectory() + getContainerServices().getFileSeparator() + getContainerServices().containerName(); cp = new MqttDefaultFilePersistence(cpDir); } else { cp = new MemoryPersistence(); } } finally { writeServiceTraceExit(clsName, "initialize", "Exit"); } }
Example #10
Source File: MqttClientManager.java From micro-integrator with Apache License 2.0 | 4 votes |
public void registerClientDataStore(String identifier, MqttDefaultFilePersistence dataStore) { mqttClientDataStoreMap.put(identifier, dataStore); }
Example #11
Source File: MqttClientManager.java From micro-integrator with Apache License 2.0 | 4 votes |
public MqttDefaultFilePersistence getMqttClientDataStore(String identifier) { return mqttClientDataStoreMap.get(identifier); }
Example #12
Source File: VenvyMqttClient.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 4 votes |
public VenvyMqttClient(String serverURI, String clientId) throws MqttException { super(serverURI, clientId, new MqttDefaultFilePersistence()); }
Example #13
Source File: AsyncClient.java From mqtt-jmeter with Apache License 2.0 | 4 votes |
/** * Constructs an instance of the sample client wrapper * * @param brokerUrl the url to connect to * @param clientId the client id to connect with * @param cleanSession clear state at end of connection or not (durable or non-durable subscriptions) * @param userName the username to connect with * @param password the password for the user * @throws MqttException */ public AsyncClient(String brokerUrl, String clientId, boolean cleanSession, String userName, String password, int keepAlive) throws MqttException { this.brokerUrl = brokerUrl; String testPlanFileDir = System.getProperty("java.io.tmpdir") + File.separator + "mqtt" + File.separator + clientId + File.separator + Thread.currentThread().getId(); MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(testPlanFileDir); try { // Construct the connection options object that contains connection parameters // such as cleanSession and LWT MqttConnectOptions conOpt = new MqttConnectOptions(); conOpt.setCleanSession(cleanSession); if (password != null && !password.isEmpty()) { conOpt.setPassword(password.toCharArray()); } if (userName != null && !userName.isEmpty()) { conOpt.setUserName(userName); } // Setting keep alive time conOpt.setKeepAliveInterval(keepAlive); // Construct a non-blocking MQTT client instance client = new MqttAsyncClient(this.brokerUrl, clientId, dataStore); // Set this wrapper as the callback handler client.setCallback(this); // Connect to the MQTT server // issue a non-blocking connect and then use the token to wait until the // connect completes. An exception is thrown if connect fails. log.info("Connecting to " + brokerUrl + " with client ID '" + client.getClientId() + "' and cleanSession " + " is " + String.valueOf(cleanSession) + " as an async clientt"); IMqttToken conToken = client.connect(conOpt, null, null); conToken.waitForCompletion(); log.info("Connected"); } catch (MqttException e) { log.info("Unable to set up client: " + e.toString()); } }