Search in sources :

Example 1 with MqttConnectionFailedEvent

use of org.springframework.integration.mqtt.event.MqttConnectionFailedEvent in project spring-integration by spring-projects.

the class MqttPahoMessageDrivenChannelAdapter method connectionLost.

@Override
public synchronized void connectionLost(Throwable cause) {
    if (isRunning()) {
        this.logger.error("Lost connection: " + cause.getMessage() + "; retrying...");
        this.connected = false;
        scheduleReconnect();
        if (this.applicationEventPublisher != null) {
            this.applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, cause));
        }
    }
}
Also used : MqttConnectionFailedEvent(org.springframework.integration.mqtt.event.MqttConnectionFailedEvent)

Example 2 with MqttConnectionFailedEvent

use of org.springframework.integration.mqtt.event.MqttConnectionFailedEvent in project spring-integration by spring-projects.

the class MqttPahoMessageDrivenChannelAdapter method connectAndSubscribe.

private synchronized void connectAndSubscribe() throws MqttException {
    MqttConnectOptions connectionOptions = this.clientFactory.getConnectionOptions();
    this.cleanSession = connectionOptions.isCleanSession();
    this.consumerStopAction = this.clientFactory.getConsumerStopAction();
    if (this.consumerStopAction == null) {
        this.consumerStopAction = ConsumerStopAction.UNSUBSCRIBE_CLEAN;
    }
    Assert.state(getUrl() != null || connectionOptions.getServerURIs() != null, "If no 'url' provided, connectionOptions.getServerURIs() must not be null");
    this.client = this.clientFactory.getClientInstance(getUrl(), getClientId());
    this.client.setCallback(this);
    if (this.client instanceof MqttClient) {
        ((MqttClient) this.client).setTimeToWait(this.completionTimeout);
    }
    this.topicLock.lock();
    String[] topics = getTopic();
    try {
        this.client.connect(connectionOptions);
        int[] requestedQos = getQos();
        int[] grantedQos = Arrays.copyOf(requestedQos, requestedQos.length);
        this.client.subscribe(topics, grantedQos);
        for (int i = 0; i < requestedQos.length; i++) {
            if (grantedQos[i] != requestedQos[i]) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Granted QOS different to Requested QOS; topics: " + Arrays.toString(topics) + " requested: " + Arrays.toString(requestedQos) + " granted: " + Arrays.toString(grantedQos));
                }
                break;
            }
        }
    } catch (MqttException e) {
        if (this.applicationEventPublisher != null) {
            this.applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, e));
        }
        logger.error("Error connecting or subscribing to " + Arrays.toString(topics), e);
        this.client.disconnectForcibly(this.completionTimeout);
        throw e;
    } finally {
        this.topicLock.unlock();
    }
    if (this.client.isConnected()) {
        this.connected = true;
        String message = "Connected and subscribed to " + Arrays.toString(topics);
        if (logger.isDebugEnabled()) {
            logger.debug(message);
        }
        if (this.applicationEventPublisher != null) {
            this.applicationEventPublisher.publishEvent(new MqttSubscribedEvent(this, message));
        }
    }
}
Also used : IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttConnectionFailedEvent(org.springframework.integration.mqtt.event.MqttConnectionFailedEvent) MqttSubscribedEvent(org.springframework.integration.mqtt.event.MqttSubscribedEvent)

Aggregations

MqttConnectionFailedEvent (org.springframework.integration.mqtt.event.MqttConnectionFailedEvent)2 IMqttClient (org.eclipse.paho.client.mqttv3.IMqttClient)1 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1 MqttSubscribedEvent (org.springframework.integration.mqtt.event.MqttSubscribedEvent)1