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

The following examples show how to use org.eclipse.paho.client.mqttv3.MqttPersistenceException. 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 localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException {
    MqttMessage message = new MqttMessage(payload);
    message.setQos(qos);
    message.setRetained(retained);
    switch (type) {
        case Publisher:
            publishedMessage = new MQTTQueueMessage(topic, message);
            break;
        case Subscriber:
            try {
                mqttCallback.messageArrived(topic, message);
            } catch (Exception e) {
                throw new MqttException(e);
            }
            break;
    }
}
 
Example #2
Source File: MqttService.java    From android-mqtt-service with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a Keep Alive message to the specified topic
 * @see MQTT_KEEP_ALIVE_MESSAGE
 * @see MQTT_KEEP_ALIVE_TOPIC_FORMAT
 * @return MqttDeliveryToken specified token you can choose to wait for completion
 */
private synchronized MqttDeliveryToken sendKeepAlive()
throws MqttConnectivityException, MqttPersistenceException, MqttException {
        if(!isConnected())
                throw new MqttConnectivityException();

        if(mKeepAliveTopic == null) {
                mKeepAliveTopic = mClient.getTopic(
                        String.format(Locale.US, MQTT_KEEP_ALIVE_TOPIC_FORAMT,mDeviceId));
        }

        Log.i(DEBUG_TAG,"Sending Keepalive to " + MQTT_BROKER);

        MqttMessage message = new MqttMessage(MQTT_KEEP_ALIVE_MESSAGE);
        message.setQos(MQTT_KEEP_ALIVE_QOS);

        return mKeepAliveTopic.publish(message);
}
 
Example #3
Source File: MqttTestClient.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException {
    MqttMessage message = new MqttMessage(payload);
    message.setQos(qos);
    message.setRetained(retained);
    switch (type) {
        case Publisher:
            publishedMessage = new MQTTQueueMessage(topic, message);
            break;
        case Subscriber:
            try {
                mqttCallback.messageArrived(topic, message);
            } catch (Exception e) {
                throw new MqttException(e);
            }
            break;
    }
}
 
Example #4
Source File: MQService.java    From HelloMQTT with Apache License 2.0 6 votes vote down vote up
/**
 * 发送一个心跳包,保持长连接
 * @return MqttDeliveryToken specified token you can choose to wait for completion
 */
private synchronized MqttDeliveryToken sendKeepAlive()
        throws MqttConnectivityException, MqttPersistenceException, MqttException {
    if(!isConnected())
        throw new MqttConnectivityException();

    if(mKeepAliveTopic == null) {
        mKeepAliveTopic = mClient.getTopic(TOPIC);

    }
    Log.i(DEBUG_TAG,"Sending Keepalive to " + MQTT_BROKER);

    MqttMessage message = new MqttMessage(MQTT_KEEP_ALIVE_MESSAGE.getBytes());
    message.setQos(MQTT_KEEP_ALIVE_QOS);
    /**发送一个心跳包给服务器,然后回调到:messageArrived 方法中*/
   return mKeepAliveTopic.publish(message);
}
 
Example #5
Source File: MqttIndegoAdapter.java    From iot-device-bosch-indego-controller with Apache License 2.0 6 votes vote down vote up
/**
 * This marks the Indego device as offline.
 * 
 * @param mqttClient the connection to use
 * @throws MqttPersistenceException
 * @throws MqttException
 */
private void pushMqttStateOffline (MqttClient mqttClient) throws MqttPersistenceException, MqttException
{
    LOG.info("Pushing offline state to MQTT");

    publish(mqttClient, MQTT_TOPIC_ONLINE, false, true);
    publish(mqttClient, MQTT_TOPIC_STATE_CODE, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_STATE_MESSAGE, "", RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_ERROR_CODE, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_STATE_LEVEL, -2, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MOWED_PERCENTAGE, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MAP_SVG_CACHE_TS, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MAP_UPDATE_AVAILABLE, false, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MOWED_TS, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_TOTAL_OPERATE_MINS, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_TOTAL_CHARGE_MINS, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_SESSION_OPERATE_MINS, 0, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_SESSION_CHARGE_MINS, 0, RETAINMENT);
}
 
Example #6
Source File: MqttIndegoAdapter.java    From iot-device-bosch-indego-controller with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes a single topic on the MQTT broker
 * 
 * @param mqttClient the broker connection
 * @param topic the topic to publish (relative to configured topic root)
 * @param data the data to publish
 * @param retained if the data should be retained
 * @throws MqttPersistenceException
 * @throws MqttException
 */
