B4J Question Error on request inside a server Handler (java.lang.InterruptedException)

djmainero

Member
Licensed User
Longtime User
Hello!
I'm gettin this error but i have no clue of what's happening because it works good for days before happen.
I suspect that is caused by a request that I make inside a handler but I'm not sure.

Log:
java.lang.RuntimeException: java.lang.InterruptedException
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
    at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:130)
    at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
    at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
    at org.eclipse.jetty.servlets.DoSFilter.doFilterChain(DoSFilter.java:482)
    at org.eclipse.jetty.servlets.DoSFilter.doFilter(DoSFilter.java:327)
    at org.eclipse.jetty.servlets.DoSFilter.doFilter(DoSFilter.java:297)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
    at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:135)
    at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
    at anywheresoftware.b4j.object.JServlet.doFilter(JServlet.java:72)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
    at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:724)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:531)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.InterruptedException
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(Unknown Source)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
    at java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:43)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
    at com.xxx.yyy.generapago._handle(generapago.java:986)
    at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
    ... 44 more

The MSQL is released after this on another sub
generapago:
Dim j As HttpJob
j.Initialize("j", Me)

j.PostString(Main.URL360, JSON.ToString)
j.GetRequest.SetContentType("application/json")
j.GetRequest.SetHeader("Authorization", "Bearer " & APIKey)


Wait For (j) JobDone(Job As HttpJob)
Dim q As StringBuilder
q.Initialize
Dim Params As List
Params.Initialize
If Not(Job.Success) Then
    q.Append("INSERT INTO WEBPAGOSONLINE ")
    Params.Add("Error")
    Params.Add("idsFac: " & IDs & " | " & Job.GetString)
    Params.Add("Req: " & JSON.ToString)
    Params.Add("Movil")
    Params.Add(Funciones.TicksToClarion(DateTime.Now))
    MSQL.ExecNonQuery2(q.ToString, Params)
    Job.Release
    j.Release
    Return wResp.ArmaRespuestaMap("Error al consultar la API. Intente nuevamente más tarde.", False)
Else
    JSONP.Initialize(Job.GetString)
    Dim Res As Map = JSONP.NextObject
    DateTime.DateFormat = "yyyy-MM-dd"
    Dim sTemp As String
    If(Res.Get("id") > 1) Then
        Params.Add(Res.Get("id"))
        Params.Add(User)
        Params.Add(Res.Get("state"))
    Else
        Params.Add("Error")
        Params.Add("idsFac: " & IDs & " | " & Job.GetString)
        Params.Add("Req: " & JSON.ToString)
        Params.Add("Movil")
        Params.Add(Funciones.TicksToClarion(DateTime.Now))
    End If
End If
Job.Release
j.Release

Thanks in advance!
 

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

djmainero

Member
Licensed User
Longtime User
Thanks for reply Mandfred!
On the Sub Handle I have
Handle:
    GenerarPago(jMap, resp, SQL)
    StartMessageLoop
End If
If SQL <> Null And SQL.IsInitialized Then SQL.Close

Sub GenerarPago(jMap As Map, resp As ServletResponse, SQL As SQL):
Wait For(GeneraURLPago(SQL, IDsConBarra, Socio)) Complete (Result As String)
resp.Write(Result)
SQL.Close
If RS.IsInitialized Then RS.Close
StopMessageLoop

The GeneraURLPago is the function that I posted before

Sub GeneraURLPago(MSQL As SQL, IDs As String, User As Int) As ResumableSub:
Dim j As HttpJob
j.Initialize("j", Me)

j.PostString(Main.URL360, JSON.ToString)
j.GetRequest.SetContentType("application/json")
j.GetRequest.SetHeader("Authorization", "Bearer " & APIKey)


Wait For (j) JobDone(Job As HttpJob)
Dim q As StringBuilder
q.Initialize
Dim Params As List
Params.Initialize
If Not(Job.Success) Then
    q.Append("INSERT INTO WEBPAGOSONLINE ")
    Params.Add("Error")
    Params.Add("idsFac: " & IDs & " | " & Job.GetString)
    Params.Add("Req: " & JSON.ToString)
    Params.Add("Movil")
    Params.Add(Funciones.TicksToClarion(DateTime.Now))
    MSQL.ExecNonQuery2(q.ToString, Params)
    Job.Release
    j.Release
    Return wResp.ArmaRespuestaMap("Error al consultar la API. Intente nuevamente más tarde.", False)
Else
    JSONP.Initialize(Job.GetString)
    Dim Res As Map = JSONP.NextObject
    DateTime.DateFormat = "yyyy-MM-dd"
    Dim sTemp As String
    If(Res.Get("id") > 1) Then
        Params.Add(Res.Get("id"))
        Params.Add(User)
        Params.Add(Res.Get("state"))
    Else
        Params.Add("Error")
        Params.Add("idsFac: " & IDs & " | " & Job.GetString)
        Params.Add("Req: " & JSON.ToString)
        Params.Add("Movil")
        Params.Add(Funciones.TicksToClarion(DateTime.Now))
    End If
End If
Job.Release
j.Release

The request works but in couple of days depending the trafic stop working until I reboot the jar
 
Upvote 0

djmainero

Member
Licensed User
Longtime User
Is called once after the Wait For I have no other Wait For in the code.
The StartMessageLoop is called before the Wait For, at the end of Sub Handle

Sub Handle:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    GenerarPago(jMap, resp, SQL)
    StartMessageLoop
    If SQL <> Null And SQL.IsInitialized Then SQL.Close
End Sub

Sub GenerarPago(jMap As Map, resp As ServletResponse, SQL As SQL)
    Wait For(GeneraURLPago(SQL, IDsConBarra, Socio)) Complete (Result As String)
    resp.Write(Result)
    StopMessageLoop
End Sub

Sub GeneraURLPago(MSQL As SQL, IDs As String, User As Int) As ResumableSub
    Wait For (j) JobDone(Job As HttpJob)
    Return someValue
End Sub

Is that correct?
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
It is possible that the client is timing out before your code finishes executing, that could explain that it doesnt happen always, this is even more true as you are calling an external api.

Increase client timeout or handle the communication asynchronously with a background worker
 
Upvote 0

djmainero

Member
Licensed User
Longtime User
Thanks Enrique! I'll try the easier, increase time out
B4X:
job.GetRequest.Timeout = 60000
When the error appears, it stop working until I reboot the service and then it works again.
 
Upvote 0

djmainero

Member
Licensed User
Longtime User
Sorry i cut the sub
B4X:
Dim j As HttpJob
HttpUtils2Service.TempFolder = File.DirApp & "\temp"
j.Initialize("j", Me)
j.PostString(Main.URL, JSON.ToString) 'JSON is loaded in the same sub, I omitted because will be to long
Wait For (j) JobDone(Job As HttpJob)
If Not(Job.Success) Then
    Job.Release
    If j.IsInitialized Then j.Release
    Return "error"
End If
'do stuff with the result
job.release
If j.IsInitialized Then j.Release
Return someValue

I changed the request timeout, until now don't have any errors, but is not the day of the month with too much movements. So I keep waiting to see if it works.
 
Upvote 0
Top