use of org.eclipse.paho.client.mqttv3.IMqttAsyncClient in project mist by snuspl.
the class MQTTNoSharedResource method getMqttSinkClient.
@Override
public IMqttAsyncClient getMqttSinkClient(final String brokerURI, final String topic) throws MqttException, IOException {
final IMqttAsyncClient client = new MqttAsyncClient(brokerURI, MQTT_PUBLISHER_ID_PREFIX + sinkClientCounter.getAndIncrement());
final MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setMaxInflight(maxInflightMqttEventNum);
connectOptions.setKeepAliveInterval(mqttSinkKeepAliveSec);
client.connect(connectOptions).waitForCompletion();
return client;
}
use of org.eclipse.paho.client.mqttv3.IMqttAsyncClient in project spring-integration by spring-projects.
the class MqttPahoMessageHandler method checkConnection.
private synchronized IMqttAsyncClient checkConnection() throws MqttException {
if (this.client != null && !this.client.isConnected()) {
this.client.close();
this.client = null;
}
if (this.client == null) {
try {
MqttConnectOptions connectionOptions = this.clientFactory.getConnectionOptions();
Assert.state(this.getUrl() != null || connectionOptions.getServerURIs() != null, "If no 'url' provided, connectionOptions.getServerURIs() must not be null");
IMqttAsyncClient client = this.clientFactory.getAsyncClientInstance(this.getUrl(), this.getClientId());
incrementClientInstance();
client.setCallback(this);
client.connect(connectionOptions).waitForCompletion(this.completionTimeout);
this.client = client;
if (logger.isDebugEnabled()) {
logger.debug("Client connected");
}
} catch (MqttException e) {
throw new MessagingException("Failed to connect", e);
}
}
return this.client;
}
use of org.eclipse.paho.client.mqttv3.IMqttAsyncClient in project spring-integration by spring-projects.
the class MqttPahoMessageHandler method doStop.
@Override
protected void doStop() {
try {
IMqttAsyncClient client = this.client;
if (client != null) {
client.disconnect().waitForCompletion(this.completionTimeout);
client.close();
this.client = null;
}
} catch (MqttException e) {
logger.error("Failed to disconnect", e);
}
}
use of org.eclipse.paho.client.mqttv3.IMqttAsyncClient in project spring-integration by spring-projects.
the class MqttPahoMessageHandler method publish.
@Override
protected void publish(String topic, Object mqttMessage, Message<?> message) throws Exception {
Assert.isInstanceOf(MqttMessage.class, mqttMessage);
IMqttAsyncClient client = checkConnection();
IMqttDeliveryToken token = client.publish(topic, (MqttMessage) mqttMessage);
if (!this.async) {
token.waitForCompletion(this.completionTimeout);
} else if (this.asyncEvents && this.applicationEventPublisher != null) {
this.applicationEventPublisher.publishEvent(new MqttMessageSentEvent(this, message, topic, token.getMessageId(), getClientId(), getClientInstance()));
}
}
use of org.eclipse.paho.client.mqttv3.IMqttAsyncClient in project mist by snuspl.
the class MQTTSharedResource method createSinkClient.
/**
* A helper function which creates create sink client. Should be called with publisherLock acquired.
* @param brokerURI broker URI
* @param mqttAsyncClientList the client list which broker URI belongs to
* @return newly created sink client
* @throws MqttException
* @throws IOException
*/
private void createSinkClient(final String brokerURI, final List<IMqttAsyncClient> mqttAsyncClientList) throws MqttException, IOException {
final IMqttAsyncClient client = new MqttAsyncClient(brokerURI, MQTT_PUBLISHER_ID_PREFIX + taskHostname + brokerURI + mqttAsyncClientList.size());
final MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setMaxInflight(maxInflightMqttEventNum);
connectOptions.setKeepAliveInterval(mqttSinkKeepAliveSec);
client.connect(connectOptions).waitForCompletion();
mqttAsyncClientList.add(client);
publisherSinkNumMap.put(client, 0);
}
Aggregations