B4J Question Resumale sub in server handler

marcick

Well-Known Member
Licensed User
Longtime User
I have read in another thread how to manage this, but something is not working properly.
In my situation I need to give a response.write not after downloading a file but after a RDC connection to a remote server.

So I have this in the server handler:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    QueryUserFromRemoteServer(resp, "user001"))
    StartMessageLoop
End Sub

And this is the sub:

B4X:
Sub QueryUserFromRemoteServer(resp As ServletResponse, user As String)
    Dim dbcmd As DBCommand
    dbcmd.Initialize
    dbcmd.Name = "QueryUser"
    dbcmd.Parameters=Array As Object(user)
    If dbreq.IsInitialized=False Then dbreq.Initialize(Me, "http://" & RemoteServer & ":" & RemotePort & "/rdc")
    dbreq.ExecuteQuery(dbcmd, 0, "tagQueryUser")
    wait for JobDone(j As HttpJob)
    If J.Success = True Then
        Dim result As DBResult = dbreq.HandleJob(J)
        For Each records() As Object In result.Rows
            If records(result.columns.Get("Authorized"))=True Then
                resp.Write("OK")
            Else
                resp.Write("UnAuthorized")
            End If
            Exit
        Next
    Else
        Log("Error in query user")
    End If
    j.Release
    StopMessageLoop
End Sub

This way the server hangs and then timeout with an error.
If I remove StarMessageLoop and StopMessageLoop looks like is ok server side, but the resp.write sometimes does not arrive in time to the client.

Where I'm wrong ?
I'm using jOkHttpUtils2_NONUI library
 

marcick

Well-Known Member
Licensed User
Longtime User
This is the log. Yes, tested in release also.

B4X:
java.net.SocketTimeoutException: timeout
    at okio.Okio$4.newTimeoutException(Okio.java:227)
    at okio.AsyncTimeout.exit(AsyncTimeout.java:284)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:240)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210)
    at okhttp3.internal.http1.Http1Codec.readResponse(Http1Codec.java:191)
    at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:132)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:54)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
    at okhttp3.RealCall.execute(RealCall.java:63)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:156)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:153)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:201)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Socket Closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at okio.Okio$2.read(Okio.java:138)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:236)
    ... 28 more
ResponseError. Reason: java.net.SocketTimeoutException: timeout, Response:
JobDone
Error in query user
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Yes, server logs.
It can't be a timeout problem I think. If I remove "StarMessageLoop" and "StopMessageLoop" it works and HTTPJob is executed in a shot.
But the client sometimes does not receive the response, for the reason you explained here I suppose:

https://www.b4x.com/android/forum/threads/resumable-subs-wait-for-sleep-in-server-handlers.81833/

One detail: the remote server of the HTTP request, is the same local server where where runs the app.
The real situation is: Client send a request to Server1. Server1 handler connect to Server2 via RDC, wait for the answer and reply to the client.
I don't have two server here to test everything, so I simulate everything with server1.
 
Upvote 0
Top