Search in sources :

Example 1 with IMqttAsyncClient

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;
}
Also used : MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient)

Example 2 with IMqttAsyncClient

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;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient)

Example 3 with IMqttAsyncClient

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);
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient)

Example 4 with IMqttAsyncClient

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()));
    }
}
Also used : MqttMessageSentEvent(org.springframework.integration.mqtt.event.MqttMessageSentEvent) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient)

Example 5 with IMqttAsyncClient

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);
}
Also used : MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient)

Aggregations

IMqttAsyncClient (org.eclipse.paho.client.mqttv3.IMqttAsyncClient)6 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)3 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)2 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 HashMap (java.util.HashMap)1 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)1 MqttMessageSentEvent (org.springframework.integration.mqtt.event.MqttMessageSentEvent)1 MessagingException (org.springframework.messaging.MessagingException)1