Java Code Examples for org.eclipse.paho.client.mqttv3.MqttException#getMessage()

The following examples show how to use org.eclipse.paho.client.mqttv3.MqttException#getMessage() . 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: MqttClientInstance.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
private MqttClientInstance(final String url) {
    final String adjustedUrl = URLUtils.sanitizeURL(url);

    final UUID uuid = UUID.randomUUID();
    final String clientId = uuid.toString();
    final MemoryPersistence memoryPersistence = new MemoryPersistence();

    this.id = clientId;
    try {
        client = new MqttClient(adjustedUrl, "maestro.exchange." + clientId, memoryPersistence);

        Runtime.getRuntime().addShutdownHook(new Thread(this::terminate));
    }
    catch (MqttException e) {
        throw new MaestroConnectionException("Unable create a MQTT client instance : " + e.getMessage(),
                e);
    }

}
 
Example 2
Source File: SolaceController.java    From solace-samples-cloudfoundry-java with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/message", method = RequestMethod.POST)
  public ResponseEntity<String> sendMessage(@RequestBody SimpleMessage message) {

  	if( mqttClient == null )
  		return new ResponseEntity<>("{'description': 'Unable to perform operation, the MqttClient was not initialized'}", HttpStatus.INTERNAL_SERVER_ERROR);

  	if (!mqttClient.isConnected()) {
	logger.error("mqttClient was not connected, Could not send message");
	return new ResponseEntity<>("{'description': 'Unable to perform operation, the MqttClient was not connected!'}", HttpStatus.INTERNAL_SERVER_ERROR);
}

logger.info(
		"Sending message on topic: " + message.getTopic() + " with body: " + message.getBody());

try {
	MqttMessage mqttMessage = new MqttMessage(message.getBody().getBytes());
	mqttMessage.setQos(0);
	mqttClient.publish(message.getTopic(), mqttMessage);
	numMessagesSent.incrementAndGet();
} catch (MqttException e) {
	logger.error("sendMessage failed.", e);
          return new ResponseEntity<>("{'description': '" + e.getMessage() + "'}", HttpStatus.BAD_REQUEST);
}
      return new ResponseEntity<>("{'description': 'Message sent on topic " + message.getTopic() + "'}", HttpStatus.OK);
  }
 
Example 3
Source File: SolaceController.java    From solace-samples-cloudfoundry-java with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/subscription", method = RequestMethod.POST)
  public ResponseEntity<String> addSubscription(@RequestBody SimpleSubscription subscription) {
      String subscriptionTopic = subscription.getSubscription();
      logger.info("Adding a subscription to topic: " + subscriptionTopic);

      try {
      	if( mqttClient != null )
      		mqttClient.subscribe(subscriptionTopic);
      	else
      		return new ResponseEntity<>("{'description': 'Unable to perform operation, the MqttClient was not initialized'}", HttpStatus.INTERNAL_SERVER_ERROR);
          logger.info("Finishing Adding a subscription to topic: " + subscriptionTopic);
} catch (MqttException e) {
	logger.error("Subscription Creation failed.", e);
	return new ResponseEntity<>("{'description': '" + e.getMessage() + "'}", HttpStatus.BAD_REQUEST);
}
      return new ResponseEntity<>("{}", HttpStatus.OK);
  }
 
Example 4
Source File: SolaceController.java    From solace-samples-cloudfoundry-java with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/subscription/{subscriptionName}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteSubscription(@PathVariable("subscriptionName") String subscriptionTopic) {

       logger.info("Deleting a subscription to topic: " + subscriptionTopic);
	try {
           if( mqttClient != null )
       		mqttClient.unsubscribe(subscriptionTopic);
       	else
       		return new ResponseEntity<>("{'description': 'Unable to perform operation, the MqttClient was not initialized'}", HttpStatus.INTERNAL_SERVER_ERROR);
           logger.info("Finished Deleting a subscription to topic: " + subscriptionTopic);
	} catch (MqttException e) {
		logger.error("removeSubscription failed.", e);
		return new ResponseEntity<>("{'description': '" + e.getMessage() + "'}", HttpStatus.BAD_REQUEST);
	}

       return new ResponseEntity<>("{}", HttpStatus.OK);
   }
 
Example 5
Source File: MaestroMqttClient.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to the maestro broker
 * @throws MaestroConnectionException if unable to connect to the broker
 */
public void connect() throws MaestroConnectionException {
    try {
        if (!mqttClient.isConnected()) {
            final MqttConnectOptions connOpts = MqttClientInstance.getConnectionOptions();

            mqttClient.connect(connOpts);
        }
    }
    catch (MqttException e) {
        throw new MaestroConnectionException("Unable to establish a connection to Maestro: " + e.getMessage(), e);
    }
}
 
Example 6
Source File: MaestroMqttClient.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
/**
 * Disconnects from the maestro broker
 * @throws MaestroConnectionException if failed to disconnect cleanly (should be safe to ignore in most cases)
 */
public void disconnect() throws MaestroConnectionException {
    try {
        if (mqttClient.isConnected()) {
            mqttClient.disconnect();
        }
    }
    catch (MqttException e) {
        throw new MaestroConnectionException("Unable to disconnect cleanly from Maestro: " + e.getMessage(), e);
    }
}
 
Example 7
Source File: MaestroMqttClient.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribe to a topic
 *
 * @param topic the topic to subscribe
 * @param qos   the QOS for the topic
 */
public void subscribe(final String topic, int qos) {
    try {
        mqttClient.subscribe(topic, qos);
    } catch (MqttException e) {
        throw new MaestroConnectionException("Unable to subscribe to topic %s : %s", topic, e.getMessage(), e);
    }
}
 
Example 8
Source File: MaestroMqttClient.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
/**
 * Unsubscribe from a topic
 *
 * @param topic the topic to unsubscribe from
 */
public void unsubscribe(final String topic) {
    try {
        mqttClient.unsubscribe(topic);
    } catch (MqttException e) {
        throw new MaestroConnectionException("Unable to unsubscribe to topic %s : %s", topic, e.getMessage(), e);
    }
}
 
Example 9
Source File: MoquetteProxyContext.java    From cloud-pubsub-mqtt-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Connects to the broker running on localhost, so that we can publish messages to the broker.
 *
 * @throws IOException when you're unable to connect to the server.
 */
public void open() throws IOException {
  // TODO properly handle the exception
  try {
    client.connect();
  } catch (MqttException e) {
    // This exception is thrown if a client is already connected, connection in progress,
    // disconnecting, or client has been closed.
    throw new IOException("The client is in an inappropriate state for connecting."
        + e.getMessage());
  }
}
 
Example 10
Source File: MoquetteProxyContext.java    From cloud-pubsub-mqtt-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Terminates any resources that were used to publish messages to the broker.
 *
 * @throws IOException when the client is in an inappropriate state to disconnect.
 */
public void close() throws IOException {
  try {
    dataStore.close();
    client.disconnect();
    client.close();
  } catch (MqttException e) {
    // This exception is thrown if a client is not in an appropriate state for disconnecting.
    throw new IOException("The client is in an innapropriate state for disconnecting."
        + e.getMessage());
  }
}
 
Example 11
Source File: BlockingClient.java    From mqtt-jmeter with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void close() throws IOException{
    try {
        client.disconnect();
    } catch (MqttException e) {
        throw new IOException(e.getMessage(), e);
    }
}