org.eclipse.paho.client.mqttv3.MqttClientPersistence Java Examples

The following examples show how to use org.eclipse.paho.client.mqttv3.MqttClientPersistence. 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: MQTTManager.java    From EMQ-Android-Toolkit with Apache License 2.0 6 votes vote down vote up
public MqttAndroidClient createClient(String id, String serverURI, String clientId) {
    MqttClientPersistence mqttClientPersistence = new MemoryPersistence();
    MqttAndroidClient client = new MqttAndroidClient(MyApplication.getContext(), serverURI, clientId, mqttClientPersistence);
    client.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable cause) {
            LogUtil.e("connectionLost");
            EventBus.getDefault().post(new MQTTActionEvent(Constant.MQTTStatusConstant.CONNECTION_LOST, null, cause));

        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            LogUtil.d("topic is " + topic + ",message is " + message.toString() + ", qos is " + message.getQos());
            EventBus.getDefault().postSticky(new MessageEvent(new EmqMessage(topic, message)));

        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {
            LogUtil.d("deliveryComplete");


        }
    });

    mClients.put(id, client);

    return client;

}
 
Example #2
Source File: MqttConnection.java    From Sparkplug with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Constructor - create an MqttConnection to communicate with MQTT server
 * 
 * @param service
 *            our "parent" service - we make callbacks to it
 * @param serverURI
 *            the URI of the MQTT server to which we will connect
 * @param clientId
 *            the name by which we will identify ourselves to the MQTT
 *            server
 * @param persistence
 *            the persistence class to use to store in-flight message. If
 *            null then the default persistence mechanism is used
 * @param clientHandle
 *            the "handle" by which the activity will identify us
 */
MqttConnection(MqttService service, String serverURI, String clientId,
		MqttClientPersistence persistence, String clientHandle) {
	this.serverURI = serverURI;
	this.service = service;
	this.clientId = clientId;
	this.persistence = persistence;
	this.clientHandle = clientHandle;

	StringBuilder stringBuilder = new StringBuilder(this.getClass().getCanonicalName());
	stringBuilder.append(" ");
	stringBuilder.append(clientId);
	stringBuilder.append(" ");
	stringBuilder.append("on host ");
	stringBuilder.append(serverURI);
	wakeLockTag = stringBuilder.toString();
}
 
Example #3
Source File: MessagingModule.java    From winthing with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    bind(MqttClientPersistence.class).to(MemoryPersistence.class).in(Singleton.class);

    bind(Registry.class).to(SimpleRegistry.class).in(Singleton.class);

    bind(Engine.class).in(Singleton.class);
    bind(MessagePublisher.class).to(Engine.class);

    expose(Registry.class);
    expose(MessagePublisher.class);
    expose(Engine.class);
}
 
Example #4
Source File: Engine.java    From winthing with Apache License 2.0 5 votes vote down vote up
@Inject
public Engine(final Gson gson, final Registry registry, final Config config,
        final MqttClientPersistence persistence) throws MqttException {
    String topicPrefix = config.getString(Settings.TOPIC_PREFIX).replaceFirst("^/+", "");
    if (!topicPrefix.isEmpty() && !topicPrefix.endsWith("/")) {
        topicPrefix += "/";
    }
    this.topicPrefix = topicPrefix;

    this.reconnectInterval = Duration.ofSeconds(config.getLong(Settings.RECONNECT_INTERVAL));

    this.gson = Objects.requireNonNull(gson);
    this.registry = Objects.requireNonNull(registry);

    this.client = new MqttAsyncClient(
        "tcp://" + config.getString(Settings.BROKER_URL),
        config.getString(Settings.CLIENT_ID),
        persistence
    );
    this.client.setCallback(this);

    {
        final String username = config.getString(Settings.BROKER_USERNAME);
        if (username != null && !username.isEmpty()) {
            this.options.setUserName(username);
        }
    }
    {
        final String password = config.getString(Settings.BROKER_PASSWORD);
        if (password != null && !password.isEmpty()) {
            this.options.setPassword(password.toCharArray());
        }
    }

    this.options.setCleanSession(true);
}
 