private void publish (MqttClient mqttClient, String topic, String data, boolean retained) throws MqttPersistenceException, MqttException
{
    if ( LOG.isDebugEnabled() ) {
        LOG.debug(String.format("Publishing '%s' to topic '%s' (retained = %s)", data, topic, retained));
    }

    MqttMessage msg = new MqttMessage(data.getBytes());
    msg.setQos(configuration.getMqttQos());
    msg.setRetained(retained);
    mqttClient.publish(configuration.getMqttTopicRoot() + topic, msg);
}
 
Example #7
Source File: MqttConnectionFactory.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public void shutdown(boolean isClientConnected) {
    //need to clear the resources if and only if client holds the lock for the resource
    //that is client has made a successful connection to the server
    //this will clear the persistence resources and releases the the lock bound to that resource
    if (dataStore != null && isClientConnected) {
        try {
            dataStore.clear();
            dataStore.close();
        } catch (MqttPersistenceException ex) {
            log.error("Error while releasing the resources for data store", ex);
        }
    }
}
 
Example #8
Source File: MqttService.java    From android-mqtt-service with Apache License 2.0 5 votes vote down vote up
/**
 * Initalizes the DeviceId and most instance variables
 * Including the Connection Handler, Datastore, Alarm Manager
 * and ConnectivityManager.
 */
@Override
public void onCreate() {
        super.onCreate();

        mDeviceId = String.format(DEVICE_ID_FORMAT,
                                  Secure.getString(getContentResolver(), Secure.ANDROID_ID));

        HandlerThread thread = new HandlerThread(MQTT_THREAD_NAME);
        thread.start();

        mConnHandler = new Handler(thread.getLooper());

        try {
                mDataStore = new MqttDefaultFilePersistence(getCacheDir().getAbsolutePath());
        } catch(MqttPersistenceException e) {
                e.printStackTrace();
                mDataStore = null;
                mMemStore = new MemoryPersistence();
        }

        mOpts = new MqttConnectOptions();
        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 #9
Source File: MqttTestClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException {
    switch (type) {
        case Publisher:
            publishedMessage = new MQTTQueueMessage(topic, message);
            break;
        case Subscriber:
            try {
                mqttCallback.messageArrived(topic, message);
            } catch (Exception e) {
                throw new MqttException(e);
            }
            break;
    }
}
 
Example #10
Source File: MqttTestClient.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException {
    switch (type) {
        case Publisher:
            publishedMessage = new MQTTQueueMessage(topic, message);
            break;
        case Subscriber:
            try {
                mqttCallback.messageArrived(topic, message);
            } catch (Exception e) {
                throw new MqttException(e);
            }
            break;
    }
}
 
Example #11
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained, Object userContext,
        IMqttActionListener callback) throws MqttException, MqttPersistenceException {

    if (connection.publishSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getDeliveryToken(userContext, callback, topic);
}
 
Example #12
Source File: MqttAsyncClientEx.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained, Object userContext,
        IMqttActionListener callback) throws MqttException, MqttPersistenceException {

    if (connection.publishSuccess) {
        callback.onSuccess(getToken(userContext, callback, topic));
    } else {
        callback.onFailure(getToken(userContext, callback, topic), new MqttException(0));
    }
    return getDeliveryToken(userContext, callback, topic);
}
 
Example #13
Source File: MqttMain.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private void publishClientStatus(Boolean state) throws MqttException,
		MqttPersistenceException {
	if (!nullOrEmpty(publishClientInfoTopic)) {
		client.publish(publishClientInfoTopic, state.toString()
				.getBytes(), 0, RETAINED);
	}
}
 
Example #14
Source File: SparkplugExample.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
private void handleCommand(String command) 
		throws SparkplugException, MqttPersistenceException, MqttException, IOException {
	String [] tokens = command.split(" ");
	
	if (tokens.length > 0) {
		String cmd = tokens[0];
		if (cmd.equals("")) {
			return;
		}
		if (cmd.equals("?") || cmd.toLowerCase().equals("help")) {
			// Help with commands
			System.out.println("\nCOMMANDS");
			System.out.println(" - rebirth: Publishes a rebirth command to the Edge Node");
			System.out.println("     usage: rebirth");
			return;
		} else if (cmd.toLowerCase().equals("rebirth")) {
			// Issue a rebirth
			client.publish(NAMESPACE + "/" + groupId + "/NCMD/" + edgeNode, 
					new SparkplugBPayloadEncoder().getBytes(new SparkplugBPayloadBuilder()
							.addMetric(new MetricBuilder("Node Control/Rebirth", MetricDataType.Boolean, true)
									.createMetric())
							.createPayload()), 
					0, false);
			return;
		}
		
	}
	
	System.out.println("\nInvalid command: " + command);
}
 
