Class Mqttv5PahoMessageDrivenChannelAdapter

java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.endpoint.AbstractEndpoint
org.springframework.integration.endpoint.MessageProducerSupport
org.springframework.integration.mqtt.inbound.AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
org.springframework.integration.mqtt.inbound.Mqttv5PahoMessageDrivenChannelAdapter
All Implemented Interfaces:
org.eclipse.paho.mqttv5.client.MqttCallback, Aware, BeanFactoryAware, BeanNameAware, DisposableBean, InitializingBean, SmartInitializingSingleton, ApplicationContextAware, ApplicationEventPublisherAware, Lifecycle, Phased, SmartLifecycle, ExpressionCapable, MessageProducer, IntegrationPattern, ClientManager.ConnectCallback, MqttComponent<org.eclipse.paho.mqttv5.client.MqttConnectionOptions>, NamedComponent, IntegrationInboundManagement, IntegrationManagement, ManageableLifecycle, ManageableSmartLifecycle, TrackableComponent

public class Mqttv5PahoMessageDrivenChannelAdapter extends AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions> implements org.eclipse.paho.mqttv5.client.MqttCallback, MqttComponent<org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
The AbstractMqttMessageDrivenChannelAdapter implementation for MQTT v5.

The MqttProperties are mapped via the provided HeaderMapper; meanwhile the regular MqttMessage properties are always mapped into headers.

It is recommended to have the MqttConnectionOptions.setAutomaticReconnect(boolean) set to true to let an internal IMqttAsyncClient instance to handle reconnects. Otherwise, only the manual restart of this component can handle reconnects, e.g. via MqttConnectionFailedEvent handling on disconnection.

See setPayloadType(java.lang.Class<?>) for more information about type conversion.

