eWON JTK

com.ewon.ewonitf
Class MqttClient

java.lang.Object
  extended by com.ewon.ewonitf.MqttClient
All Implemented Interfaces:
EvtIntListener, EvtListener

public abstract class MqttClient
extends Object
implements EvtIntListener


Constructor Summary
MqttClient(String MqttId, String Host)
          Create an MQTT client instance.
 
Method Summary
 void call(int eventHandler, String nullStr, int clientHandle)
          Internal use only, this function will be called when an MQTT event occurs.
abstract  void callMqttEvent(int event)
          This abstract function must be defined in your actual MQTT client.
 void close()
          Call this function when the client must not be used any more to release all allocated resources.
 void connect()
          Connect to the selected host with the parameters configured.
protected  void finalize()
           
 int getIntRef()
          Internally used for event management.
 void publish(MqttMessage msg, int QoS, boolean retain)
          Call this function to publish a message to the broker.
 MqttMessage readMessage()
          Read a message from the receive queue.
 void setOption(String paramName, String paramValue)
          Configure an option in the client.
 void subscribe(String subscriptionPattern, int QoS)
          Subscribe to a topic pattern.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MqttClient

public MqttClient(String MqttId,
                  String Host)
           throws Exception
Create an MQTT client instance.

Parameters:
MqttId - String used to identify the client in the broker.
Host - The IP address or name of the broker.
Throws:
Exception - if creation fails.
Method Detail

getIntRef

public int getIntRef()
Internally used for event management.

Specified by:
getIntRef in interface EvtIntListener
Returns:
Integer reference for event management.

setOption

public void setOption(String paramName,
                      String paramValue)
               throws EWException
Configure an option in the client. This is typically done before calling the connect() function to configure the connection.

Parameters:
paramName - The parameter to configure.

possible values are:

  • username: for connection requiring authentication. See also "password". Not used if ""
  • password: password for "username". Must be set AFTER "username"
  • port: IP port for connection to the broker. Default to 1883
paramValue - The value to set the parameter.
Throws:
EWException

connect

public void connect()
             throws EWException
Connect to the selected host with the parameters configured. This function will also start listening to messages received and subsequently call the {@link #callMqttEvent(com.ewon.ewonitf.MqttMessage, int) function.

Throws:
EWException

subscribe

public void subscribe(String subscriptionPattern,
                      int QoS)
               throws EWException
Subscribe to a topic pattern.

 
 

For example, for a topic of "a/b/c/d", the following example subscriptions will match:

  • a/b/c/d
  • +/b/c/d
  • a/+/c/d
  • a/+/+/d
  • +/+/+/+

The following subscriptions will not match:

  • a/b/c
  • b/+/c/d
  • +/+/+

# can be used as a wildcard for all remaining levels of hierarchy. This means that it must be the final character in a subscription. With a topic of "a/b/c/d", the following example subscriptions will match:

  • a/b/c/d
  • #
  • a/#
  • a/b/#
  • a/b/c/#
  • +/b/c/#

Parameters:
subscriptionPattern - The pattern for the subscription.
QoS - The quality of service for the subscription.
Throws:
EWException

readMessage

public MqttMessage readMessage()
                        throws EWException
Read a message from the receive queue.

Returns:
an MqttMessage object or null if no message was available.
Throws:
EWException

close

public void close()
Call this function when the client must not be used any more to release all allocated resources.


publish

public void publish(MqttMessage msg,
                    int QoS,
                    boolean retain)
             throws EWException
Call this function to publish a message to the broker.

This is an example of how to publish a string to the root/home topic.

 
    myMqttClient.publish(new MqttMessage("root/home","abdc", 0, false);
 

Parameters:
msg - The message instance to publish.
QoS - The QoS to be used for publishing the message.
retain - Define if the message must be retained by the broker for future clients that will connect.
Throws:
EWException

callMqttEvent

public abstract void callMqttEvent(int event)
This abstract function must be defined in your actual MQTT client. It will be called every time an MQTT message is received with the actual message. Other events could trigger the function in the future so you should always check if msg is not null.

Parameters:
event - is reserved for future use. Currently always 0.

call

public void call(int eventHandler,
                 String nullStr,
                 int clientHandle)
Internal use only, this function will be called when an MQTT event occurs. Currently used only for receiving MQTT messages.

Specified by:
call in interface EvtListener
Parameters:
eventHandler -
nullStr -
clientHandle -

finalize

protected void finalize()

eWON JTK

www.eWON.biz: Your Eyes Watching Over Net