Example #5
Source File: MqttConfig.java    From quarks with Apache License 2.0 5 votes vote down vote up
private static MqttClientPersistence newPersistenceProvider(String className) {
    Class<?> clazz = null;
    try {
        clazz = Class.forName(className);
        return (MqttClientPersistence) clazz.newInstance();
    }
    catch (Exception e) {
        throw new IllegalArgumentException(e.getLocalizedMessage(), e);
    }
}
 
Example #6
Source File: Client.java    From java-app-sdk with MIT License 5 votes vote down vote up
/**
 * Change the default Mqtt persistence settings
 *
 * @param _persistence A custom persistence setting
 * @return the Client instance
 */
public Client setMqttPersistence(MqttClientPersistence _persistence) {
    if (mqttClient != null) {
        throw new RuntimeException("Can not be called while client is running");
    }
    persistence = _persistence;
    return this;
}
 
Example #7
Source File: MQClient.java    From HelloMQTT with Apache License 2.0 4 votes vote down vote up
public MQClient(String serverURI, String clientId, MqttClientPersistence persistence) throws MqttException {
    super(serverURI, clientId, persistence);
}
 
Example #8
Source File: MQTTConnectorFactory.java    From mqtt-client-connector with Eclipse Public License 1.0 4 votes vote down vote up
public MqttClientPersistence getClientPersistence() {
	return cp;
}
 
Example #9
Source File: MQTTClientBuilder.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
MqttClient getClient( String broker, String clientId, MqttClientPersistence persistence )
throws MqttException;
 
Example #10
Source File: PahoObservableMqttClient.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
public static Builder builder(final String brokerUri, final String clientId,
        final MqttClientPersistence persistence) throws MqttException {
    return builder(new MqttAsyncClient(brokerUri, clientId, persistence));
}
 
Example #11
Source File: MqttAsyncClientFactory.java    From jframe with Apache License 2.0 4 votes vote down vote up
private MqttClientPersistence createPersistence() throws Exception {
    return (MqttClientPersistence) Class.forName(conf.getConf(id, MqttClientConf.F_mqtt_persistence)).newInstance();
}
 
Example #12
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
public MqttAsyncClientEx(String serverURI, String clientId, MqttClientPersistence dataStore,
        MqttBrokerConnectionEx connection) throws MqttException {
    super(serverURI, clientId, dataStore);
    this.connection = connection;
}
 
Example #13
Source File: MqttBrokerConnectionEx.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected MqttAsyncClient createClient(String serverURI, String clientId, MqttClientPersistence dataStore)
        throws org.eclipse.paho.client.mqttv3.MqttException {
    return spy(new MqttAsyncClientEx(serverURI, clientId, dataStore, this));
}
 
Example #14
Source File: VenvyMqttClient.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public VenvyMqttClient(String serverURI, String clientId, MqttClientPersistence persistence) throws Exception {
    super(serverURI, clientId, persistence);
}
 
Example #15
Source File: PahoObservableMqttClient.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
public Builder(final String brokerUri, final String clientId,
        final MqttClientPersistence persistence) throws MqttException {
    this(new MqttAsyncClient(brokerUri, clientId, persistence));
}
 
Example #16
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
public MqttAsyncClientEx(String serverURI, String clientId, MqttClientPersistence dataStore,
        MqttBrokerConnectionEx connection) throws MqttException {
    super(serverURI, clientId, dataStore);
    this.connection = connection;
}
 
Example #17
Source File: MqttBrokerConnectionEx.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected MqttAsyncClient createClient(String serverURI, String clientId, MqttClientPersistence dataStore)
        throws org.eclipse.paho.client.mqttv3.MqttException {
    return spy(new MqttAsyncClientEx(serverURI, clientId, dataStore, this));
}
 
Example #18
Source File: AlMqttClient.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public AlMqttClient(String serverURI, String clientId, MqttClientPersistence persistence) throws MqttException {
    super(serverURI, clientId, persistence);
}
 
Example #19
Source File: AlMqttClient.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public AlMqttClient(String serverURI, String clientId, MqttClientPersistence persistence, ScheduledExecutorService executorService) throws MqttException {
    super(serverURI, clientId, persistence, executorService);
}
 