Example #15
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Publishes a message to a topic on the server.
 * <p>
 * A convenience method, which will create a new {@link MqttMessage} object
 * with a byte array payload, the specified QoS and retained, then publish it.
 * </p>
 * 
 * @param topic
 *            to deliver the message to, for example "finance/stock/ibm".
 * @param payload
 *            the byte array to use as the payload
 * @param qos
 *            the Quality of Service to deliver the message at. Valid values
 *            are 0, 1 or 2.
 * @param retained
 *            whether or not this message should be retained by the server.
 * @param userContext
 *            optional object used to pass context to the callback. Use null
 *            if not required.
 * @param callback
 *            optional listener that will be notified when message delivery
 *            has completed to the requested quality of service
 * @return token used to track and wait for the publish to complete. The
 *         token will be passed to any callback that has been set.
 * @throws MqttPersistenceException
 *             when a problem occurs storing the message
 * @throws IllegalArgumentException
 *             if value of QoS is not 0, 1 or 2.
 * @throws MqttException
 *             for other errors encountered while publishing the message.
 *             For instance client not connected.
 * @see #publish(String, MqttMessage, Object, IMqttActionListener)
 */
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos,
		boolean retained, Object userContext, IMqttActionListener callback)
		throws MqttException, MqttPersistenceException {

	MqttMessage message = new MqttMessage(payload);
	message.setQos(qos);
	message.setRetained(retained);
	MqttDeliveryTokenAndroid token = new MqttDeliveryTokenAndroid(
			this, userContext, callback, message);
	String activityToken = storeToken(token);
	IMqttDeliveryToken internalToken = mqttService.publish(clientHandle,
			topic, payload, qos, retained, null, activityToken);
	token.setDelegate(internalToken);
	return token;
}
 
Example #16
Source File: MqttIndegoAdapter.java    From iot-device-bosch-indego-controller with Apache License 2.0 4 votes vote down vote up
/**
 * This writes the given device state to the data topics and sets the online state topic to true.
 * 
 * @param mqttClient the connection to use
 * @param state the Indego state to write out
 * @throws MqttPersistenceException
 * @throws MqttException
 */
