Using the MQTT Adapters
This topic describes the behavior of the MQTT inbound and outbound adapters, EnsLib.MQTT.Adapter.InboundOpens in a new tab and EnsLib.MQTT.Adapter.OutboundOpens in a new tab 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.ClientOpens in a new tab client.
-
On task, it will:
-
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.ClientOpens in a new tab client to connect to the broker and subscribe to the specified topic, which can optionally contain wildcards.
-
Use the %Net.MQTT.ClientOpens in a new tab client to receive a topic and a message.
-
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.
The following list describes the basic requirements of the business service class:
-
Your business service class should extend Ens.BusinessServiceOpens in a new tab.
-
In your class, the ADAPTER parameter should equal EnsLib.MQTT.Adapter.InboundOpens in a new tab.
-
Your class should implement the OnProcessInput() method, as described in Implementing the OnProcessInput() Method
-
For other options and general information, see Defining a 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
}
}
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
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.