Example #20
Source File: MqttService.java    From Sparkplug with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Get an MqttConnection object to represent a connection to a server
 *
 * @param serverURI specifies the protocol, host name and port to be used to connect to an MQTT server
 * @param clientId specifies the name by which this connection should be identified to the server
 * @param contextId specifies the app conext info to make a difference between apps
 * @param persistence specifies the persistence layer to be used with this client
 * @return a string to be used by the Activity as a "handle" for this
 *         MqttConnection
 */
public String getClient(String serverURI, String clientId, String contextId, MqttClientPersistence persistence) {
  String clientHandle = serverURI + ":" + clientId+":"+contextId;
  if (!connections.containsKey(clientHandle)) {
    MqttConnection client = new MqttConnection(this, serverURI,
        clientId, persistence, clientHandle);
    connections.put(clientHandle, client);
  }
  return clientHandle;
}
 
Example #21
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Constructor- create an MqttAndroidClient that can be used to communicate
 * with an MQTT server on android
 * 
 * @param context
 *            used to pass context to the callback.
 * @param serverURI
 *            specifies the protocol, host name and port to be used to
 *            connect to an MQTT server
 * @param clientId
 *            specifies the name by which this connection should be
 *            identified to the server
 * @param persistence
 *            the persistence class to use to store in-flight message. If
 *            null then the default persistence mechanism is used
 * @param ackType
 *            how the application wishes to acknowledge a message has been
 *            processed.
 */
public MqttAndroidClient(Context context, String serverURI,
		String clientId, MqttClientPersistence persistence, Ack ackType) {
	myContext = context;
	this.serverURI = serverURI;
	this.clientId = clientId;
	this.persistence = persistence;
	messageAck = ackType;
}
 
Example #22
Source File: MqttBrokerConnection.java    From smarthome with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Encapsulates the creation of the paho MqttAsyncClient
 *
 * @param serverURI A paho uri like ssl://host:port, tcp://host:port, ws[s]://host:port
 * @param clientId the mqtt client ID
 * @param dataStore The datastore to save qos!=0 messages until they are delivered.
 * @return Returns a valid MqttAsyncClient
 * @throws org.eclipse.paho.client.mqttv3.MqttException
 */
protected MqttAsyncClient createClient(String serverURI, String clientId, MqttClientPersistence dataStore)
        throws org.eclipse.paho.client.mqttv3.MqttException {
    return new MqttAsyncClient(serverURI, clientId, dataStore);
}
 
Example #23
Source File: MqttStreamer.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the persistence mechanism. If not set, Paho will use its default.
 *
 * @param persistence A configurable persistence mechanism.
 */
public void setPersistence(MqttClientPersistence persistence) {
    this.persistence = persistence;
}
 
Example #24
Source File: MqttStreamer.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the currently set persistence mechanism.
 *
 * @return The persistence mechanism.
 */
public MqttClientPersistence getPersistence() {
    return persistence;
}
 
Example #25
Source File: MqttConfig.java    From quarks with Apache License 2.0 2 votes vote down vote up
/**
 * QoS 1 and 2 in-flight message persistence.
 * <p>
 * optional. default: use memory persistence.
 * @param persistence
 */
public void setPersistence(MqttClientPersistence persistence) {
    this.persistence = persistence;
}
 
Example #26
Source File: MqttConfig.java    From quarks with Apache License 2.0 2 votes vote down vote up
/**
 * Get the QoS 1 and 2 in-flight message persistence handler.
 * @return the value
 */
public MqttClientPersistence getPersistence() {
    return persistence;
}
 
Example #27
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Constructor - create an MqttAndroidClient that can be used to communicate
 * with an MQTT server on android
 * 
 * @param ctx
 *            Application's context
 * @param serverURI
 *            specifies the protocol, host name and port to be used to
 *            connect to an MQTT server
 * @param clientId
 *            specifies the name by which this connection should be
 *            identified to the server
 * @param persistence
 *            The object to use to store persisted data
 */
public MqttAndroidClient(Context ctx, String serverURI, String clientId, MqttClientPersistence persistence) {
	this(ctx, serverURI, clientId, persistence, Ack.AUTO_ACK);
}
 
Example #28
Source File: MqttClientFactory.java    From enmasse with Apache License 2.0 votes vote down vote up
C newInstance(String serverUri, String clientId, MqttClientPersistence persistence) throws MqttException;