B4J Question MQTT MqttConnectOptions

madru

Active Member
Licensed User
Longtime User
Hi,

how can I set
"setConnectionTimeout", "setKeepAliveInterval", "setAutomaticReconnect", "setMqttVersion" and "setMaxInflight"?

fails with
java.lang.RuntimeException: Method: getMaxInflight not found in: org.eclipse.paho.client.mqttv3.MqttConnectOptions
java.lang.RuntimeException: Method: isAutomaticReconnect not found in: org.eclipse.paho.client.mqttv3.MqttConnectOptions
java.lang.RuntimeException: Method: setAutomaticReconnect not found in: org.eclipse.paho.client.mqttv3.MqttConnectOptions
java.lang.RuntimeException: Method: setCleanSession( not found in: org.eclipse.paho.client.mqttv3.MqttConnectOptions

B4X:
    Dim obj,jo As JavaObject
    Dim result As String
    obj = jo.InitializeNewInstance("org.eclipse.paho.client.mqttv3.MqttConnectOptions",Null)
    Log("Object Test result:"  & obj)
   
    result= obj.RunMethod("getConnectionTimeout",Null)
    Log("getConnectionTimeout:"  & result)
       
    result= obj.RunMethod("getDebug",Null)
    Log("getDebug:"  & result)
       
    result= obj.RunMethod("getKeepAliveInterval",Null)
    Log("getKeepAliveInterval:"  & result)
       
    'result= obj.RunMethod("getMaxInflight",Null)
    'Log("getMaxInflight:"  & result)
       
    result= obj.RunMethod("getMqttVersion",Null)
    Log("getMqttVersion:"  & result)
       
    result= obj.RunMethod("getPassword",Null)
    Log("getPassword:"  & result)
       
    result= obj.RunMethod("getServerURIs",Null)
    Log("getServerURIs:"  & result)
       
    result= obj.RunMethod("getSocketFactory",Null)
    Log("getSocketFactory:"  & result)
       
    result= obj.RunMethod("getSSLProperties",Null)
    Log("getSSLProperties:"  & result)
       
    result= obj.RunMethod("getUserName",Null)
    Log("getUserName:"  & result)
       
    result= obj.RunMethod("getWillDestination",Null)
    Log("getWillDestination:"  & result)
       
    result= obj.RunMethod("getWillMessage",Null)
    Log("getWillMessage:"  & result)
       
    'result= obj.RunMethod("isAutomaticReconnect",Null)
    'Log("isAutomaticReconnect:"  & result)
       
    result= obj.RunMethod("isCleanSession",Null)
    Log("isCleanSession:"  & result)
       
    'result= obj.RunMethod("setAutomaticReconnect",Null)
    'Log("setAutomaticReconnect:"  & result)
       
    'result= obj.RunMethod("setCleanSession(",Null)
    'Log("setCleanSession:"  & result)

    result= obj.RunMethod("setConnectionTimeout",Array As Object( 20 )) 'not working
    Log("setConnectionTimeout:"  & result)
       
    result= obj.RunMethod("setKeepAliveInterval",Array As Object( 20 )) 'not working
    Log("setKeepAliveInterval:"  & result)
       
    'result= obj.RunMethod("setMaxInflight",Null)
    'Log("setMaxInflight:"  & result)
       
    result= obj.RunMethod("setMqttVersion",Array As Object( 0 )) 'not working
    Log("setMqttVersion:"  & result)

    'result= obj.RunMethod("setPassword",Null)
    'Log("setPassword:"  & result)
       
    'result= obj.RunMethod("setServerURIs",Null)
    'Log("setServerURIs:"  & result)
       
    'result= obj.RunMethod("setSSLProperties",Null)
    'Log("setSSLProperties:"  & result)
       
    'result= obj.RunMethod("setUserName",Null)
    'Log("setUserName:"  & result)
           
    'result= obj.RunMethod("setWill",Null)
    'Log("setWill:"  & result)

============== Connection options ==============
CleanSession : true
SocketFactory : null
MqttVersion : 0
KeepAliveInterval : 60
ConTimeout : 30
UserName : null
SSLProperties : null
WillDestination : null
==========================================
getConnectionTimeout:30
getDebug:{SocketFactory=null, CleanSession=true, MqttVersion=0, KeepAliveInterval=60, ConTimeout=30, UserName=null, SSLProperties=null, WillDestination=null}
getKeepAliveInterval:60
getMqttVersion:0
getPassword:null
getServerURIs:null
getSocketFactory:null
getSSLProperties:null
getUserName:null
getWillDestination:null
getWillMessage:null
isCleanSession:true
setKeepAliveInterval:null

THX
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
These methods are available in the MQTT library:

SS-2017-05-22_18.11.53.png
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
mmh, any change to get the actual Paho version in B4x soon?

some observations:
e.g. looks like that setting the "setConnectionTimeout" does not work, (maybe I do it wrong) I can set and read the value back but it does not have an effect


B4X:
    Dim mqtt As MqttClient
    Dim obj,jo As JavaObject
    Dim result As String

    mqtt.Initialize("MQTT","tcp://192.168.1.232:1883","1234")
    obj = jo.InitializeNewInstance("org.eclipse.paho.client.mqttv3.MqttConnectOptions",Null)
    Log("Object Test result:"  & obj)

'Object Test result:(MqttConnectOptions) 
'============== Connection options ==============
'CleanSession                :  true
'SocketFactory               :  null
'MqttVersion                 :  0
'KeepAliveInterval           :  60
'ConTimeout                  :  30
'UserName                    :  null
'SSLProperties               :  null
'WillDestination             :  null
'==========================================

    mqtt.Connect

    result= obj.RunMethod("setConnectionTimeout",Array As Object( 2 )) 'not working
    Log("setConnectionTimeout:"  & result)
''    setConnectionTimeout:null

    result= obj.RunMethod("getConnectionTimeout",Null)
    Log("getConnectionTimeout:"  & result)
''    getConnectionTimeout:2

    mqtt.Subscribe("test",1)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code is wrong.
1. No reason to use InitializeNewInstance. Create a regular MqttConnectOptions. You can access additional methods with:
B4X:
Dim jo As JavaObject = MqttConnectionOptions1
jo.RunMethod(...)
2. You must call mqtt.Connect2 and pass the options object.

mmh, any change to get the actual Paho version in B4x soon?
Post a feature request in the wishlist forum and explain why you need the library to be updated.
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
sometimes it is so easy.....


THX

I will put a feature request in later or tomorrow
 
Upvote 0
Top