Since:
5.5.5
Author:
Artem Bilan, Mikhail Polivakha, Lucas Bowler, Artem Vozhdayenko, Matthias Thoma
  • Constructor Details

    • Mqttv5PahoMessageDrivenChannelAdapter

      public Mqttv5PahoMessageDrivenChannelAdapter(String url, String clientId, org.eclipse.paho.mqttv5.common.MqttSubscription... mqttSubscriptions)
      Create an instance based on the MQTT url, client id and subscriptions.
      Parameters:
      url - the MQTT url to connect.
      clientId - the unique client id.
      mqttSubscriptions - the MQTT subscriptions.
      Since:
      6.3
    • Mqttv5PahoMessageDrivenChannelAdapter

      public Mqttv5PahoMessageDrivenChannelAdapter(String url, String clientId, String... topic)
    • Mqttv5PahoMessageDrivenChannelAdapter

      public Mqttv5PahoMessageDrivenChannelAdapter(org.eclipse.paho.mqttv5.client.MqttConnectionOptions connectionOptions, String clientId, org.eclipse.paho.mqttv5.common.MqttSubscription... mqttSubscriptions)
      Create an instance based on the MQTT connection options, client id and subscriptions.
      Parameters:
      connectionOptions - the MQTT connection options.
      clientId - the unique client id.
      mqttSubscriptions - the MQTT subscriptions.
      Since:
      6.3
    • Mqttv5PahoMessageDrivenChannelAdapter

      public Mqttv5PahoMessageDrivenChannelAdapter(org.eclipse.paho.mqttv5.client.MqttConnectionOptions connectionOptions, String clientId, String... topic)
    • Mqttv5PahoMessageDrivenChannelAdapter

      public Mqttv5PahoMessageDrivenChannelAdapter(ClientManager<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions> clientManager, org.eclipse.paho.mqttv5.common.MqttSubscription... mqttSubscriptions)
      Create an instance based on the client manager and subscriptions.
      Parameters:
      clientManager - The client manager.
      mqttSubscriptions - the MQTT subscriptions.
      Since:
      6.3
    • Mqttv5PahoMessageDrivenChannelAdapter

      public Mqttv5PahoMessageDrivenChannelAdapter(ClientManager<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions> clientManager, String... topic)
      Use this constructor when you need to use a single ClientManager (for instance, to reuse an MQTT connection).
      Parameters:
      clientManager - The client manager.
      topic - The topic(s).
      Since:
      6.0
  • Method Details

    • getConnectionInfo

      public org.eclipse.paho.mqttv5.client.MqttConnectionOptions getConnectionInfo()
      Description copied from interface: MqttComponent
      Return information about the connection.
      Specified by:
      getConnectionInfo in interface MqttComponent<org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
      Returns:
      the information.
    • setPersistence

      public void setPersistence(@Nullable org.eclipse.paho.mqttv5.client.MqttClientPersistence persistence)
    • setConverter

      public void setConverter(MqttMessageConverter converter)
      Overrides:
      setConverter in class AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
    • setMessageConverter

      public void setMessageConverter(SmartMessageConverter messageConverter)
    • setPayloadType

      public void setPayloadType(Class<?> payloadType)
      Set the type of the target message payload to produce after conversion from MQTT message. Defaults to byte[].class - just extract MQTT message payload without conversion. Can be set to MqttMessage class to produce the whole MQTT message as a payload.
      Parameters:
      payloadType - the expected payload type to convert MQTT message to.
    • setHeaderMapper

      public void setHeaderMapper(HeaderMapper<org.eclipse.paho.mqttv5.common.packet.MqttProperties> headerMapper)
    • onInit

      protected void onInit()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this for initialization logic.
      Overrides:
      onInit in class AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
    • doStart

      protected void doStart()
      Description copied from class: MessageProducerSupport
      Take no action by default. Subclasses may override this if they need lifecycle-managed behavior. Protected by 'lifecycleLock'.
      Overrides:
      doStart in class MessageProducerSupport
    • doStop

      protected void doStop()
      Description copied from class: MessageProducerSupport
      Take no action by default. Subclasses may override this if they need lifecycle-managed behavior.
      Overrides:
      doStop in class MessageProducerSupport
    • destroy

      public void destroy()
      Specified by:
      destroy in interface DisposableBean
      Specified by:
      destroy in interface IntegrationManagement
      Overrides:
      destroy in class AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
    • setQos

      public void setQos(int... qos)
      Description copied from class: AbstractMqttMessageDrivenChannelAdapter
      Set the QoS for each topic; a single value will apply to all topics otherwise the correct number of qos values must be provided.
      Overrides:
      setQos in class AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
      Parameters:
      qos - The qos value(s).
    • addTopic

      public void addTopic(String topic, int qos)
      Description copied from class: AbstractMqttMessageDrivenChannelAdapter
      Add a topic to the subscribed list.
      Overrides:
      addTopic in class AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
      Parameters:
      topic - The topic.
      qos - The qos.
    • removeTopic

      public void removeTopic(String... topic)
      Description copied from class: AbstractMqttMessageDrivenChannelAdapter
      Remove a topic (or topics) from the subscribed list.
      Overrides:
      removeTopic in class AbstractMqttMessageDrivenChannelAdapter<org.eclipse.paho.mqttv5.client.IMqttAsyncClient,org.eclipse.paho.mqttv5.client.MqttConnectionOptions>
      Parameters:
      topic - The topic.
    • messageArrived

      public void messageArrived(String topic, org.eclipse.paho.mqttv5.common.MqttMessage mqttMessage)
      Specified by:
      messageArrived in interface org.eclipse.paho.mqttv5.client.MqttCallback
    • disconnected

      public void disconnected(org.eclipse.paho.mqttv5.client.MqttDisconnectResponse disconnectResponse)
      Specified by:
      disconnected in interface org.eclipse.paho.mqttv5.client.MqttCallback
    • mqttErrorOccurred

      public void mqttErrorOccurred(org.eclipse.paho.mqttv5.common.MqttException exception)
      Specified by:
      mqttErrorOccurred in interface org.eclipse.paho.mqttv5.client.MqttCallback
    • deliveryComplete

      public void deliveryComplete(org.eclipse.paho.mqttv5.client.IMqttToken token)
      Specified by:
      deliveryComplete in interface org.eclipse.paho.mqttv5.client.MqttCallback
    • connectComplete

      public void connectComplete(boolean isReconnect)
      Description copied from interface: ClientManager.ConnectCallback
      Called when the connection to the server is completed successfully.
      Specified by:
      connectComplete in interface ClientManager.ConnectCallback
      Parameters:
      isReconnect - if true, the connection was the result of automatic reconnect.
    • connectComplete

      public void connectComplete(boolean reconnect, String serverURI)
      Specified by:
      connectComplete in interface org.eclipse.paho.mqttv5.client.MqttCallback
    • authPacketArrived

      public void authPacketArrived(int reasonCode, org.eclipse.paho.mqttv5.common.packet.MqttProperties properties)
      Specified by:
      authPacketArrived in interface org.eclipse.paho.mqttv5.client.MqttCallback