Skip to main content
Previous sectionNext section

Using the MQTT Adapters

This chapter describes the behavior of the MQTT inbound and outbound adapters, EnsLib.MQTT.Adapter.Inbound and EnsLib.MQTT.Adapter.Oubound and how to use them in custom business services and operations.

MQTT Inbound Adapter

The MQTT inbound adapter is responsible for subscribing to messages on the broker and then receiving messages from the subscription. The inbound adapter does the following:

  • On initialization, it creates the %Net.MQTT.Client client.

  • On task, it will:

    1. Check if it is connected to the broker. If it is being called the first time or the connection has broken, it uses the %Net.MQTT.Client client to connect to the broker and subscribe to the specified topic, which can optionally contain wildcards.

    2. Use the %Net.MQTT.Client client to receive a topic and a message.

    3. If the receive call does not time out, it calls the business service ProcessInput() method and passes it the message.

Creating a Business Service Using the MQTT Inbound Adapter

To use this adapter in your production, create a new business service class as described here. Later, compile it, add it to a production, and configure it in a similar manner as the passthrough service and operation as described in “Using the MQTT Passthrough Business Service and Operation.” If you need new custom message classes, create them as described in “Defining Messages” in Developing Productions.

The following list describes the basic requirements of the business service class:

For a description of the settings associated with the inbound adapter, see “Configuring and Using the MQTT Passthrough Business Service and Operation” and “Settings for the MQTT Adapter.”

The following example shows the overall structure of your business service class.

Class EMQTT.NewService1 Extends Ens.BusinessService 
{
Parameter ADAPTER = "EnsLib.MQTT.Adapter.Inbound";

Method OnProcessInput(pInput As EnsLib.MQTT.Message, pOutput As %RegisteredObject) As %Status
{
   set tsc=$$$OK
   //your code here
   Quit tsc
}
}
Copy code to clipboard

Implementing the OnProcessInput() Method

Within your custom business service class, the signature of your OnProcessInput() method should be similar to

Method OnProcessInput(pInput As EnsLib.MQTT.Message, pOutput As %RegisteredObject) As %Status
Copy code to clipboard

MQTT Outbound Adapter

The outbound adapter is responsible for publishing messages to the broker. When the outbound adapter is called with a message, it does the following:

  • It creates a connection to the broker if it does not already have one.

  • If its input parameter includes a value for the topic, it uses it. If the topic is not supplied, it uses the topic specified in the setting.

  • It publishes the message to the broker with the topic from its input parameter or the setting.