B4J Question JServer - Run mqtt class in a different thread

hatzisn

Expert
Licensed User
Longtime User
Good evening everyone (or good morning if in your timezone it is morning),

I have created an mqtt class that works flawlessly in a JServer app but it must be run in the main thread which locks it if it receives a published message.
I was looking to run the class in a different thread but I cannot figure out how to do this with the Threading library by @agraham. It has a class object but I am not sure how to do it because I do not control the executing of the MessageReceived sub but the Payload sender does. So what should I do in such a case?

Edit - I forgot to mention that it does a time consuming database procedure on mqtt message received.
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
different thread but I cannot figure out how to do this with the Threading library by @agraham
Jserver has a background workers
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thank you, I figured it out a little after I posted the message. I thought of using an "inception" class (the mqtt class in a background worker class) and I was experimenting with it before I post my answer. Nevertheless I deeply thank you for your answer.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
The inception class works. B4X the Great.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
* The inception class is because the mqtt class uses arguments in initialize.
 
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
Could you please provide some code snippet that would help others to understand how you solved your problem? How does your interception class look like? How does it interact with the Mqtt Broker?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Could you please provide some code snippet that would help others to understand how you solved your problem? How does your interception class look like? How does it interact with the Mqtt Broker?

Hi, I used the word "inception" from the same name movie which used some kind of similar notion. The similarity is that I use a class in a class just like in the movie they used a dream in a dream. Here is the code. Do not worry about what you do not understand because it is related to my class. This code is in a class named "Worker1" and you add it with an srvr.AddBackgroundWorker("Worker1") in Main module.

B4X:
Sub Class_Globals
    Private tim As Timer
    Private iCount As Int = 0
    Private mqtt As MyMQTT
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    tim.Initialize("tim", 1000)
    tim_Tick
    tim.Enabled = True
    InitializeMyMQTT
    StartMessageLoop
End Sub



Sub InitializeMyMQTT
    mqtt.Initialize(Me, "mq")
    mqtt.InitializeBroker("user", "passforuser", 56000)
    mqtt.InitializeClient("user", "passforuser", "127.0.0.1", 56000)
    mqtt.Connect
    Do While True
        Sleep(1000)
        Log("In Here")
        If mqtt.IsClientConnected Then
            mqtt.Subscribe("SampleTopic", mqtt.QOS_0_MOST_ONCE)
            Exit
        End If
    Loop
End Sub

Private Sub mq_Connected(Success As Boolean)

End Sub

Private Sub mq_Connected2(Success As Boolean, ClientID As String)

End Sub

Private Sub mq_Disconnected

End Sub

Private Sub mq_Disconnected2(ClientID As String)

End Sub

Private Sub mq_MessageArrived(Topic As String, Payload() As Byte)
    Log(BytesToString(Payload, 0, Payload.Length, "UTF-8"))
End Sub

Sub StopEverything
    #if b4i

    #Else
    Try
        mqtt.StopBroker
    Catch
        Log(LastException)
    End Try
    #End If

    Try
        mqtt.StopClient
    Catch
        Log(LastException)
    End Try
End Sub


Private Sub tim_Tick
    iCount = iCount + 1
    Log(iCount)
End Sub
 
Upvote 0
Top