private void pushMqttStateOnline (MqttClient mqttClient, DeviceStateInformation state) throws MqttPersistenceException, MqttException
{
    LOG.info("Pushing online state to MQTT");

    DeviceStatus status = DeviceStatus.decodeStatusCode(state.getState());

    int stateLevel;
    switch ( status.getAssociatedCommand() ) {
    case MOW:
        stateLevel = 2;
        break;
    case PAUSE:
        stateLevel = 1;
        break;
    case RETURN:
        stateLevel = 0;
        break;
    default:
        stateLevel = -1;
        break;
    }
    if ( state.getError() != 0 ) {
        stateLevel = -1;
    }

    publish(mqttClient, MQTT_TOPIC_ONLINE, true, true);
    publish(mqttClient, MQTT_TOPIC_STATE_CODE, status.getCode(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_STATE_MESSAGE, status.getMessage(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_ERROR_CODE, state.getError(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_STATE_LEVEL, stateLevel, RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MOWED_PERCENTAGE, state.getMowed(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MAP_SVG_CACHE_TS, state.getMapSvgCacheTimestamp(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MAP_UPDATE_AVAILABLE, state.isMapUpdateAvailable(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MOWED_TS, state.getMowedTimestamp(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_MOW_MODE, state.getMowMode(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_TOTAL_OPERATE_MINS, state.getRuntime().getTotal().getOperate(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_TOTAL_CHARGE_MINS, state.getRuntime().getTotal().getCharge(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_SESSION_OPERATE_MINS, state.getRuntime().getSession().getOperate(), RETAINMENT);
    publish(mqttClient, MQTT_TOPIC_RUNTIME_SESSION_CHARGE_MINS, state.getRuntime().getSession().getCharge(), RETAINMENT);
}
 
Example #17
Source File: AlMqttClient.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void publish(String topic, MqttMessage message, IMqttActionListener callback) throws MqttException,
        MqttPersistenceException {
    aClient.publish(topic, message, null, callback).waitForCompletion(getTimeToWait());
}
 
Example #18
Source File: MqttClientFactory.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException {
    return mqttClient.publish(topic, payload, qos, retained);
}
 
Example #19
Source File: MqttClientFactory.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained, Object userContext, IMqttActionListener callback)
        throws MqttException, MqttPersistenceException {
    return mqttClient.publish(topic, payload, qos, retained, userContext, callback);
}
 
Example #20
Source File: MqttClientFactory.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException {
    return mqttClient.publish(topic, message);
}
 
Example #21
Source File: MqttClientFactory.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
public IMqttDeliveryToken publish(String topic, MqttMessage message, Object userContext, IMqttActionListener callback) throws MqttException, MqttPersistenceException {
    return mqttClient.publish(topic, message, userContext, callback);
}
 
Example #22
Source File: MqttMain.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
private void publish(String topic, String message)
		throws MqttException, MqttPersistenceException {
	client.publish(topic, new MqttMessage(message.getBytes()));
}
 
Example #23
Source File: AnotherMqttClient.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
private void sendMessage(Message msg) throws MqttException,
		MqttPersistenceException {
	mqttClient.publish(msg.getTopic(), new MqttMessage(msg.getMessage()
			.getBytes()));
}
 
Example #24
Source File: AnotherMqttClient.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
public void switchDigitalPin(int pin, boolean value)
		throws MqttPersistenceException, MqttException {
	sendMessage(createSetMessage(newMsgBuilder().digitalPin(pin), value));
}
 
Example #25
Source File: AnotherMqttClient.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
public void switchAnalogPin(int pin, Object value)
		throws MqttPersistenceException, MqttException {
	sendMessage(createSetMessage(newMsgBuilder().analogPin(pin), value));
}
 
Example #26
Source File: MqttService.java    From Sparkplug with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Publish a message to a topic
 *
 * @param clientHandle
 *            identifies the MqttConnection to use
 * @param topic
 *            the topic to which to publish
 * @param message
 *            the message to publish
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary identifier to be passed back to the Activity
 * @throws MqttPersistenceException when a problem occurs storing the message
 * @throws MqttException if there was an error publishing the message
 * @return token for tracking the operation
 */
public IMqttDeliveryToken publish(String clientHandle, String topic,
    MqttMessage message, String invocationContext, String activityToken)
    throws MqttPersistenceException, MqttException {
  MqttConnection client = getConnection(clientHandle);
  return client.publish(topic, message, invocationContext, activityToken);
}
 
Example #27
Source File: MqttService.java    From Sparkplug with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Publish a message to a topic
 *
 * @param clientHandle
 *            identifies the MqttConnection to use
 * @param topic
 *            the topic to which to publish
 * @param payload
 *            the content of the message to publish
 * @param qos
 *            the quality of service requested
 * @param retained
 *            whether the MQTT server should retain this message
 * @param invocationContext
 *            arbitrary data to be passed back to the application
 * @param activityToken
 *            arbitrary identifier to be passed back to the Activity
 * @throws MqttPersistenceException when a problem occurs storing the message
 * @throws MqttException if there was an error publishing the message
 * @return token for tracking the operation
 */
public IMqttDeliveryToken publish(String clientHandle, String topic,
    byte[] payload, int qos, boolean retained,
    String invocationContext, String activityToken)
    throws MqttPersistenceException, MqttException {
  MqttConnection client = getConnection(clientHandle);
  return client.publish(topic, payload, qos, retained, invocationContext,
      activityToken);
}
 
Example #28
Source File: MqttProducer.java    From iot-ocp with Apache License 2.0 3 votes vote down vote up
public void run(String topic, String data) throws MqttPersistenceException, MqttException {
	
	MqttMessage message = new MqttMessage();
	message.setQos(QOS);
	
	message.setPayload(data.getBytes());
	client.publish(topic, message);
			
}
 
Example #29
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Publishes a message to a topic on the server.
 * <p>
 * Once this method has returned cleanly, the message has been accepted for
 * publication by the client and will be delivered on a background thread.
 * In the event the connection fails or the client stops, Messages will be
 * delivered to the requested quality of service once the connection is
 * re-established to the server on condition that:
    * </p>
 * <ul>
 * <li>The connection is re-established with the same clientID
 * <li>The original connection was made with (@link
 * MqttConnectOptions#setCleanSession(boolean)} set to false
 * <li>The connection is re-established with (@link
 * MqttConnectOptions#setCleanSession(boolean)} set to false
 * <li>Depending when the failure occurs QoS 0 messages may not be
 * delivered.
 * </ul>
 * 
 * <p>
 * When building an application, the design of the topic tree should take
 * into account the following principles of topic name syntax and semantics:
 * </p>
 * 
 * <ul>
 * <li>A topic must be at least one character long.</li>
 * <li>Topic names are case sensitive. For example, <em>ACCOUNTS</em> and
 * <em>Accounts</em> are two different topics.</li>
 * <li>Topic names can include the space character. For example,
 * <em>Accounts
 * 	payable</em> is a valid topic.</li>
 * <li>A leading "/" creates a distinct topic. For example,
 * <em>/finance</em> is different from <em>finance</em>. <em>/finance</em>
 * matches "+/+" and "/+", but not "+".</li>
 * <li>Do not include the null character (Unicode <em>\x0000</em>) in any topic.</li>
 * </ul>
 * 
 * <p>
 * The following principles apply to the construction and content of a topic
 * tree:
 * </p>
 * 
 * <ul>
 * <li>The length is limited to 64k but within that there are no limits to
 * the number of levels in a topic tree.</li>
 * <li>There can be any number of root nodes; that is, there can be any
 * number of topic trees.</li>
 * </ul>
 * <p>
 * The method returns control before the publish completes. Completion can
 * be tracked by:
    * </p>
 * <ul>
 * <li>Setting an {@link IMqttAsyncClient#setCallback(MqttCallback)} where
 * the {@link MqttCallback#deliveryComplete(IMqttDeliveryToken)} method will
 * be called.</li>
 * <li>Waiting on the returned token {@link MqttToken#waitForCompletion()}
 * or</li>
 * <li>Passing in a callback {@link IMqttActionListener} to this method</li>
 * </ul>
 * 
 * @param topic
 *            to deliver the message to, for example "finance/stock/ibm".
 * @param message
 *            to deliver to the server
 * @param userContext
 *            optional object used to pass context to the callback. Use null
 *            if not required.
 * @param callback
 *            optional listener that will be notified when message delivery
 *            has completed to the requested quality of service
 * @return token used to track and wait for the publish to complete. The
 *         token will be passed to callback methods if set.
 * @throws MqttPersistenceException
 *             when a problem occurs storing the message
 * @throws IllegalArgumentException
 *             if value of QoS is not 0, 1 or 2.
 * @throws MqttException
 *             for other errors encountered while publishing the message.
 *             For instance, client not connected.
 * @see MqttMessage
 */
@Override
public IMqttDeliveryToken publish(String topic, MqttMessage message,
		Object userContext, IMqttActionListener callback)
		throws MqttException, MqttPersistenceException {
	MqttDeliveryTokenAndroid token = new MqttDeliveryTokenAndroid(
			this, userContext, callback, message);
	String activityToken = storeToken(token);
	IMqttDeliveryToken internalToken = mqttService.publish(clientHandle,
			topic, message, null, activityToken);
	token.setDelegate(internalToken);
	return token;
}
 
Example #30
Source File: MqttAndroidClient.java    From Sparkplug with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Publishes a message to a topic on the server. Takes an
 * {@link MqttMessage} message and delivers it to the server at the
 * requested quality of service.
 * 
 * @param topic
 *            to deliver the message to, for example "finance/stock/ibm".
 * @param message
 *            to deliver to the server
 * @return token used to track and wait for the publish to complete. The
 *         token will be passed to any callback that has been set.
 * @throws MqttPersistenceException
 *             when a problem occurs storing the message
 * @throws IllegalArgumentException
 *             if value of QoS is not 0, 1 or 2.
 * @throws MqttException
 *             for other errors encountered while publishing the message.
 *             For instance client not connected.
 * @see #publish(String, MqttMessage, Object, IMqttActionListener)
 */
@Override
public IMqttDeliveryToken publish(String topic, MqttMessage message)
		throws MqttException, MqttPersistenceException {
	return publish(topic, message, null, null);
}