B4J Question Detecting if StartMessageLoop is Active

nbarakat

Member
Hello All,

I am trying to find a way to detect if the StartMessageLoop is active or not. Something like:

If StartMessageLoop Then StopMessageLoop.

Is this possible to do in B4J?

Thanks everyone for the great community!
 

nbarakat

Member
Hi Erel and thank you for your response. Sorry for not clarifying enough in the first place.

Basically I have a server solution with a Handler that will handle many different type of requests for Example:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Elements = Regex.Split("/", req.RequestURI)

    ProcessRequest
    
    StartMessageLoop
    
End Sub

Private Sub ProcessRequest
    Try
        Select Case Request.Method.ToUpperCase
            Case "GET"
                Select Case Elements(Elements.Length - 1)
                    Case "customer" ' /
                         getCustomer(Request.GetHeader("cid"))
                    ...

            Case "POST"
                Select Case Elements(Elements.Length - 1)
                    Case "customer"
                        PostCustomer
                    Case "currency" 
                         addCurrency
                    Case "user"
                        PostUser
                    Case "lob"
                        ...
                    Case "exchangerate"
                        ....
                    Case "companies"
                        ...
                    Case ...                                    
                    Case Else                        
                        Utility.ReturnError("Bad Request", 400, Response)
                        StopMessageLoop
                End Select
            Case "PUT"
                ...
            Case "DELETE"
                ...
        End Select
    Catch
        LogError(LastException)
        Utility.ReturnError("Bad Request", 400, Response)
        StopMessageLoop
    End Try
End Sub

Some of these requests call resumable subs while others don't.

B4X:
Public Sub PostCustomer

    Dim inputJ As String = (Utility.RequestText(Request))
    Dim parser As JSONParser
    parser.Initialize(inputJ)
        Dim cust As clsCustomer
    cust.Initialize
    Dim root As Map = parser.NextObject
    'Dim Customers As List = root.Get("Customers")
    Dim User As Map = root.Get("User")
    cust.CustomerShortName = root.Get("CustomerShortName")
    cust.CustomerBaseCurrency = root.Get("CustomerBaseCurrency")
    cust.RootUserName = root.Get("RootUserName")
    cust.CustomerName = root.Get("CustomerName")
    cust.CID = root.Get("CID")
    
    
    Try
    
        
        Dim rs As ResumableSub = Main.DL.createCustomer(User.Get("UID"), User.Get("Fname"), User.Get("Lname"),1,cust)       
        Wait For (rs) Complete (Result As HttpResponseMessage)
            Utility.ReturnHttpResponse(Result,Response)

        StopMessageLoop

    Catch
        
        Log(LastException)
        StopMessageLoop
    End Try

End Sub

Public Sub PutCustomer (cid As Int, cName As String, cShortName As String, cBaseCurrency As Int) As HttpResponseMessage
        StopMessageLoop
        'Call to regular Sub
        UpdateCustomer
End Sub

It seems I need to add StopMessageLoop for all subs even if it is not a resumable sub. I.E. if PutCustomer is called without StopMessageLoop, it will freeze executing after it finishes the sub.

What I was thinking about was to add an if Statement at the end of ProcessRequest to detect the state of the message loop and based on that to stop it or start it so I won't need to add it at every regular sub as this could add the probability of a bug if one forgets to add that statement in the sub or Try/Catch ...

Maybe I still don't clearly understand the Resumable sub concept even though I read great information on the Forum about them most written by you.

Thanks for the help
 
Upvote 0

nbarakat

Member
Thanks Erel for your support and input. I just noticed a video tutorial on the subject which I will watch...I missed that during my search. I think I need to understand more the concept of resumable subs in API web handlers more before trying to use them...
 
Upvote 0
Top